文件依赖缓存的运用

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO ; using System.Xml ; using System.Web.Caching ; using System.Threading ; namespace BasicCompent { public class PageCssprovider { static System.Web.HttpContext cxt; static ManualResetEvent mr = new ManualResetEvent(true); static ManualResetEvent mw = new ManualResetEvent(true); static XmlDocument _doc; private PageCssprovider() { } static PageCssprovider() { cxt = System.Web.HttpContext.Current; Init(); } public static string PageCss(string pageName) { if (string.IsNullOrEmpty(pageName)) return string.Empty; string xmpath = string.Format("/pages/page[@name=/"{0}/"]",pageName); mw.WaitOne(); mr.Reset(); try { if(_doc ==null ) return string.Empty ; XmlNode node = _doc.SelectSingleNode(xmpath); return node.InnerXml; } finally { mr.Set(); } } static void Init() { mr.WaitOne(); mw.Reset(); try { string realPath = cxt.Server.MapPath("~/Css.xml"); if (!File.Exists(realPath)) return; _doc = new XmlDocument(); _doc.Load(realPath); CacheDependency cd = new CacheDependency(realPath); cxt.Cache.Insert("pageCss",realPath,cd,Cache.NoAbsoluteExpiration,Cache.NoSlidingExpiration,CacheItemPriority.NotRemovable,reportRemovedCallback); } finally { mw.Set(); } } static void reportRemovedCallback(String key,object value,CacheItemRemovedReason reason) { Init(); } } }

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...