views:

156

answers:

3

I have a wrapper class for Caching (CachingBL) where I store users that are currently signed in (some of their session info).

In CachingBL wrapper there is actually a dictionary of users, and I am putting that dictionary in cache like this: HttpContext.Current.Cache.Insert(...):

At the session end I would need to access to the cache like this:

var cacheBL = (CacheBL)HttpContext.Current.Cache.Get("MyCache_CacheSlot");

But the problem is that HttpContext.Current is empty, so I cannot access the Cache object. The Cache itself is not empty (tested), but I can't figure out how to access it at Session_End.

+1  A: 

Instead of putting the whole dictionary in the cache as one cache entry, put each element in the cache as an entry. Then you can give each element a sliding time window of the session timeout time, and let the system handle expiration.

Chris Shaffer
But session and cache entry could expire at different times. I need exactly the same mapping - all the users that are in session need to have the cache entry. At the moment the session expires, user needs to be removed from cache.
the berserker
A: 

Inside the Session_OnEnd event there is no way to get access to the HttpContext.Current because there is no current request.

But you do have access to the session state which includes all session variables. So if you us a session variable to store your token to the key name of the sessions cache slot ("MyCache_CacheSlot" in your example), you will be able to release that cache inside the Session_OnEnd event.

Mark Arnott
A: 

System.Web.SessionState.HttpSessionState is the one I should use instead of HttpContext.Current

the berserker