国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

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

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)
    {
 
    }
 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 隆德县| 榕江县| 仁布县| 华安县| 宜春市| 南京市| 运城市| 新龙县| 蒙阴县| 竹北市| 大渡口区| 山阴县| 阳东县| 聂荣县| 东港市| 焦作市| 黑山县| 乡宁县| 六枝特区| 鹤庆县| 霞浦县| 姚安县| 昆山市| 津市市| 庄浪县| 合江县| 耒阳市| 天柱县| 泗阳县| 衡南县| 长泰县| 阜南县| 金乡县| 定远县| 台南县| 连平县| 和顺县| 桦南县| 方城县| 天镇县| 米林县|