这种策略让缓存依赖于一个指定的文件,通过改变文件的更新日期来清除缓存。
1 |
<div style="margin:0px;padding:0px;line-height:21px;"><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"></span><span style="margin:0px;padding:0px;color:#808080;"><summary></span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"> 获取当前应用程序指定CacheKey的Cache对象值<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"></span><span style="margin:0px;padding:0px;color:#808080;"></summary></span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"></span><span style="margin:0px;padding:0px;color:#808080;"><param name="CacheKey"></span><span style="margin:0px;padding:0px;color:#008000;">索引键值</span><span style="margin:0px;padding:0px;color:#808080;"></param></span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"></span><span style="margin:0px;padding:0px;color:#808080;"><returns></span><span style="margin:0px;padding:0px;color:#008000;">返回缓存对象</span><span style="margin:0px;padding:0px;color:#808080;"></returns></span><span style="margin:0px;padding:0px;color:#808080;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;">public</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">static</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">object</span><span style="margin:0px;padding:0px;color:#000000;"> GetCache(</span><span style="margin:0px;padding:0px;color:#0000ff;">string</span><span style="margin:0px;padding:0px;color:#000000;"> CacheKey)<br style="margin:0px;padding:0px;line-height:10px;" />{<br style="margin:0px;padding:0px;line-height:10px;" /> System.Web.Caching.Cache objCache </span><span style="margin:0px;padding:0px;color:#000000;">=</span><span style="margin:0px;padding:0px;color:#000000;"> HttpRuntime.Cache;<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;">return</span><span style="margin:0px;padding:0px;color:#000000;"> objCache[CacheKey];<br style="margin:0px;padding:0px;line-height:10px;" />}<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"></span><span style="margin:0px;padding:0px;color:#808080;"><summary></span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"> 设置以缓存依赖的方式缓存数据<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"></span><span style="margin:0px;padding:0px;color:#808080;"></summary></span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"></span><span style="margin:0px;padding:0px;color:#808080;"><param name="CacheKey"></span><span style="margin:0px;padding:0px;color:#008000;">索引键值</span><span style="margin:0px;padding:0px;color:#808080;"></param></span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"></span><span style="margin:0px;padding:0px;color:#808080;"><param name="objObject"></span><span style="margin:0px;padding:0px;color:#008000;">缓存对象</span><span style="margin:0px;padding:0px;color:#808080;"></param></span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#808080;">///</span><span style="margin:0px;padding:0px;color:#008000;"></span><span style="margin:0px;padding:0px;color:#808080;"><param name="cacheDepen"></span><span style="margin:0px;padding:0px;color:#008000;">依赖对象</span><span style="margin:0px;padding:0px;color:#808080;"></param></span><span style="margin:0px;padding:0px;color:#808080;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;">public</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">static</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">void</span><span style="margin:0px;padding:0px;color:#000000;"> SetCache(</span><span style="margin:0px;padding:0px;color:#0000ff;">string</span><span style="margin:0px;padding:0px;color:#000000;"> CacheKey, </span><span style="margin:0px;padding:0px;color:#0000ff;">object</span><span style="margin:0px;padding:0px;color:#000000;"> objObject, System.Web.Caching.CacheDependency dep)<br style="margin:0px;padding:0px;line-height:10px;" />{<br style="margin:0px;padding:0px;line-height:10px;" /> System.Web.Caching.Cache objCache </span><span style="margin:0px;padding:0px;color:#000000;">=</span><span style="margin:0px;padding:0px;color:#000000;"> HttpRuntime.Cache;<br style="margin:0px;padding:0px;line-height:10px;" /> objCache.Insert(<br style="margin:0px;padding:0px;line-height:10px;" /> CacheKey,<br style="margin:0px;padding:0px;line-height:10px;" /> objObject,<br style="margin:0px;padding:0px;line-height:10px;" /> dep,<br style="margin:0px;padding:0px;line-height:10px;" /> System.Web.Caching.Cache.NoAbsoluteExpiration, </span><span style="margin:0px;padding:0px;color:#008000;">//</span><span style="margin:0px;padding:0px;color:#008000;">从不过期</span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#000000;"> System.Web.Caching.Cache.NoSlidingExpiration, </span><span style="margin:0px;padding:0px;color:#008000;">//</span><span style="margin:0px;padding:0px;color:#008000;">禁用可调过期</span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#000000;"> System.Web.Caching.CacheItemPriority.Default,<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;">null</span><span style="margin:0px;padding:0px;color:#000000;">);<br style="margin:0px;padding:0px;line-height:10px;" />}<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;">protected</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">void</span><span style="margin:0px;padding:0px;color:#000000;"> Page_Load(</span><span style="margin:0px;padding:0px;color:#0000ff;">object</span><span style="margin:0px;padding:0px;color:#000000;"> sender, EventArgs e)<br style="margin:0px;padding:0px;line-height:10px;" />{<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;">string</span><span style="margin:0px;padding:0px;color:#000000;"> CacheKey </span><span style="margin:0px;padding:0px;color:#000000;">=</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#800000;">"</span><span style="margin:0px;padding:0px;color:#800000;">cachetest</span><span style="margin:0px;padding:0px;color:#800000;">"</span><span style="margin:0px;padding:0px;color:#000000;">;<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#0000ff;">object</span><span style="margin:0px;padding:0px;color:#000000;"> objModel </span><span style="margin:0px;padding:0px;color:#000000;">=</span><span style="margin:0px;padding:0px;color:#000000;"> GetCache(CacheKey);</span><span style="margin:0px;padding:0px;color:#008000;">//</span><span style="margin:0px;padding:0px;color:#008000;">从缓存中获取</span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">if</span><span style="margin:0px;padding:0px;color:#000000;"> (objModel </span><span style="margin:0px;padding:0px;color:#000000;">==</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">null</span><span style="margin:0px;padding:0px;color:#000000;">) </span><span style="margin:0px;padding:0px;color:#008000;">//</span><span style="margin:0px;padding:0px;color:#008000;">缓存里没有</span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#000000;"> {<br style="margin:0px;padding:0px;line-height:10px;" /> objModel </span><span style="margin:0px;padding:0px;color:#000000;">=</span><span style="margin:0px;padding:0px;color:#000000;"> DateTime.Now;</span><span style="margin:0px;padding:0px;color:#008000;">//</span><span style="margin:0px;padding:0px;color:#008000;">把当前时间进行缓存</span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">if</span><span style="margin:0px;padding:0px;color:#000000;"> (objModel </span><span style="margin:0px;padding:0px;color:#000000;">!=</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">null</span><span style="margin:0px;padding:0px;color:#000000;">)<br style="margin:0px;padding:0px;line-height:10px;" /> {<br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#008000;">//</span><span style="margin:0px;padding:0px;color:#008000;">依赖 C:\\test.txt 文件的变化来更新缓存</span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#000000;"> System.Web.Caching.CacheDependency dep </span><span style="margin:0px;padding:0px;color:#000000;">=</span><span style="margin:0px;padding:0px;color:#000000;"></span><span style="margin:0px;padding:0px;color:#0000ff;">new</span><span style="margin:0px;padding:0px;color:#000000;"> System.Web.Caching.CacheDependency(</span><span style="margin:0px;padding:0px;color:#800000;">"</span><span style="margin:0px;padding:0px;color:#800000;">C:\\test.txt</span><span style="margin:0px;padding:0px;color:#800000;">"</span><span style="margin:0px;padding:0px;color:#000000;">);<br style="margin:0px;padding:0px;line-height:10px;" /> SetCache(CacheKey, objModel, dep);</span><span style="margin:0px;padding:0px;color:#008000;">//</span><span style="margin:0px;padding:0px;color:#008000;">写入缓存</span><span style="margin:0px;padding:0px;color:#008000;"><br style="margin:0px;padding:0px;line-height:10px;" /></span><span style="margin:0px;padding:0px;color:#000000;"> }<br style="margin:0px;padding:0px;line-height:10px;" /> } <br style="margin:0px;padding:0px;line-height:10px;" /> Label1.Text </span><span style="margin:0px;padding:0px;color:#000000;">=</span><span style="margin:0px;padding:0px;color:#000000;"> objModel.ToString();<br style="margin:0px;padding:0px;line-height:10px;" />}</span></div> |
当我们改变test.txt的内容时,缓存会自动更新。这种方式非常适合读取配置文件的缓存处理。如果配置文件不变化,就一直读取缓存的信息,一旦配置发生变化,自动更新同步缓存的数据。
这种方式的缺点是,如果缓存的数据比较多,相关的依赖文件比较松散,对管理这些依赖文件有一定的麻烦。对于负载均衡环境下,还需要同时更新多台Web服务器下的缓存文件,如果多个Web应用中的缓存依赖于同一个共享的文件,可能会省掉这个麻烦。
FROM URI:http://kb.cnblogs.com/page/69726/