MemoryCache Cache---Cache aging test
var cachepool = new Mycachepool ();
Thread.Sleep (1000);
var value = Cachepool.getfilevalue ();
/// <summary> ///MemoryCache Cache/// </summary> Public classMycachepool {ObjectCache cache=Memorycache.default; Const stringCacheKey ="Testcachekey"; Public stringGetValue () {varContent = Cache[cachekey] as string; if(Content = =NULL) { //Console.WriteLine ("Get New Item"); varPolicy =NewCacheItemPolicy () {absoluteexpiration = DateTime.Now.AddSeconds (5) }; Content=Guid.NewGuid (). ToString (); Cache. Set (CacheKey, content, policy); } Else{Console.WriteLine ("Get Cached Item"); } returncontent; } Public stringGetfilevalue () {stringStrcachekey ="Filecachekey"; varContent = Cache[strcachekey] as string; if(Content = =NULL) { //Console.WriteLine ("Get New Item"); //var file = @ "E:\test.txt"; //cacheitempolicy policy = new CacheItemPolicy (); //policy. Changemonitors.add (New Hostfilechangemonitor (new list<string> {file})); //content = File.readalltext (File); //Cache. Set (Strcachekey, content, policy);cacheitempolicy Policy=NewCacheItemPolicy (); Policy. Absoluteexpiration= DateTimeOffset.Now.AddMinutes (3); Content=Guid.NewGuid (). ToString (); CacheItem Item=NewCacheItem ("Cachedtext", content); List<string> keys =Newlist<string>{Strcachekeychange}; Policy. Changemonitors.add (Cache. Createcacheentrychangemonitor (keys)); //dependent on a value changeCache. Set (item, policy); } Else{Console.WriteLine ("Get Cached Item"); } returncontent; } }
View Code
MemoryCache Cache---cache aging