本篇內(nèi)容介紹了“.NET緩存設(shè)計(jì)的使用方法”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
在巴里坤哈薩克等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需設(shè)計(jì),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),全網(wǎng)整合營(yíng)銷推廣,成都外貿(mào)網(wǎng)站制作,巴里坤哈薩克網(wǎng)站建設(shè)費(fèi)用合理。關(guān)于緩存的設(shè)計(jì)
1、什么情況下用緩存
緩存是提高應(yīng)用程序性能的好方法之一。運(yùn)用緩存可以優(yōu)化數(shù)據(jù)查詢,避免不必要的網(wǎng)絡(luò)數(shù)據(jù)回傳,和避免執(zhí)行不必要的完全相同的數(shù)據(jù)處理邏輯。在實(shí)現(xiàn)緩存的時(shí)候我們要確定什么時(shí)候裝入緩存數(shù)據(jù)。用異步裝入緩存或用批處理方式來(lái)避免出現(xiàn)客戶端數(shù)據(jù)延遲。
一般來(lái)說(shuō)在一定時(shí)間內(nèi)請(qǐng)求了相同的業(yè)務(wù)邏輯而沒(méi)有變更的話,可以采用緩存來(lái)設(shè)計(jì)。數(shù)據(jù)請(qǐng)求頻繁的的請(qǐng)求不適合采用緩存,如論壇的回復(fù),但是論壇的主題是可以采用緩存設(shè)計(jì)的。
2、緩存設(shè)計(jì)的步驟
確定緩存數(shù)據(jù)結(jié)構(gòu):即設(shè)計(jì)中哪些數(shù)據(jù)用到了緩存,設(shè)計(jì)這些數(shù)據(jù)的緩存結(jié)構(gòu)
確定緩存什么數(shù)據(jù)
確定緩存過(guò)期規(guī)則和清理
確定如何裝入緩存數(shù)據(jù)
3、示例 Community Server的緩存類
復(fù)制代碼 代碼如下:
using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
namespace Larry.Cache
{
///
/// 緩存類 Community Server的緩存類
///
public class BaseCache
{
///
/// CacheDependency 說(shuō)明
/// 如果您向 Cache 中添加某個(gè)具有依賴項(xiàng)的項(xiàng),當(dāng)依賴項(xiàng)更改時(shí),
/// 該項(xiàng)將自動(dòng)從 Cache 中刪除。例如,假設(shè)您向 Cache 中添加某項(xiàng),
/// 并使其依賴于文件名數(shù)組。當(dāng)該數(shù)組中的某個(gè)文件更改時(shí),
/// 與該數(shù)組關(guān)聯(lián)的項(xiàng)將從緩存中刪除。
/// [C#]
/// Insert the cache item.
/// CacheDependency dep = new CacheDependency(fileName, dt);
/// cache.Insert("key", "value", dep);
///
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 = ;
///
/// 單件模式
///
static BaseCache()
{
HttpContext context = HttpContext.Current;
if (context != null)
{
_cache = context.Cache;
}
else
{
_cache = HttpRuntime.Cache;
}
}
///
/// 一次性清除所有緩存
///
public static void Clear()
{
IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
ArrayList al = new ArrayList();
while (CacheEnum.MoveNext()) //逐個(gè)清除
{
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());
}
}
///
/// 清除特定的緩存
///
///
public static void Remove(string key)
{
_cache.Remove(key);
}
///
/// 緩存OBJECT.
///
///
///
public static void Insert(string key, object obj)
{
Insert(key, obj, null, );
}
///
/// 緩存obj 并建立依賴項(xiàng)
///
///
///
///
public static void Insert(string key, object obj, CacheDependency dep)
{
Insert(key, obj, dep, MinuteFactor * );
}
///
/// 按秒緩存對(duì)象
///
///
///
///
public static void Insert(string key, object obj, int seconds)
{
Insert(key, obj, null, seconds);
}
///
/// 按秒緩存對(duì)象 并存儲(chǔ)優(yōu)先級(jí)
///
///
///
///
///
public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
{
Insert(key, obj, null, seconds, priority);
}
///
/// 按秒緩存對(duì)象 并建立依賴項(xiàng)
///
///
///
///
///
public static void Insert(string key, object obj, CacheDependency dep, int seconds)
{
Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
}
///
/// 按秒緩存對(duì)象 并建立具有優(yōu)先級(jí)的依賴項(xiàng)
///
///
///
///
///
///
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);
}
}
///
/// 較大時(shí)間緩存
///
///
///
public static void Max(string key, object obj)
{
Max(key, obj, null);
}
///
/// 具有依賴項(xiàng)的較大時(shí)間緩存
///
///
///
///
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);
}
}
///
/// Insert an item into the cache for the Maximum allowed time
///
///
///
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];
}
///
/// Return int of seconds * SecondFactor
///
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));
}
}
}
其實(shí)這個(gè)類就是一個(gè)單件模式的設(shè)計(jì) 和緩存的公共操作方法,其中CacheDependency表示建立緩存依賴項(xiàng),CacheItemPriority表示緩存的優(yōu)先級(jí)。S使用如下
復(fù)制代碼 代碼如下:
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("已經(jīng)加載了緩存不需要再加載");
}
return sampleCacheTable;
}
“.NET緩存設(shè)計(jì)的使用方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!