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");
    }
}

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页