您好,登錄后才能下訂單哦!
本篇內容介紹了“.NET緩存設計的使用方法”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
關于緩存的設計
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;
}
“.NET緩存設計的使用方法”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。