C# 基礎數據緩存


最近做一個C#項目,剛做完基礎數據,現把緩存機制給大家分享一下:

做過基礎數據的同學應該都知道,每次涉及到查詢數據時都會去數據庫把配置好的基礎數據查詢出來,這樣每次在操作是會很慢。那么我們每次點開一個新頁面都會去數據庫查詢一下基礎數據,這樣會頻繁的連接數據庫,用戶多了之后肯定就會特別慢。因為數據一旦配好之后就會很少去改動它,所以我們可以做一個緩存把每次查詢出來的基礎數據緩存起來,這樣就可以提高查詢效率。好了,廢話不多說了,上代碼。

我聲明一下我用的是Dictionary數據字典,您也可以使用Cache等等別的技術,實現機制都一樣。

     我使用了一個接口,一個抽象類,一個工廠類,一個普通類。

首先我們得定義設置緩存,獲取緩存,刪除緩存的方法,還要定義緩存的大小,太大了就會不好控制,消耗內存。在接口中定義方法,然后用抽象類繼承接口實現方法,因為緩存是一直保存在整個網站運行期間,所以不能實例化它,我們得用abstract類,用工廠模式實現調用。 需要注意一點就是我們可以定義Dictionary里面的類型為泛型類而不用object類型,這樣在使用的時候就不用裝箱,拆箱。

 以下是接口ICache代碼:

/// <summary> 
/// 設置緩存 
/// </summary> 
/// <param name="key"></param> 
/// <param name="value"></param> 
void SetCache(TKey key, TValue value);

/// <summary> 
/// 獲取緩存 
/// </summary> 
/// <param name="key"></param> 
/// <returns></returns> 
TValue GetCache(TKey key);


/// <summary> 
/// 清空指定緩存 
/// </summary> 
void Clear(TKey key);

/// <summary> 
/// 清空當前登錄用戶的緩存 
/// </summary> 
/// <param name="key"></param> 
void ClearAll();

/// <summary> 
/// 定義緩存Key 
/// </summary> 
/// <param name="key"></param> 
/// <returns></returns> 
TKey JoinKey(TKey key);

/// <summary> 
/// 緩存大小 
/// </summary> 
int CacheSize { get; set; }

接着,我們用一個抽象類(AbstractCache)繼承接口,這里面的大部分都是虛方法,因為我們在使用的時候考慮到還有別的緩存,所以可以在繼承類里面重寫這些方法。

public AbstractCache(string type) 

this._type = type; 
if (AbstractCache<TKey, TValue>._cacheList == null) 

AbstractCache<TKey, TValue>._cacheList = new Dictionary<string, Dictionary<TKey, TValue>>(); 

if (!AbstractCache<TKey, TValue>._cacheList.ContainsKey(_type)) 

AbstractCache<TKey, TValue>._cacheList.Add(this._type, new Dictionary<TKey, TValue>()); 


private static Dictionary<string, Dictionary<TKey, TValue>> _cacheList; 
private string _type; 
private int _cacheSize = 100;

public virtual void SetCache(TKey key, TValue value) 

Dictionary<TKey, TValue> dic = this.getTypeCache(); 
lock (AbstractCache<TKey, TValue>._cacheList) 

TKey fullKey = this.JoinKey(key); 
if (!dic.ContainsKey(fullKey)) 

if (dic.Count >= this._cacheSize) 

if (dic.Keys.Count > 0) 

TKey tmpKey = default(TKey); 
foreach (TKey k in dic.Keys) 

tmpKey = k; 
break; 

dic.Remove(tmpKey); 


if (value != null) 

dic.Add(fullKey, value); 

else 

dic.Add(fullKey, default(TValue)); 


else 

if (value != null) 

dic[fullKey] = value; 

else 

dic[fullKey] = default(TValue); 



}


public virtual TValue GetCache(TKey key) 

Dictionary<TKey, TValue> dic = this.getTypeCache(); 
TKey fullKey = this.JoinKey(key); 
if (dic.ContainsKey(fullKey)) 

return dic[fullKey]; 

return default(TValue); 
}

public virtual void Clear(TKey key) 

Dictionary<TKey, TValue> dic = this.getTypeCache(); 
lock (AbstractCache<TKey, TValue>._cacheList) 

TKey fullKey = this.JoinKey(key); 
if (dic.ContainsKey(fullKey)) 

dic.Remove(fullKey); 



public virtual void ClearAll() 

throw new NotImplementedException(); 
}

public abstract TKey JoinKey(TKey key);


public int CacheSize 

get 

return _cacheSize; 

set 

_cacheSize = value; 

}

/// <summary> 
/// 獲取當前類型的緩存數據 
/// </summary> 
/// <returns></returns> 
protected Dictionary<TKey, TValue> getTypeCache() 

Dictionary<TKey, TValue> dic = AbstractCache<TKey, TValue>._cacheList[this._type]; 
if (dic == null) 

throw new Exception("不正確的初始化方式"); 

return dic; 
}

接下來我們需要頂一個DataAuthCache類來繼承抽象類重寫一些自己需要的方法,

public DataAuthCache() 
: base("DataAuthCache")  //這個參數是區分緩存那一部分類容的 


private static object _lockObj = new object(); 
public override string JoinKey(string entityName) 

return Entity.Session.Manage.Current.AuthContext.OrgID + "_" + Entity.Session.Manage.Current.AuthContext.UserID + "_" + entityName; 

public override void ClearAll() 

long orgKey = Entity.Session.Manage.Current.AuthContext.OrgID; 
long userKey = Entity.Session.Manage.Current.AuthContext.UserID; 
lock (_lockObj) 

Dictionary<string, string> dic = this.getTypeCache(); 
string containKey = orgKey + "_" + userKey; 
List<string> keys = new List<string>(); 
//Dictionary里面Keys不能用索引直接訪問 
foreach (string key in dic.Keys) 

keys.Add(key);


if (keys.Count > 0) 

foreach (string key in keys) 

if (key.IndexOf(containKey) > 0) 

dic.Remove(key); 




}

然后,我們可以定義一個工廠類(AbstractCacheFactory)來實現外部的調用

private static ICache<string, string> _dataAuthCache = null; 
public static AbstractCache<string, string> GetDataAuthCache() 

if (_dataAuthCache == null) 

_dataAuthCache = new DataAuthCache(); 

return (AbstractCache<string, string>)_dataAuthCache; 
}

好了,到這,我們就完成緩存了,很簡單吧!

AbstractCache<string, string> dac =AbstractCacheFactory.GetDataAuthCache(); 
string cacheHql = dac.GetCache(tpl.EntityName);

cacheHql就是我們緩存的數據查詢片段,好了,就分享到這兒了。歡迎大家提出寶貴意見,和分享搞好的方法。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM