开发者

Is there some sort of CacheDependency in System.Runtime.Caching?

开发者 https://www.devze.com 2022-12-31 21:58 出处:网络
I heard that .NET 4 has a new caching API. Okay, so the good old System.Web.Caching开发者_JS百科.Cache (which is, by the way, still there in .NET 4) has the ability to set so-called CacheDependency o

I heard that .NET 4 has a new caching API.

Okay, so the good old System.Web.Caching开发者_JS百科.Cache (which is, by the way, still there in .NET 4) has the ability to set so-called CacheDependency objects to determine whether a cached item is expired or not.

One can also specify custom logic for determining whether a cached item is still useable or not by deriving a custom subclass from CacheDependency.

I'm curious, is there a way to provide such a logic in the new API?


I haven't really used it yet but classes derived from ChangeMonitor Class appear to serve a similar purpose.


Using System.Web.Caching.Cache with a CacheDependency was constructed something like the following:

CacheDependency cacheDependency = new System.Web.Caching.CacheDependency(null, new string[] { "dependentOnThisKey" });
cacheInstance.Add("someCacheKey", new object(), cacheDependency);

With System.Runtime.Caching this moves into the CacheItemPolicy ChangeMonitors. The above example changes to the following:

CacheItemPolicy policy = new CacheItemPolicy();
policy.ChangeMonitors.Add(MemoryCache.Default.CreateCacheEntryChangeMonitor(new List<string> { "dependentOnThisKey" }));
cacheInstance.Add("someCacheKey", new object(), policy);
0

精彩评论

暂无评论...
验证码 换一张
取 消