麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

ASP.NET緩存Cache之數據緩存

2019-11-14 16:49:50
字體:
來源:轉載
供稿:網友

添加 Cache[Key]=object  or Cache.Insert

移除 Cache.Remove(key)

1、將值直接寫入Cache

 代碼如下復制代碼

HttpContext.Current.Cache["One"] = "1";

使用'絕對過期'方式處理緩存,過期時間為:9999年12月31日 (不推薦使用該方法處理緩存,并且應在適當的時候清空緩存Key)


2、使用Insert(String, Object)插入Cache

 代碼如下復制代碼

string cacheKey = "Two";
object cacheValue = HttpContext.Current.Cache.Get(cacheKey);

if(cacheValue == null)
{
    cacheValue = WebConfigurationManager.ConnectionStrings["applicationServices"].ConnectionString;

    HttpContext.Current.Cache.Insert(cacheKey, cacheValue);
}

//顯示指定緩存的Key 與 Value
this.ShowMessage(cacheKey, cacheValue.ToString());

3、使用Insert(String, Object, CacheDependency, DateTime, TimeSpan)插入Cache

 代碼如下復制代碼

string cacheKey = "__cache__students";

DataSet dataSet = this.Cache.Get(cacheKey) as DataSet;

if(dataSet == null)
{
    dataSet = new DataSet();

    //加載xml并填充至DataSet
    dataSet.ReadXml(this.Server.MapPath(@"XMLFile.xml"));

    //加入緩存,并設定'絕對過期時間'為5分鐘
    this.Cache.Insert(cacheKey, dataSet, null, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);
}

//綁定DataGrid數據
grdDefault.DataSource = dataSet;
grdDefault.DataBind();

該方法較重要的兩個參數為absoluteExpiration及slidingExpiration
absoluteExpiration  DateTime類型,代表絕對過期時間
slidingExpiration  TimeSpan類型,代表滑動過期時間
absoluteExpiration與slidingExpiration不能同時使用
例如:設定了absoluteExpiration參數時,slidingExpiration必須設定為System.Web.Caching.Cache.NoSlidingExpiration
反之:設定了slidingExpiration參數時,http://www.111cn.net/net/net/56762.htm absoluteExpiration必須設定為System.Web.Caching.Cache.NoAbsoluteExpiration

 4、使用Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPRiority,

 代碼如下復制代碼

CacheItemRemovedCallback)插入Cache

public partial class PriorityAndCallbackDemo : System.Web.UI.Page
{
    #region 靜態字段
    static bool CacheItemRemoved = false;
    static CacheItemRemovedReason Reason;
    static string CacheItemKey = "fw__cache__students";
    #endregion

    #region 事件處理
    //頁面加載
    protected void Page_Load(object sender, EventArgs e)
    {
        //緩存項已移除
        if(PriorityAndCallbackDemo.CacheItemRemoved)
        {
            ltMessage.Text = string.Format("Key={0}已從緩存移出,原因為:{1}", PriorityAndCallbackDemo.CacheItemKey, PriorityAndCallbackDemo.Reason.ToString());
        }
    }

    //'添加緩存'按鈕 點擊事件 處理
    protected void btnAddCache_Click(object sender, EventArgs e)
    {
        DataSet dataSet = this.Cache.Get(PriorityAndCallbackDemo.CacheItemKey) as DataSet;

        if(dataSet == null)
        {
            dataSet = new DataSet();
            dataSet.ReadXml(this.Server.MapPath(@"XMLFile.xml"));

            //使用 Web.config 作為緩存過期依賴項
            CacheDependency dependency = new CacheDependency(this.Server.MapPath(@"Web.config"), DateTime.Now);

            //加入緩存,設定優先級為默認級別
            this.Cache.Insert(PriorityAndCallbackDemo.CacheItemKey, dataSet, dependency, DateTime.Now.AddMinutes(1), System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback(this.CacheItemRemovedHandler));
        }

        //綁定GridView數據
        grdDefault.DataSource = dataSet;
        grdDefault.DataBind();
    }

