outputcache Dependencies

本文介绍了ASP.NET中两种实现缓存的方法,一种是通过代码设置输出缓存(output cache)依赖于数据缓存(data cache),另一种是在页面声明中配置输出缓存,并结合后台代码实现数据缓存。此外,还说明了如何清除指定的缓存项。

 第一个写法:

//假如cache["ProcessIntensiveReport"]=null,outputcache会没有效果的。
        //所以一定要设cache["ProcessIntensiveReport"]的值
        Response.AddCacheItemDependency("ProcessIntensiveReport");
       

        // Set additional properties to enable caching.
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
        Response.Cache.SetCacheability(HttpCacheability.Public);
        Response.Cache.SetValidUntilExpires(true);

        Response.Write(System.DateTime.Now.ToString()); 

  Cache.Insert("ProcessIntensiveReport", "canue", null, System.DateTime.Now.AddMinutes(60), System.Web.Caching.Cache.NoSlidingExpiration);

//好多时候我们想同时清空outputcache和datacache,可以用上面的方法。

使用outputcache Dependencies datacache那个object。当清空datacache时同时清空outputcache.

//第二个写法:

在页面中加入:

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

后台代码是:

 protected void Page_Load(object sender, EventArgs e)
    {
        Cache.Insert("ProcessIntensiveReport", "canue", null, System.DateTime.Now.AddMinutes(60), System.Web.Caching.Cache.NoSlidingExpiration);
        Response.AddCacheItemDependency("ProcessIntensiveReport");
        Response.Write(System.DateTime.Now.ToString());
    }

第一次运行了。它就会被cache了。实现了outputcache Dependencies,它的key为ProcessIntensiveReport,

如果你想清空cache的话。你需要在另一个页面实现清除cache的行为:

另一个页面的代码:

    protected void Button2_Click(object sender, EventArgs e)
    {
        Cache.Remove("ProcessIntensiveReport");
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

田猿笔记

写文章不容易,希望大家小小打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值