problem with items disappearing for cache

G

Guest

Hi Everybody,

I'm trying to save some items in the asp.net cache, but they are removed
from the cache WAY before they are supposed to (at least as I understand the
documentation).

I add the object to the cache using

this.Context.Cache.Insert(key, userInfo, null,
System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10),
System.Web.Caching.CacheItemPriority.Default,
new System.Web.Caching.CacheItemRemovedCallback(itemRemoved));

but the Item is removed from the cache (with reason of underused) after
something like 1 minute.

What is going on here?

Thanks,
Nadav
 
K

Karl Seguin [MVP]

Is the system under memory pressure?

Is the worker process being recycled?

There are performance counters to help identify both problems above, in
addition to specific performance counters that track the behaviour of the
Cache API.

Karl
 
S

Steven Cheng[MSFT]

Thanks for Karl's input.

Hello Nadav,

Assume that you haven't used webfarm or webgarden for your application(only
hosted on a single server and single worker process). As Karl has
mentioned, you can first check the memory pressure on the server machine.
There are some cases that the ASP.NET web application will restart or the
worker process will recycle, this will surely cause the ASP.NET
applciation's cache data be lost.

For ASP.NET worker process recycle, you can try looking up the server's
event log to see whether there is worker process recycle entries there. For
ASP.NET application restart, you can use the Application_End,
application_Start event to do logging.

#Logging ASP.NET Application Shutdown Events
http://weblogs.asp.net/scottgu/archive/2005/12/14/433194.aspx

Also, for ASP.NET application/process healthy monitoring, you can also
utilize the windows performance counters. Here are some specific categories
and counters

**Process
**Thread
**Processor
**Memory
** .NET CLR Data
**.NET CLR Exceptions
** .NET CLR Jit
**.NET CLR LocksAndThreads
**.NET CLR Memory
** ASP.NET v2.0.50727
** ASP.NET Applications v2.0.50727

Hope this also helps some for your troubleshooting.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi Steven,

Yes, the machine is under memory pressure
(I assume that Page File usage of 1.5GB with 1GB of ram is considered memory
pressure).
Will recycling of the worker process interfere with debugging?
I mean, can the web server recycle the worker process while I'm debugging
the application without me noticing?

(by the way I'm using the internal web server of VS2005, not IIS)

If the problem IS memory pressure, is there some way for me to prevent this
recycling happening?
Some items on the cache I can recreate if needed, so this is not a problem.
But in some cases, at the place I'm reading the item from the cache, I can't
recreate the item, so I MUST insure that the object will remain in the cache
for a certain amount of time.

I'm trying to implement a DynamicImage control where an event handler of the
control creates the image and stores it in the cache, and the HttpHandler
later reads it from the cache and returns it. The HttpHandler CAN'T recreate
the image!

Thanks,
Nadav
 
S

Steven Cheng[MSFT]

Thanks for your reply Nadav,

If you're debugging the web application(the worker process), I think you
can be awared of the application restart or worker process recycle. Since
you mentioned that your machine does be under memory pressure, I think you
can try changing your cached item's Priority to see whether the problem
remains. e.g.

Cache.Insert("DSN", connectionString, null, DateTime.Now.AddMinutes(2),
TimeSpan.Zero, CacheItemPriority.NotRemovable, null);


#CacheItemPriority Enumeration
http://msdn2.microsoft.com/en-us/library/system.web.caching.cacheitempriorit
y(VS.80).aspx


As the document mentioned, the "NotRemovable" priority can help prevent
your cached item be removed when system free memory.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
K

Karl Seguin [MVP]

If you REQUIRE an item to be available, you shouldn't use the cache,
regardless of what the priority is.

You must use the Application object, which WILL pin the object in memory.

However, an app recycle will still cause application items to get dropped.

Karl
 
G

Guest

Hi Steven,

Thanks, this is exactly what I need.

I tried to change the CacheItemPriority to High, Some how I must have missed
the
NotRemovable priority!

I will change my code and see if it solved the problem.

Thanks again
Nadav
 
S

Steven Cheng[MSFT]

thanks for your reply Nadav,

If you meet any further problem, please feel free to followup here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi Karl,

The problem with the Application object is that objects stored there will
NEVER expire unless I (i.e. my code) explicitly remove them.
I don't want to have to add code to handle this
Especially when I sometimes don't know when I don't need this data anymore...

I WANT my data to be expired after a period of inactivity without having to
recycle the whole application.
All I want is to be sure that the object will NOT be removed before I can
use it.
A pity you can't specify a minimum interval when the cache will NOT expire
it because of memory pressure and after that it can.

Thanks,
Nadav
 
S

Steven Cheng[MSFT]

Hi Nadav,

So does the "NotRemovable" priority finally resolve the previous problem?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi Steven,

Yes, the NotRemovable priority did fix the problem.
So I didn't reply sooner.

Thanks for your help.
Nadav
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top