2

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.

4 Answers 4

3

You can use System.Web.HttpRuntime.Cache to access the cache statically.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

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.
0

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.

Comments

0

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

1 Comment

HttpSessionState provides access to the session. Not the Cache so you haven't actually answered your own question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.