ASP.Net 更新页面输出缓存的几种方法
(1)代码级缓存(对象缓存) Cache 对象
HttpRuntime.Cache.Remove(cacheEnmu.Key.ToString());
IDictionaryEnumerator cacheEnmu = HttpRuntime.Cache.GetEnumerator(); while (cacheEnmu.MoveNext()) { HttpRuntime.Cache.Remove(cacheEnmu.Key.ToString()); }
(2)页面级缓存 OutputCache
HttpResponse.RemoveOutputCacheItem(fname); //fname 是待清除缓存页面的绝对路径,如 /article/read.aspx
(3)用户控件级缓存 OutputCache
//arg 是系统传入的自定义变量名,需要在下面进行判断 //DataCache 是一个缓存类,这里用于存取 Cache 对象,当更新该Cache对象时,页面缓存同时更新
public override string GetVaryByCustomString(HttpContext context, string arg) {
if(arg == "ucCacheFlag")
{
Object _flag = DataCache.GetCache("ucCacheFlag"); if(_flag == null) { _flag = DateTime.Now.Ticks.ToString(); DataCache.SetCache( "ucCacheFlag", _flag, DateTime.Now.AddMinutes(CommonValue.CacheTime), TimeSpan.Zero); } return _flag.ToString(); } return base.GetVaryByCustomString(context, arg); }
<%@ OutputCache Duration="1800" VaryByCustom="ucCacheFlag" %> //ucCacheFlag 就是在GetVaryByCustomString()中判断的字符串
public override string GetVaryByCustomString(HttpContext context, string arg)
{
if (arg == "Cashgroupclass")
{
Cache objCache = HttpRuntime.Cache;
Object _flag = objCache["Cashgroupclass"];
if (_flag == null)
{
_flag = DateTime.Now.Ticks.ToString();
objCache.Insert("Cashgroupclass", _flag);
}
return _flag.ToString();
}
return base.GetVaryByCustomString(context, arg);
}
if (objCache["Cashgroupclass"] != null)
{
objCache.Remove("Cashgroupclass");
}
public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
HttpRuntime.Cache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。