HttpRuntime.Cache的使用經驗

Hellen.Li發表於2013-07-27

配置檔案

<appSettings>  
<add key="EnableCache" value="true"/>  
<add key="CacheDurationSeconds" value="300"/>
</appSettings>

操作方法

 

程式碼
using System;
using System.Web.Configuration;
public class SiteHelper
{
    static public object GetCache(string CacheId)
    {
        object objCache = System.Web.HttpRuntime.Cache.Get(CacheId);
       
// 判斷 Cache 是否啟用
        if (WebConfigurationManager.AppSettings["EnableCache"] ==null
            ||!Convert.ToBoolean(WebConfigurationManager.AppSettings["EnableCache"]))
        {
            objCache =null;
            System.Web.HttpRuntime.Cache.Remove(CacheId);
        }

       
return objCache;
    }

   
///<summary>
    /// 寫入 Cache 資料 ( 預設 60 秒 )
    ///</summary>
    ///<param name="CacheId"></param>
    ///<param name="objCache"></param>
    static public void SetCache(string CacheId, object objCache)
    {
        if (WebConfigurationManager.AppSettings["CacheDurationSeconds"] !=null)
        {
            SetCache(CacheId, objCache, Convert.ToInt32(WebConfigurationManager.AppSettings["CacheDurationSeconds"]));
        }
        else
        {
            SetCache(CacheId, objCache, 60);
        }
    }

   
static public void SetCache(string CacheId, object objCache, int cacheDurationSeconds)
    {
        if (objCache !=null)
        {
            System.Web.HttpRuntime.Cache.Insert(CacheId, objCache, null, System.Web.Caching.Cache.NoAbsoluteExpiration,new TimeSpan(0, 0, cacheDurationSeconds), System.Web.Caching.CacheItemPriority.High, null);
        }
    }
}

使用方法

程式碼
string strCache1 = SiteHelper.GetCache("Cache1") asstring;
if (strCache1 ==null) {     Response.Write("<p>Cache is empty</p>");
    strCache1
="OK";     SiteHelper.SetCache("Cache1", strCache1, 30); }
Response.Write(strCache1);

常見問題#Cache顯示與清空問題

程式碼
List<string> cacheKeys =new List<string>();
IDictionaryEnumerator cacheEnum = Cache.GetEnumerator();
while (cacheEnum.MoveNext())
{
    cacheKeys.Add(cacheEnum.Key.ToString());
}
foreach (string cacheKey in cacheKeys)
{
    Cache.Remove(cacheKey);
}
程式碼
    //清除所有快取
    protected void RemoveAllCache()
    {
        System.Web.Caching.Cache _cache = HttpRuntime.Cache;
        IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
        ArrayList al =new ArrayList();
        while (CacheEnum.MoveNext())
        {
            al.Add(CacheEnum.Key);
        }
        foreach (string key in al)
        {
            _cache.Remove(key);
        }
        show();
    }    
//顯示所有快取
    void show()
    {
        string str ="";
        IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();

       
while (CacheEnum.MoveNext())
        {
            str +="快取名<b>["+ CacheEnum.Key +"]</b><br />";
        }
        this.Label1.Text ="當前網站總快取數:"+ HttpRuntime.Cache.Count +"<br />"+ str;
    }

新增到擴充套件方法

程式碼
using System;
using System.Web.Caching;
using System.Collections;
using System.Collections.Generic;

///<summary>
/// 擴充 System.Web.Caching 名稱空間的 Extension Methods
///</summary>
static public class CacheExtensionMethod
{
    public static void Clear(this Cache x)
    {
        List<string> cacheKeys =new List<string>();
        IDictionaryEnumerator cacheEnum = x.GetEnumerator();
        while (cacheEnum.MoveNext())
        {
            cacheKeys.Add(cacheEnum.Key.ToString());
        }
        foreach (string cacheKey in cacheKeys)
        {
            x.Remove(cacheKey);
        }
    }
}

 

轉自:http://blog.csdn.net/fengloveyun/article/details/5934158

相關文章