R.C
R.C

Reputation: 10565

Cache dependency between two items in cache

I have two items in cache. My requirement is to make one item in a cache dependent on another single item in same cache.

Cache["UserName"] = "Test User";
Cache["Message"] = "Test Message";

when Cache["UserName"] changes or is removed from the cache, Cache["Message"] should automatically be dropped/invalidated.

Using DateTime is not my requirement and neither any file.

Upvotes: 0

Views: 177

Answers (2)

TCM
TCM

Reputation: 16900

You can give your cache keys as an argument and it is supported by default in Asp.Net. Read this for more info:

http://msdn.microsoft.com/en-us/library/system.web.caching.cachedependency.aspx

or especially this one:

http://msdn.microsoft.com/en-us/library/818kahch.aspx

Giving a filename is not mandatory. Cache keys are just supported out of the box.

Cache["key1"] = "Value 1";

// Make key2 dependent on key1.
String[] dependencyKey = new String[1];
dependencyKey[0] = "key1";
CacheDependency dependency = new CacheDependency(null, dependencyKey);

Cache.Insert("key2", "Value 2", dependency);

Upvotes: 1

Royi Namir
Royi Namir

Reputation: 148524

Try this

Cache["UserName"] = "Test User";

// Make Cache["Message"] dependent on Cache["UserName"].
string[] dependencyKey = new string[1];
dependencyKey[0] = "UserName";

CacheDependency dependency = new CacheDependency(null, dependencyKey);
Cache.Insert("Message", "Test Message", dependency);

Upvotes: 1

Related Questions