緩存(cun)篇(pian)~第七回 Redis實現基于方法簽名(ming)的數據集緩存(cun)(可控更新(xin),分(fen)布(bu)式數據緩存(cun))
本篇文章可以說是第六回(hui) Microsoft.Practices.EnterpriseLibrary.Caching實現(xian)基于方法簽名的數據集緩(huan)存(可控更新,WEB端(duan)數據緩(huan)存)的續篇(pian),事實(shi)(shi)上,有EnterpriseLibrary.Caching也只(zhi)是實(shi)(shi)現緩存持久化的一種方式,而(er)Redis做為成熟(shu)的分布(bu)式存儲中(zhong)間件來說,實(shi)(shi)現這(zhe)個數(shu)據集緩存功(gong)能(neng)顯(xian)得(de)更加(jia)得(de)心應手(shou),也更加(jia)滿足(zu)大型(xing)網站的設計規(gui)則。(在多web服務器時(shi)(web端實(shi)(shi)現負載(zai)均衡,反(fan)向代理),EnterpriseLibrary.Caching顯(xian)得(de)沒什么作(zuo)為,而(er)這(zhe)時(shi),分布(bu)式緩存就可以一顯(xian)身手(shou)了,它可以很輕(qing)松(song)的將(jiang)緩存服務器部(bu)署(shu)到第三方服務器上,解決了上面的問題)
一個標準(zhun),多(duo)種實現,面(mian)向對象的真諦(di):多(duo)態性(xing),如果你問(wen)我接口有什么用,那(nei)么本篇文(wen)章可以告訴你答(da)案:根據(ju)不同(tong)的場合(he),使用不同(tong)的持(chi)久化方式(shi)去存儲數據(ju)。
下面是緩存標準接口ICacheProvider
/// <summary> /// 表示實(shi)現該接口(kou)的類(lei)型(xing)是能夠為應用程序提供(gong)緩(huan)存機制(zhi)的類(lei)型(xing)。 /// 這可以有多(duo)種實(shi)現機(ji)制 /// </summary> public interface ICacheProvider { #region Methods /// <summary> /// 向(xiang)緩(huan)存中添(tian)加一個對(dui)象。 /// </summary> /// <param name="key">緩(huan)存的(de)鍵值(zhi),該(gai)值(zhi)通常是使(shi)用緩(huan)存機制的(de)方法的(de)名(ming)稱(cheng)。</param> /// <param name="valKey">緩(huan)存值(zhi)的(de)鍵值(zhi),該值(zhi)通常是由(you)使用緩(huan)存機制的(de)方(fang)法的(de)參(can)數值(zhi)所產(chan)生(sheng)。</param> /// <param name="value">需要(yao)緩存(cun)的對(dui)象。</param> void Add(string key, string valKey, object value); /// <summary> /// 向(xiang)緩存中更(geng)新(xin)一個(ge)對象。 /// </summary> /// <param name="key">緩(huan)存(cun)的鍵值(zhi),該值(zhi)通常是使用緩(huan)存(cun)機制(zhi)的方法的名(ming)稱。</param> /// <param name="valKey">緩(huan)存值的鍵值,該值通常(chang)是由(you)使(shi)用緩(huan)存機制的方法的參(can)數(shu)值所產生。</param> /// <param name="value">需要緩存的(de)對象。</param> void Put(string key, string valKey, object value); /// <summary> /// 從緩存中(zhong)讀取(qu)對(dui)象。 /// </summary> /// <param name="key">緩存的鍵值(zhi),該(gai)值(zhi)通常是使(shi)用緩存機制的方法(fa)的名(ming)稱。</param> /// <param name="valKey">緩存值的鍵值,該值通常是由使用緩存機制的方(fang)法的參數值所產生(sheng)。</param> /// <returns>被緩(huan)存的對象。</returns> object Get(string key, string valKey); /// <summary> /// 從緩存中移除對象。 /// </summary> /// <param name="key">緩(huan)(huan)存(cun)的(de)(de)鍵值(zhi),該值(zhi)通常(chang)是使用(yong)緩(huan)(huan)存(cun)機(ji)制的(de)(de)方法的(de)(de)名稱(cheng)。</param> void Remove(string key); /// <summary> /// 獲取一個<see cref="Boolean"/>值,該值表示擁有指定鍵值的緩(huan)存(cun)是否(fou)存(cun)在。 /// </summary> /// <param name="key">指定的鍵值。</param> /// <returns>如果(guo)緩存(cun)存(cun)在,則返回(hui)true,否(fou)則返回(hui)false。</returns> bool Exists(string key); /// <summary> /// 獲取(qu)一個<see cref="Boolean"/>值(zhi)(zhi),該(gai)值(zhi)(zhi)表示(shi)擁有指(zhi)定鍵值(zhi)(zhi)和緩(huan)存(cun)值(zhi)(zhi)鍵的緩(huan)存(cun)是否存(cun)在。 /// </summary> /// <param name="key">指定的(de)鍵值。</param> /// <param name="valKey">緩存值鍵(jian)。</param> /// <returns>如(ru)果緩存存在,則(ze)返(fan)回true,否則(ze)返(fan)回false。</returns> bool Exists(string key, string valKey); #endregion }
而這次我們使用Redis來作為實(shi)現持久化的方式(shi),看一個RedisCacheProvider代(dai)碼,它(ta)為了兼(jian)容性(xing),將(jiang)Dictionary<string, object>類型改為了Dictionary<string, byte[]>類型,這種設計避(bi)免了很多錯誤(wu),因為我們知(zhi)道,數(shu)據在發送(song)時,它(ta)會被序列化,而兼(jian)容性(xing),
安(an)全(quan)性(xing),性(xing)能(neng)等(deng)最佳的方(fang)式就(jiu)是(shi)二進制的方(fang)式,所(suo)以,我們使用它來(lai)對數據進行存(cun)儲。
/// <summary> ///使(shi)用redis方(fang)式進行緩存持(chi)久(jiu)化 /// </summary> internal class RedisCacheProvider : ICacheProvider, IDisposable { private readonly IRedisClient _cacheManager = Redis.Client.RedisManager.GetClient(); static byte[] Serialize(object data) { BinaryFormatter formatter = new BinaryFormatter(); MemoryStream rems = new MemoryStream(); formatter.Serialize(rems, data); return rems.GetBuffer(); } static object Deserialize(byte[] data) { BinaryFormatter formatter = new BinaryFormatter(); MemoryStream rems = new MemoryStream(data); data = null; return formatter.Deserialize(rems); } public void Add(string key, string valKey, object value) { byte[] byteValue = Serialize(value); using (var tbl = _cacheManager.GetTypedClient<Dictionary<string, byte[]>>()) { Dictionary<string, byte[]> dict = null; if (tbl.ContainsKey(key)) { dict = (Dictionary<string, byte[]>)tbl.Lists[key][0]; dict[valKey] = byteValue; } else { dict = new Dictionary<string, byte[]>(); dict.Add(valKey, byteValue); } Remove(key); tbl.Lists[key].Add(dict); } } public void Put(string key, string valKey, object value) { Add(key, valKey, value); } public object Get(string key, string valKey) { using (var tbl = _cacheManager.GetTypedClient<Dictionary<string, byte[]>>()) { if (tbl.ContainsKey(key)) { Dictionary<string, byte[]> dict = (Dictionary<string, byte[]>)tbl.Lists[key][0]; if (dict != null && dict.ContainsKey(valKey)) return Deserialize(dict[valKey]); else return null; } } return null; } public void Remove(string key) { using (var tbl = _cacheManager.GetTypedClient<Dictionary<string, byte[]>>()) { tbl.Lists[key].RemoveAll(); } } public bool Exists(string key) { using (var tbl = _cacheManager.GetTypedClient<Dictionary<string, byte[]>>()) { return tbl.ContainsKey(key); } } public bool Exists(string key, string valKey) { using (var tbl = _cacheManager.GetTypedClient<Dictionary<string, byte[]>>()) { return tbl.ContainsKey(key) && ((System.Collections.Generic.Dictionary<string, byte[]>)tbl.Lists[key][0]).ContainsKey(valKey); } } public void Dispose() { _cacheManager.Dispose(); } }
事實(shi)上,寫(xie)到(dao)這里,我(wo)們(men)的redis方法簽名存儲(chu)就完(wan)成了,配合上一(yi)篇文章,你可以設(she)計出自己(ji)的緩(huan)存系統(tong)了,在這里再多(duo)說一(yi)句,本緩(huan)存系統(tong)用到(dao)的設(she)計模(mo)式類似于策(ce)略模(mo)式,它對于一(yi)個完(wan)善功能,可以多(duo)種實(shi)現的策(ce)略,而對外只公開一(yi)個標準的對象,其它具體
的,完整功能的實現都使用了internal作為修(xiu)飾符,來(lai)對外界(jie)隱藏。