网页实现缓存

 C#中禁止cache的方法!

+展开
-C#
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是分布式的高速缓存。

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

1.输出缓存:

要实现页面输出缓存,只要将一条 OutputCache 指令添加到页面即可。

<%@ OutputCache Duration="60" VaryByParam="*" %>

如同其他页面指令一样,该指令应该出现在 ASPX 页面的顶部,即在任何输出之前。它支持五个属性(或参数),其中两个是必需的。

Duration

必需属性。页面应该被缓存的时间,以秒为单位。必须是正整数。

Location

指定应该对输出进行缓存的位置。如果要指定该参数,则必须是下列选项之一:Any、Client、Downstream、None、Server 或 ServerAndClient。

VaryByParam

必需属性。Request 中变量的名称,这些变量名应该产生单独的缓存条目。“none” 表示没有变动。“*” 可用于为每个不同的变量数组创建新的缓存条目。变量之间用 “;” 进行分隔。

VaryByHeader

基于指定的标头中的变动改变缓存条目。

VaryByCustom

允许在 global.asax 中指定自定义变动(例如,“Browser”)。

2.片段缓存:

示例

<%@ OutputCache Duration="60" VaryByParam="*" %> 该示例将缓存用户控件60秒,并且将针对查询字符串的每个变动、针对此控件所在的每个页面创建单独的缓存条目。<%@ OutputCache Duration="60" VaryByParam="none"
VaryByControl="CategoryDropDownList" %> 该示例将缓存用户控件60秒,并且将针对CategoryDropDownList控件的每个不同的值、针对此控件所在的每个页面创建单独的缓存条目。<%@ OutputCache Duration="60" VaryByParam="none" VaryByCustom="browser"
Shared="true" %>



最后,该示例将缓存用户控件60秒,并且将针对每个浏览器名称和主要版本创建一个缓存条目。然后,每个浏览器的缓存条目将由引用此用户控件的所有页面共享(只要所有页面都用相同的ID引用该控件即可)。

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

System.Web.Caching.cahe被httpRuntime.Cache或HttpContext.Current.Cache实例化。httpRuntime.Cache,HttpContext.Current.Cache是内置对象就想当与seesion。

HttpContext.Current.Cache与HttpRuntime.Cache区别:

HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象。 HttpRuntime.Cache:获取当前应用程序的Cache。HttpContext.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失效。

可到网上查询如何使用

类别:Asp.Net/C#/WCF 作者:转载 日期:2009-07-06 【评论:0 阅读: 182】 繁體中文

public string GetNBBarData()
    {
        string strContent = "";
        if (HttpContext.Current.Cache["NBBar"] != null)

           {
              strContent = (string)HttpContext.Current.Cache["NBBar"];
          }
        else
        {
            string StrUrl = "http://www.126.com";

            System.Net.WebClient wc = new System.Net.WebClient();
            byte[] buffer = wc.DownloadData(new Uri(StrUrl));
            strContent = Encoding.UTF8.GetString(buffer);     // 获取网页内容
            HttpContext.Current.Cache.Insert("NBBar", strContent, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero);
        }
        return strContent;
    }


posted on 2011-03-25 17:03  陈国利  阅读(1324)  评论(0编辑  收藏  举报