Microsoft Enterprise Library: Cache 模块
来源:互联网 发布:网络摄像头播放应用 编辑:程序博客网 时间:2024/05/18 02:50
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:自己扩展的处理器。我们可以将数据保存在注册表中或文本文件中。
In-Memory
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();
配置信息
Isolated Storage Cache Store
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));
配置信息
Data 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();
配置信息
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();
Custom Cache Storage实现代码
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();
CacehRefresher
[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!"); } }
- Microsoft Enterprise Library: Cache 模块
- Microsoft Enterprise Library: Logging 模块
- Microsoft Enterprise Library: Exception Handle 模块
- Microsoft Enterprise Library 发布
- Microsoft Enterprise Library 4.0
- Microsoft Enterprise Library
- Microsoft Enterprise Library
- 微软企业库 Microsoft Enterprise Library
- 工具分享:Microsoft Enterprise Library
- Enterprise Library 3.0--Cache Application Block
- Microsoft patterns & practices Enterprise Library下载链接
- Microsoft patterns & practices Enterprise Library下载链接
- Microsoft patterns & practices Enterprise Library下载链接
- Microsoft patterns & practices Enterprise Library下载链接
- Microsoft patterns & practices Enterprise Library下载链接
- Microsoft Enterprise Library 5.0 is coming
- Microsoft Enterprise Library 5.0 is coming
- Microsoft Enterprise Library 5.0 系列(五)
- Three20研究院之构建开发环境与HelloWorld(一)
- spring上下文在web.xml中的配置
- 依然想你
- NSHomeDirectory()
- android in practice_Using Cloud to Device Messaging(portfolio project)
- Microsoft Enterprise Library: Cache 模块
- 新入手Seagate希捷1TB移动硬盘1t USB3.0一个
- ArcGis 中通过 javascript api 来访问 Geoprocessor 服务
- oracle分页查询 .
- 最实简单的分页的存储过程
- 2012年C++期末总结
- 03,构建WP8应用程序
- IOS的一些文件操作。(沙箱) 在Documents目录下创建文件
- java 遍历磁盘目录