提高应用程序的性能----全局应用程序缓存
来源:互联网 发布:linux虚拟机root权限 编辑:程序博客网 时间:2024/05/16 05:30
1-所有的地方都提供 了对全局Cache的访问.全局应用程序缓寸,具有绝对过期/可调过期/缓寸依赖(文件/目录/数据库依赖),缓寸删除回调.
Context.Cache.Insert ("Stocks"(key), ds(cache obj), null(缓寸依赖), Cache.NoAbsoluteExpiration(绝对过期), TimeSpan.FromHours (1)(可调过期), CacheItemPriority.AboveNormal(缓寸对象优先级别), null(删除回调));
Context.Cache.Add ("Stocks", ds, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
例子:
<%@ Import Namespace="System.Data" %><script language="C#" runat="server"> static Cache _cache; static string _path; void Application_Start () { _cache = Context.Cache; _path = Context.Server.MapPath ("Stocks.xml"); DataSet ds = new DataSet (); ds.ReadXml (_path); _cache.Insert ("Stocks", ds, new CacheDependency (_path), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback (RefreshDataSet)); } static void RefreshDataSet (String key, Object item, CacheItemRemovedReason reason) { DataSet ds = new DataSet (); ds.ReadXml (_path); _cache.Insert ("Stocks", ds, new CacheDependency (_path), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback (RefreshDataSet)); }</script>
2/线程安全:
The application cache doesn’t have Lock and UnLock methods as application state does. But that doesn’t mean locking isn’t necessary; it means you have to come up with your own mechanism for doing it. System.Threading.ReaderWriterLock is the perfect tool for the job. Assuming rwlock is an instance of ReaderWriterLock, here’s how you’d lock the application cache during an update:
rwlock.AcquireWriterLock (Timeout.Infinite);try { Cache["ItemsSold"] = (int) Cache["ItemsSold"] + 1; Cache["ItemsLeft"] = (int) Cache["ItemsLeft"] - 1;}finally { rwlock.ReleaseWriterLock ();}
And here’s how you’d read “ItemsSold” and “ItemsLeft” values from the cache:
rwlock.AcquireReaderLock (Timeout.Infinite);try { int sold = (int) Cache["ItemsSold"]; int left = (int) Cache["ItemsLeft"];}finally { rwlock.ReleaseReaderLock ();}
- 提高应用程序的性能----全局应用程序缓存
- 提高应用程序的性能
- 巧用缓存提高ASP应用程序的性能
- 使用缓存计算来提高应用程序的性能
- 提高JDBC应用程序的性能
- 如何提高应用程序性能?
- WPF应用程序性能提高
- 提高应用程序启动性能
- 如何提高应用程序性能
- 如何提高 ASP.Net 应用程序的性能
- 提高ASP.NET 应用程序的性能
- 提高asp.net应用程序性能的方法
- 提高 Silverlight 应用程序性能的小贴士
- 提高 Silverlight 应用程序性能的小贴士
- 进一步提高JDBC应用程序的性能(一)
- 进一步提高JDBC应用程序的性能 (四)
- 使用Memcached提高.NET应用程序的性能
- 如何提高网络应用程序的性能
- 保险该不该买(2009.10-3 周六 第一财经理财宝典)
- android develop
- Java中关于String需要明确的几个知识点
- 什么是ARP
- 6级菜单
- 提高应用程序的性能----全局应用程序缓存
- 2个简单排序算法的java实现
- zoj 1745
- 开站测试
- ubuntu8.10上网设置
- Setting Environment Variables in Mac OSX
- 分析函数over 及开窗函数
- Detecting HTTP Load Balancers using Halberd
- 高手进阶,终极内存技术指南——完整/进阶版 I(转)