    //'移除緩存'按鈕 點擊事件 處理
    protected void btnRemoveCache_Click(object sender, EventArgs e)
    {
        if(this.Cache[PriorityAndCallbackDemo.CacheItemKey] != null)
        {
            this.Cache.Remove(PriorityAndCallbackDemo.CacheItemKey);
        }
    }
    #endregion

    #region 私有方法
    //緩存項移除事件處理
    private void CacheItemRemovedHandler(string key, object value, CacheItemRemovedReason relason)
    {
        PriorityAndCallbackDemo.CacheItemRemoved = true;
        PriorityAndCallbackDemo.Reason = relason;
    }
    #endregion
}

該方法較重要的兩個參數為CacheItemPriority及CacheItemRemovedCallback
CacheItemPriority  緩存項優先級,當服務器內存不夠時,優先級越高的項越不容易被移除
CacheItemRemovedCallback  該參數為委托類型,當緩存項被移除時所調用,包含Reason參數用于表示緩存項被移除的原因

【我是怎么用的】

首先理解緩存策略。可調過期策略 和 絕對過期策略。注意,兩則不能同時使用
 

使用可調過期策略,需要將absoluteExpiration=DateTime.MaxValue ,TimeSpan .FromMinutes(10)設置項目只有在10分鐘內不被使用才會被移除

 代碼如下復制代碼

Cache.Insert("data", "123", null , DateTime.MaxValue, TimeSpan.FromMinutes(10));

絕對策略,如天氣報告,將信息保存60分鐘

 代碼如下復制代碼

Cache.Insert("data", "123", null , DateTime.Now.AddMinutes(60), TimeSpan.Zero);

緩存依賴。

即一個緩存的失效依賴另外一個object。這里的object可以指另外一個緩存,或者一個文件,或者....


類:CacheDependency 命名空間 System.Web.Caching.CacheDependency依賴于其它緩存項目

 代碼如下復制代碼

System.Web.Caching.CacheDependency cacheDependency = new System.Web.Caching.CacheDependency (null, new string [] { "time" });
Cache.Insert( "number", ++num, cacheDependency);
 
依賴于文件或文件夾
 
System.Web.Caching. CacheDependency cacheDependency = new System.Web.Caching.CacheDependency ( "test.xml");
當test.xml文件刪除、更新時自動從緩存中移除
 
System.Web.Caching.CacheDependency cacheDependency = new System.Web.Caching.CacheDependency(null, new string[] { "time" });
Cache.Insert("test", "123", cacheDependency);

 
移除項目回調
 Cache.Insert("test", "123", null , DateTime.Now.AddSeconds(10), TimeSpan.Zero, new CacheItemUpdateCallback(Test));
 
 
 private void Test(string key, CacheItemUpdateReason reason, out object expensiveObject, out CacheDependency dependency, out DateTime absoluteExpiration, out TimeSpan slidingExpiration)
    {
 
    }
 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 成人福利在线免费观看 | 久久小视频 | 国内久久久久 | 美女污污在线观看 | 免费一区二区三区 | 久久思思爱 | 国产精品免费观看视频 | 免费观看视频在线 | 九九热精品在线视频 | 亚洲午夜久久久精品一区二区三区 | 成人国产精品一区 | 深夜福利久久久 | 久久久精品综合 | 成人免费乱码大片a毛片视频网站 | 久久91精品国产91久久yfo | 国产一级毛片网站 | 欧美成人一区二区三区电影 | 精品中文字幕久久久久四十五十骆 | 国产91丝袜在线播放 | 777午夜精品视频在线播放 | 精品久久久久久久久中文字幕 | 91在线看黄 | 黑人日比视频 | 久艹在线视频 | 成人国产在线看 | 欧美成人性生活片 | 中文字幕极速在线观看 | 最近中文字幕一区二区 | 久久福利国产 | 久久精品亚洲精品国产欧美kt∨ | 欧日一级片 | 国产精品hd免费观看 | 国产精品久久久久久久久久了 | 久色亚洲| 黄色网址在线播放 | 成人 精品 | av电影院在线观看 | av免费在线观看免费 | 久久亚洲成人 | 亚洲无av | 久久国产精品电影 |