ASP.net 缓存总结

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Caching;
using System.Web.Configuration;
using MySql.Data.MySqlClient;
public partial class HumanCatching : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GoodsInfoDataSet goodsSet = new GoodsInfoDataSet();
        GoodsInfoDataSetTableAdapters.TB_GoodsInfoTableAdapter goodsAdpter = new GoodsInfoDataSetTableAdapters.TB_GoodsInfoTableAdapter();
        goodsAdpter.Fill(goodsSet.TB_GoodsInfo);
        DataSet ds = (DataSet)Cache["ds"];
        if (ds == null)
        {
            ds = goodsSet;
            Cache.Insert("ds", ds);

        }
        GridView1.DataSource = ds;
        GridView1.DataBind();
        this.GridView2.DataSource = goodsSet;
        GridView2.DataBind();
        Cache3();
        NotfiyCache();
    }
    private void Cache3()
    {
        DataSet goodsSet3 = (DataSet)Cache["ds3"];
        if (goodsSet3 == null)
        {
            GoodsInfoDataSet set3 = new GoodsInfoDataSet();
            GoodsInfoDataSetTableAdapters.TB_GoodsInfoTableAdapter ad3 = new GoodsInfoDataSetTableAdapters.TB_GoodsInfoTableAdapter();
            ad3.Fill(set3.TB_GoodsInfo);
            CacheDependency cd = new CacheDependency(Server.MapPath("CacheDependFile.txt"));//创建缓存依赖的对象
            //Cache.Insert("ds3", set3, cd); //带有缓存的依赖的写法,当缓存依赖的文件发生改变时,缓存内容被更新!

            //Cache.Insert("ds3", set3,null , DateTime.Now.AddSeconds(15), Cache.NoSlidingExpiration);//这种写法表示 这个缓存最多能维持15秒的时间 更新之后,这个缓存又被维持了15秒,即每个缓存15秒更新一回
            Cache.Insert("ds3", set3, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(15)); //  最后访问 |------------------15s(间隔)------------------|恢复访问。当间隔>15s时,缓存被访问
        }
        this.GridView3.DataSource = goodsSet3;
        this.GridView3.DataBind();
    }


    /// <summary>
    /// //
    /// </summary>
    private void NotfiyCache()
    {

        DataSet goodsSet4 = (DataSet)Cache["ds4"];
        if (goodsSet4 == null)
        {
            GoodsInfoDataSet set4 = new GoodsInfoDataSet();
            GoodsInfoDataSetTableAdapters.TB_GoodsInfoTableAdapter ad3 = new GoodsInfoDataSetTableAdapters.TB_GoodsInfoTableAdapter();
            ad3.Fill(set4.TB_GoodsInfo);
            CacheItemRemovedCallback cacheItemBack1 = new CacheItemRemovedCallback(this.CacheRemovedCallback1);//当清除这个缓存时,将触发的事情
            Cache.Insert("ds4", set4, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, cacheItemBack1);
        }
        this.GridView4.DataSource = goodsSet4;
        GridView4.DataBind();
    }
    private void CacheRemovedCallback1(string k, object b, CacheItemRemovedReason reason)
    {
        this.Label1.Text = "这个G4的缓存被清除";
    }

   
    protected void Button1_Click(object sender, EventArgs e)
    {
        Cache.Remove("ds3");
        Cache.Remove("ds");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Cache.Remove("ds4");
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值