asp.net 缓存 2

html禁用客户端缓存

<META HTTP-EQUIV="pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1978 08:21:57 GMT">

 

C#中禁止cache的方法!
Response.Buffer = true;
Response.ExpiresAbsolute=System.DateTime.Now.AddSeconds(-1);
Response.Expires=0;
Response.CacheControl="no-cache";

 

 

 

服务端缓存有System.Web.Caching.cahe和memcached

当然System.Web.Caching.cahe是微软写的类,而memcached是第三方插件。System.Web.Caching.cache目前还不是分布式缓存,只能在一台电脑上(07时候),而

memcached是分布式的高速缓存。

服务器缓存可分为三种缓存

 

 

 

编程用得最多的缓存:数据缓存

System.Web.Caching.cahehttpRuntime.CacheHttpContext.Current.Cache实例化。httpRuntime.CacheHttpContext.Current.Cache是内置对象就想当与seesion

 

HttpContext.Current.CacheHttpRuntime.Cache区别:

HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象。 HttpRuntime.Cache:获取当前应用程序的CacheHttpContext.Current.Cache是调用了 HttpRuntime.Cache,且HttpContext.Current.Cache是用在web程序上,而HttpRuntime.Cache是用在任何程序上。System.web命名空间下。

其实HttpContext.Current.Cache是通过 HttpRuntime.Cache来实现的,所以一般实例化最好通过HttpRuntime.Cache来实例化:

例如:System.Web.Caching.Cache cache = HttpRuntime.Cache

 

System.Web.Caching.Cache有很多方法,但方法中add中是存缓存

参数中有缓存时间,依赖项。

缓存时间即到当缓存的东西达到指定时间就让缓存失效,而依赖项是当依赖项发生变化就会使缓存失效。

依赖项有一般依赖项 CacheDependency和数据库依赖项SqlCacheDependency

虽然CacheDependency类完成了很重要的功能,但其组成结构却比较简单,主要有两个属性和一个方法。
属性“HasChanged”:判断CacheDependency对象是否已更改。
属性“UtcLastModified”:返回上次依赖项的修改日期
方法“Dispose”:释放CacheDependency对象所占有的资源。因为缓存类继承了接口“IDispose”,所以必须实现此方法。

CacheDependency

例如 Cache.Insert("key", myXMLFileData, DateTime.Now.AddMinutes(1),,new
System.Web.Caching.CacheDependency(Server.MapPath("users.xml")));

 users.xml文件就相当一般依赖项,当xml文件被改了,则此缓存失效。

SqlCacheDependency一般是增对数据库的,这个设置需要在config里设置,而且还要启动数据库的这样服务。

Cache.Insert 中可设置跟数据库中那个表关联,一旦表有变化就会导致cache失效。

 

 前段时间遇到过这个问题,每次点击按钮会showModaldialog()一个窗口出来,如果窗口的页面代码上不加上“禁止缓存的话”经常会将上一次的缓存页面加载进来。在页面上加上下面三句就OK了:
<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="Cache-Control" content="no-cache,must-revalidate"/>
<meta http-equiv="expires" content="Wed,26 Feb 1997 08:21:57 GMT"/>

 

 

posted @ 2009-06-30 16:24  月光小提琴  阅读(416)  评论(0编辑  收藏  举报