views:

17

answers:

1

I have written a small ASHX handler that uses a small state object that I'd like to persist through the lifetime of a series of requests. I have the handler putting the object in the server-side cache (HttpContext.Current.Cache) and retrieving it at the start of ProcessRequest.

How long can I expect that object to remain in the cache? I expect handler instances to come and go, so I wanted something to persist across all of them (until no longer needed as determined by the requests themselves). However, if I have the handler write to the application log when it has to create a new state object due to it not being in the cache, I'm seeing it create it 2-3 times.

+1  A: 

You can specify the lifetime and priority when you add the item to the cache.

An item isn't guaranteed to stay in the cache for the entire requested lifetime. For example, if there's memory pressure then the cache might be purged, but setting a higher priority for your item makes it more likely that it will remain in the cache.

LukeH
I was using Cache[] to add the item. Thank you.
oakskc
If you just use `Cache[]` to add an item then the default lifetime and prioriy policies are used. I'm not sure what they are, but I'm pretty sure that your item shouldn't be regularly dropping out of the cache unless you're experiencing memory pressure (and/or your appdomain is restarted for some reason).
LukeH