您的位置:首页 > 编程语言 > ASP

ASP.Net 更新页面输出缓存的几种方法

2015-05-21 15:25 781 查看
ASP.Net 自带的缓存机制对于提高页面性能有至关重要的作用,另一方面,缓存的使用也会造成信息更新的延迟。如何快速更新缓存数据,有时成了困扰程序员的难题。根据我的使用经验,总结了下面几种方法,概括了常见的几种情况,如有更好的方法欢迎补充。

(1)代码级缓存(对象缓存) Cache 对象

Cache 对象提供代码级的缓存,功能强大,可操作性强。更新这种缓存的方法很简单,只要调用 Cache.Remove(key) 方法就可以清除指定的缓存。代码如下:
HttpRuntime.Cache.Remove(cacheEnmu.Key.ToString());

下列代码清空所有 Cache 缓存:
IDictionaryEnumerator cacheEnmu = HttpRuntime.Cache.GetEnumerator();
while (cacheEnmu.MoveNext())
{
HttpRuntime.Cache.Remove(cacheEnmu.Key.ToString());
}

(2)页面级缓存 OutputCache

相对 Cache 对象,页面级的 OutputCache 的使用要容易得多,且无需改动页面代码,只要在页面顶部增加 <%@ OutputCache %> 声明就可以缓存当前页的所有内容。当然有利必有弊,OutputCache 只提供有限的几个参数,当需要清除缓存时就不如 Cache 对象那么灵活。不过仍然有办法,.Net 提供了 RemoveOutputCacheItem() 方法来清空页面级输出缓存,使用方法如下:
HttpResponse.RemoveOutputCacheItem(fname);
//fname 是待清除缓存页面的绝对路径,如 /article/read.aspx

(3)用户控件级缓存 OutputCache

用户控件级缓存和页面级缓存类似,不同的是可以只缓存页面局部的用户控件,对于不需要整页缓存的页面来说是种不错的解决方案。可是清空页面缓存的 RemoveOutputCacheItem() 方法对用户控件却无效。 .net 没有提供直接清空用户控件输出缓存的方法。可以采用 OutputCache 依赖项变通处理OutputCache 有一个 VaryByCustom 参数,用于为缓存指定自定义的依赖项,当该项内容变动时就会更新缓存。

要使用 VaryByCustom 参数需要重写 Global.asax 的 GetVaryByCustomString() 方法,简单的代码如下:
//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()中判断的字符串

只需在程序中更新 key 为 ucCacheFlag 的 Cache,页面输出缓存也会相应更新。
可以在 GetVaryByCustomString() 中判断多个关键字,以控制不同的用户控件输出缓存。
---------另一方法--------------
ASP.NET输出缓存的使用网上已经有很多例子了,这里主要介绍下如何在后台管理中移除缓存。
1.基于页面缓存
对于页面:Default.aspx 如果页面顶部添加:
<%@ OutputCache Duration="60" VaryByParam="none" %>
在后台管理中要移除很简单:
System.Web.HttpResponse.RemoveOutputCacheItem(Page.ResolveUrl("Default.aspx"));
2.基于控件
对于控件WebUserControl.ascx 如果在顶部添加了
<%@ OutputCache Duration="60" VaryByParam="none" Shared="true"%>
在后台管理中要实现的话有点麻烦,在博客园的博问请朋友们解答,查尔斯提供了一种解决方法。
实现如下:
(1)添加VaryByCustom项,值为Cashgroupclass。
<%@ OutputCache Duration="60" VaryByParam="none" Shared="true" VaryByCustom="Cashgroupclass" %>
(2) 在Global.asax 中重写 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);
}
(3)在后台管理的移除页面添加如下代码:
Cache objCache = HttpRuntime.Cache;
if (objCache["Cashgroupclass"] != null)
{
objCache.Remove("Cashgroupclass");
}
当然,您也可以借助这个方法实现控件的缓存更新。对了,查尔斯贴的代码中有使用DataCache类,是个自己写的类,可以参考DataCache ,不过里面重载参数对不上。那就加一个吧。
代码
public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
HttpRuntime.Cache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
}
最后,感谢朋友们对我的帮助。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: