關於緩存的設計
1、什麼情況下用緩存
緩存是提高應用程序性能的最好方法之一。運用緩存可以優化數據查詢,避免不必要的網絡數據回傳,和避免執行不必要的完全相同的數據處理邏輯。在實現緩存的時候我們要確定什麼時候裝入緩存數據。用異步裝入緩存或用批處理方式來避免出現客戶端數據延遲。
一般來說在一定時間內請求了相同的業務邏輯而沒有變更的話,可以采用緩存來設計。數據請求頻繁的的請求不適合采用緩存,如論壇的回復,但是論壇的主題是可以采用緩存設計的。
2、緩存設計的步驟
確定緩存數據結構:即設計中哪些數據用到了緩存,設計這些數據的緩存結構
確定緩存什麼數據
確定緩存過期規則和清理
確定如何裝入緩存數據
3、示例 Community Server的緩存類
復制代碼 代碼如下:
using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
namespace Larry.Cache
{
/// <summary>
/// 緩存類 Community Server的緩存類
/// </summary>
public class BaseCache
{
/// <summary>
/// CacheDependency 說明
/// 如果您向 Cache 中添加某個具有依賴項的項,當依賴項更改時,
/// 該項將自動從 Cache 中刪除。例如,假設您向 Cache 中添加某項,
/// 並使其依賴於文件名數組。當該數組中的某個文件更改時,
/// 與該數組關聯的項將從緩存中刪除。
/// [C#]
/// Insert the cache item.
/// CacheDependency dep = new CacheDependency(fileName, dt);
/// cache.Insert("key", "value", dep);
/// </summary>
public static readonly int DayFactor = ;
public static readonly int HourFactor = ;
public static readonly int MinuteFactor = ;
public static readonly double SecondFactor = 0.;
private static readonly System.Web.Caching.Cache _cache;
private static int Factor = ;
/// <summary>
/// 單件模式
/// </summary>
static BaseCache()
{
HttpContext context = HttpContext.Current;
if (context != null)
{
_cache = context.Cache;
}
else
{
_cache = HttpRuntime.Cache;
}
}
/// <summary>
/// 一次性清除所有緩存
/// </summary>
public static void Clear()
{
IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
ArrayList al = new ArrayList();
while (CacheEnum.MoveNext()) //逐個清除
{
al.Add(CacheEnum.Key);
}
foreach (string key in al)
{
_cache.Remove(key);
}
}
public static void RemoveByPattern(string pattern)
{
IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
while (CacheEnum.MoveNext())
{
if (regex.IsMatch(CacheEnum.Key.ToString()))
_cache.Remove(CacheEnum.Key.ToString());
}
}
/// <summary>
/// 清除特定的緩存
/// </summary>
/// <param name="key"></param>
public static void Remove(string key)
{
_cache.Remove(key);
}
/// <summary>
/// 緩存OBJECT.
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
public static void Insert(string key, object obj)
{
Insert(key, obj, null, );
}
/// <summary>
/// 緩存obj 並建立依賴項
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
/// <param name="dep"></param>
public static void Insert(string key, object obj, CacheDependency dep)
{
Insert(key, obj, dep, MinuteFactor * );
}
/// <summary>
/// 按秒緩存對象
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
/// <param name="seconds"></param>
public static void Insert(string key, object obj, int seconds)
{
Insert(key, obj, null, seconds);
}
/// <summary>
/// 按秒緩存對象 並存儲優先級
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
/// <param name="seconds"></param>
/// <param name="priority"></param>
public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
{
Insert(key, obj, null, seconds, priority);
}
/// <summary>
/// 按秒緩存對象 並建立依賴項
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
/// <param name="dep"></param>
/// <param name="seconds"></param>
public static void Insert(string key, object obj, CacheDependency dep, int seconds)
{
Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
}
/// <summary>
/// 按秒緩存對象 並建立具有優先級的依賴項
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
/// <param name="dep"></param>
/// <param name="seconds"></param>
/// <param name="priority"></param>
public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)
{
if (obj != null)
{
_cache.Insert(key, obj, dep, DateTime.Now.AddSeconds(Factor * seconds), TimeSpan.Zero, priority, null);
}
}
public static void MicroInsert(string key, object obj, int secondFactor)
{
if (obj != null)
{
_cache.Insert(key, obj, null, DateTime.Now.AddSeconds(Factor * secondFactor), TimeSpan.Zero);
}
}
/// <summary>
/// 最大時間緩存
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
public static void Max(string key, object obj)
{
Max(key, obj, null);
}
/// <summary>
/// 具有依賴項的最大時間緩存
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
/// <param name="dep"></param>
public static void Max(string key, object obj, CacheDependency dep)
{
if (obj != null)
{
_cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.AboveNormal, null);
}
}
/// <summary>
/// Insert an item into the cache for the Maximum allowed time
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
public static void Permanent(string key, object obj)
{
Permanent(key, obj, null);
}
public static void Permanent(string key, object obj, CacheDependency dep)
{
if (obj != null)
{
_cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);
}
}
public static object Get(string key)
{
return _cache[key];
}
/// <summary>
/// Return int of seconds * SecondFactor
/// </summary>
public static int SecondFactorCalculate(int seconds)
{
// Insert method below takes integer seconds, so we have to round any fractional values
return Convert.ToInt(Math.Round((double)seconds * SecondFactor));
}
}
}
其實這個類就是一個單件模式的設計 和緩存的公共操作方法,其中CacheDependency表示建立緩存依賴項,CacheItemPriority表示緩存的優先級。S使用如下
復制代碼 代碼如下:
public static CardShop.Model.Systems GetConfig()
{
const string cacheKey = "WebConfig";
CardShop.Model.Systems sampleCacheTable = Larry.Cache.BaseCache.Get(cacheKey) as CardShop.Model.Systems;
if (sampleCacheTable == null)
{
OprationCheck.Message("第一次加載使用緩存");
sampleCacheTable = model;
Larry.Cache.BaseCache.Insert(cacheKey, sampleCacheTable, 24 * Larry.Cache.BaseCache.MinuteFactor);
}
else
{
OprationCheck.Message("已經加載了緩存不需要再加載");
}
return sampleCacheTable;
}