Microsoft Enterprise Library中的Cache模块能在多种场景中提供缓存功能:Web程序,桌面程序,WPF程序。我们可以配置Cache模块的缓存处理器,目前系统自带3个缓存处理器:In-Memory,Isolated Storage Cache Store 和 Data Cache Storage。 并且我们还能实现自己的缓存处理器,只需要实现接口 IBackingStore,我们又可以发明自己的轮子啦:)
In-Memory:保存在内存中。
Isolated Storage Cache Store:系统将缓存的信息保存在独立文件中(C:\Users\<<user name>>\AppData\Local\IsolatedStorage)。
Data Cache Storage:将缓存数据保存在数据库中。
Custom Cache Storage:自己扩展的处理器。我们可以将数据保存在注册表中或文本文件中。
ICacheManager cacheManager = EnterpriseLibraryContainer.Current.GetInstance<ICacheManager>(); cacheManager.Add("A", "This is letter A."); cacheManager.Add("A", "This is letter A."); cacheManager.Add("B", "This is letter B."); cacheManager.Add("C", "This is letter C."); cacheManager.Add("D", "This is letter D."); cacheManager.Add("E", "This is letter E."); Console.WriteLine(string.Format("currently there are {0} elements in cache pool.", cacheManager.Count)); cacheManager.Remove("B"); cacheManager.Remove("C"); Console.WriteLine(string.Format("currently there are {0} elements in cache pool.", cacheManager.Count)); cacheManager.Flush();
simeple demo ICacheManager cacheManager = EnterpriseLibraryContainer.Current.GetInstance<ICacheManager>(); cacheManager.Add("A", "This is letter A"); cacheManager.Add("B", "This is letter B"); Console.WriteLine(string.Format("{0}:{1}", "A", cacheManager.GetData("A"))); cacheManager.Remove("A"); Console.WriteLine(string.Format("currently there are {0} elements in cache pool.", cacheManager.Count)); cacheManager.Flush(); Console.WriteLine(string.Format("currently there are {0} elements in cache pool.", cacheManager.Count));
ICacheManager cacheManager = EnterpriseLibraryContainer.Current.GetInstance<ICacheManager>(); cacheManager.Add("A", "This is letter A."); cacheManager.Add("A", "This is letter A."); cacheManager.Add("B", "This is letter B."); cacheManager.Add("C", "This is letter C."); cacheManager.Add("D", "This is letter D."); cacheManager.Add("E", "This is letter E."); Console.WriteLine(string.Format("currently there are {0} elements in cache pool.", cacheManager.Count)); cacheManager.Remove("B"); cacheManager.Remove("C"); Console.WriteLine(string.Format("currently there are {0} elements in cache pool.", cacheManager.Count)); cacheManager.Flush();
ICacheManager cacheManager = EnterpriseLibraryContainer.Current.GetInstance<ICacheManager>(); cacheManager.Add("A", "This is letter A."); cacheManager.Add("A", "This is letter A."); cacheManager.Add("B", "This is letter B."); cacheManager.Add("C", "This is letter C."); cacheManager.Add("D", "This is letter D."); cacheManager.Add("E", "This is letter E."); Console.WriteLine(string.Format("currently there are {0} elements in cache pool.", cacheManager.Count)); cacheManager.Remove("B"); cacheManager.Remove("C"); Console.WriteLine(string.Format("currently there are {0} elements in cache pool.", cacheManager.Count)); cacheManager.Flush();
namespace SimpleCacheDemo { using System; using System.Linq; using System.Text; using System.Configuration; using System.Data; using System.IO; using Microsoft.Win32; using System.Collections.Generic; using System.Collections.Specialized; using System.Collections.Generic; using System.Collections; using Microsoft.Practices.EnterpriseLibrary.Common; using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; using Microsoft.Practices.ServiceLocation; using Microsoft.Practices.Unity.Configuration; using Microsoft.Practices.EnterpriseLibrary.Caching; using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations; using Microsoft.Practices.EnterpriseLibrary.Caching.Configuration; /// <summary> /// TODO: Update summary. /// </summary> /// [ConfigurationElementType(typeof(CustomCacheStorageData))] public class CustomStorage:IBackingStore { public CustomStorage(NameValueCollection keys) { } public void Add(CacheItem newCacheItem) { RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey("software", true); RegistryKey microsoft = software.OpenSubKey("microsoft", true); RegistryKey entryDir = microsoft.CreateSubKey("customcache", RegistryKeyPermissionCheck.ReadWriteSubTree); if (entryDir != null) { if (entryDir.GetValue(newCacheItem.Key) == null) { entryDir.SetValue(newCacheItem.Key, newCacheItem.Value.ToString(), RegistryValueKind.String); } entryDir.Close(); } microsoft.Close(); software.Close(); } public int Count { get { int keyCount = 0; RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey("software", true); RegistryKey microsoft = software.OpenSubKey("microsoft", true); using (RegistryKey entryDir = microsoft.CreateSubKey("customcache", RegistryKeyPermissionCheck.ReadWriteSubTree)) { if (entryDir != null) { keyCount = entryDir.GetValueNames().Length; } } return keyCount; } } public void Flush() { RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey("software", true); RegistryKey microsoft = software.OpenSubKey("microsoft", true); microsoft.DeleteSubKeyTree("customcache"); } public System.Collections.Hashtable Load() { Hashtable cacheItemTbl = new Hashtable(); RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey("software", true); RegistryKey microsoft = software.OpenSubKey("microsoft", true); using (RegistryKey entryDir = microsoft.CreateSubKey("customcache", RegistryKeyPermissionCheck.ReadWriteSubTree)) { if (entryDir != null) { foreach (string key in entryDir.GetValueNames()) { cacheItemTbl.Add(key, entryDir.GetValue("key")); } } } return cacheItemTbl; } public void Remove(string key) { RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey("software", true); RegistryKey microsoft = software.OpenSubKey("microsoft", true); using (RegistryKey entryDir = microsoft.CreateSubKey("customcache", RegistryKeyPermissionCheck.ReadWriteSubTree)) { if (entryDir != null) { entryDir.DeleteValue(key); } } } public void UpdateLastAccessedTime(string key, DateTime timestamp) { } public void Dispose() { } } }
////cache expire regulation string moniterFilePath = AppDomain.CurrentDomain.BaseDirectory + "tmp.txt"; ICacheManager cacheManager = EnterpriseLibraryContainer.Current.GetInstance<ICacheManager>(); if (File.Exists(moniterFilePath) == false) { using (File.Create(moniterFilePath)) { } } // these class was implemented interface ICacheItemExpiration NeverExpired neverExpired = new NeverExpired(); AbsoluteTime absoluteTime = new AbsoluteTime(new TimeSpan(0, 0, 10));//ten seconds SlidingTime slidingTime = new SlidingTime(new TimeSpan(0, 0, 10)); FileDependency fileDependency = new FileDependency(moniterFilePath); //没实现,构造函数中的参数比较奇怪 ExtendedFormatTime extendedFormatTIme = new ExtendedFormatTime("Hour - 0-23"); //cache refresh listener CacheRefreshContainer cacheRefreshContainer = new CacheRefreshContainer(cacheManager, slidingTime); CacheRefresher cacheRefresher = new CacheRefresher(cacheRefreshContainer);//new CacheRefresher();//new CacheRefresher(cacheManager); //seems we can't add a new item in cache pool during CacheRefresher was executing. cacheManager.Add("A", "This is letter A!", CacheItemPriority.High, cacheRefresher, slidingTime); Console.WriteLine(string.Format("{0}:{1}", "A", cacheManager.GetData("A"))); System.Threading.Thread.Sleep(15000); Console.WriteLine(string.Format("{0}:{1}", "AB", cacheManager.GetData("AB"))); //test encryptograme function cacheManager.Add("A", "This is letter A."); Console.WriteLine(string.Format("{0}:{1}", "A", cacheManager.GetData("A"))); Console.ReadKey(); cacheManager.Flush();
[Serializable] public class CacheRefresher:ICacheItemRefreshAction { //ICacheManager _cacheManager; //public CacheRefresher(ICacheManager cacheManager) //{ // _cacheManager = cacheManager; //} CacheRefreshContainer _cacheRefreshContainer; public CacheRefresher(CacheRefreshContainer cacheRefreshContainer) { _cacheRefreshContainer = cacheRefreshContainer; } public void Refresh(string removedKey, object expiredValue, CacheItemRemovedReason removalReason) { //_cacheManager.Remove(removedKey); //_cacheManager.Add(removedKey + "B", expiredValue.ToString() + " new!"); } }