1

I have data common for all users stored in HttpRuntime.Cache.

Then I have some user related data stored in Session.

HttpRuntime.Cache has CacheDependency mechanism, which can be used to define relationships between the items in cache.

What would you use to handle the dependency between Session and Runtime Cache?

Additional to CacheDependencies, there is also CacheItemRemovedCallback. I could remove specific values from each session during this callback, if it's even possible.

Radek Stromský
  • 838
  • 3
  • 12
  • 27

1 Answers1

2

The cache dependency works the other way, i.e. when the cache is dependent on something else, not when something is dependent on the cache.

Using the CacheItemRemovedCallback to update items in the Session object is not possible. The callback is not called in the scope of a specific user, so you can't access the Session object. You would have to keep the data somewhere else, so that you can access the data for all users, as the data depending on the item removed from the cache may belong to any user.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Yes, I know the purpose of the cache dependency. I thought there's might be something similar what could handle dependency between session and cache. But MAYBE, it would be the easiest way to move user-related data from sessions to some dictionary or hashtable stored in runtime cache. – Radek Stromský Aug 04 '11 at 07:42
  • 2
    @Radex: Yes, storing user dependent data in the object that it depends on might be easier. You may want to keep a collection of references to objects where there is user data, so that you can clean out data for a user from the `Session_End` event. Remember that web applications are multi threaded, so you need to synchronise access to any object that is accessed from different users. – Guffa Aug 04 '11 at 07:53