本章將和大家分享如何使用資料分拆+lock鎖的方式來實作本地快取,
系統性能優化的第一步,就是使用快取,快取包括:客戶端快取---CDN快取---反向代理快取---本地快取,

下面我們直接通過代碼來看下本地快取的基本原理:
using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace MyCache { /// <summary> /// 第三方資料存盤和獲取的地方 /// /// 過期策略: /// 永久有效 /// 絕對過期---有個時間點,超過就過期 /// 滑動過期---多久之后過期,如果期間更新/查詢/檢查存在,就再次延長 /// /// 主動清理+被動清理,保證過期資料不會被查詢;過期資料也不會滯留太久 /// /// 多執行緒操作非執行緒安全的容器,會造成沖突,那有什么解決方案呢? /// 1、使用執行緒安全容器ConcurrentDictionary /// 2、用lock---Add/Remove/遍歷 解決問題了,但是性能怎么辦呢? /// 怎么降低影響,提升性能呢? --- 資料分拆,多個資料容器,多個鎖,容器之間可以并發, /// </summary> public class CustomCache { #region 欄位和屬性 /// <summary> /// 模擬獲取系統的CPU數 /// </summary> private static int _cpuNumer = 0; /// <summary> /// 動態初始化多個容器 /// </summary> private static List<Dictionary<string, object>> _dictionaryList = new List<Dictionary<string, object>>(); /// <summary> /// 動態初始化多個鎖 /// </summary> private static List<object> _lockList = new List<object>(); #endregion 欄位和屬性 #region 靜態建構式 /// <summary> /// 靜態建構式 /// </summary> static CustomCache() { _cpuNumer = 4; for (int i = 0; i < _cpuNumer; i++) { _dictionaryList.Add(new Dictionary<string, object>()); _lockList.Add(new object()); } //主動清理快取 Task.Run(() => { while (true) { Thread.Sleep(1000 * 60 * 10); try { for (int i = 0; i < _cpuNumer; i++) { List<string> keyList = new List<string>(); lock (_lockList[i]) //資料分拆,減少鎖的影響范圍 { foreach (var key in _dictionaryList[i].Keys) { DataModel model = (DataModel)_dictionaryList[i][key]; if (model.ObsloteType != ObsloteType.Never && model.DeadLine < DateTime.Now) { keyList.Add(key); } } keyList.ForEach(s => _dictionaryList[i].Remove(s)); } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); continue; } } }); } #endregion 靜態建構式 /// <summary> /// 獲取容器索引 /// </summary> /// <param name="key">快取鍵</param> /// <returns>索引值</returns> public static int GetHashCodeIndex(string key) { int hash = Math.Abs(key.GetHashCode()); //相對均勻而且穩定 return hash % _cpuNumer; } /// <summary> /// 添加 /// </summary> /// <param name="key">快取鍵</param> /// <param name="oValue">快取值</param> public static void Add(string key, object oValue) { int index = GetHashCodeIndex(key); lock (_lockList[index]) { _dictionaryList[index].Add(key, new DataModel() { Value = oValue, ObsloteType = ObsloteType.Never }); } } /// <summary> /// 絕對過期 /// </summary> /// <param name="key">快取鍵</param> /// <param name="oVaule">快取值</param> /// <param name="timeOutSecond">過期時間</param> public static void Add(string key, object oVaule, int timeOutSecond) { int index = GetHashCodeIndex(key); lock (_lockList[index]) { _dictionaryList[index].Add(key, new DataModel() { Value = oVaule, ObsloteType = ObsloteType.Absolutely, DeadLine = DateTime.Now.AddSeconds(timeOutSecond) }); } } /// <summary> /// 相對過期 /// </summary> /// <param name="key">快取鍵</param> /// <param name="oVaule">快取值</param> /// <param name="duration">過期時間</param> public static void Add(string key, object oVaule, TimeSpan duration) { int index = GetHashCodeIndex(key); lock (_lockList[index]) { _dictionaryList[index].Add(key, new DataModel() { Value = oVaule, ObsloteType = ObsloteType.Relative, DeadLine = DateTime.Now.Add(duration), Duration = duration }); } } /// <summary> /// 獲取資料(要求在Get前做Exists檢測) /// </summary> /// <typeparam name="T">型別</typeparam> /// <param name="key">快取鍵</param> /// <returns>快取值</returns> public static T Get<T>(string key) { int index = GetHashCodeIndex(key); return (T)((DataModel)_dictionaryList[index][key]).Value; } /// <summary> /// 判斷快取是否存在(被動清理,請求了資料,才能清理) /// </summary> /// <param name="key">快取鍵</param> /// <returns></returns> public static bool Exists(string key) { int index = GetHashCodeIndex(key); if (_dictionaryList[index].ContainsKey(key)) { DataModel model = (DataModel)_dictionaryList[index][key]; if (model.ObsloteType == ObsloteType.Never) { return true; } else if (model.DeadLine < DateTime.Now) //現在已經超過你的最后時間 { lock (_lockList[index]) { //被動清理,請求了資料,才能清理 _dictionaryList[index].Remove(key); } return false; } else { if (model.ObsloteType == ObsloteType.Relative) //沒有過期&是滑動 所以要更新 { model.DeadLine = DateTime.Now.Add(model.Duration); } return true; } } else { return false; } } /// <summary> /// 移除快取 /// </summary> /// <param name="key">快取鍵</param> public static void Remove(string key) { int index = GetHashCodeIndex(key); lock (_lockList[index]) { _dictionaryList[index].Remove(key); } } /// <summary> /// 移除所有快取 /// </summary> public static void RemoveAll() { for (int index = 0; index < _cpuNumer; index++) { lock (_lockList[index]) { _dictionaryList[index].Clear(); } } } /// <summary> /// 按條件移除 /// </summary> /// <param name="func">條件運算式</param> public static void RemoveCondition(Func<string, bool> func) { for (int i = 0; i < _cpuNumer; i++) { List<string> keyList = new List<string>(); lock (_lockList[i]) { foreach (var key in _dictionaryList[i].Keys) { if (func.Invoke(key)) { keyList.Add(key); } } keyList.ForEach(s => Remove(s)); } } } public static T GetT<T>(string key, Func<T> func) { T t; if (!Exists(key)) { t = func.Invoke(); Add(key, t); } else { t = Get<T>(key); } return t; } } /// <summary> /// 快取的資訊 /// </summary> internal class DataModel { public object Value { get; set; } public ObsloteType ObsloteType { get; set; } public DateTime DeadLine { get; set; } public TimeSpan Duration { get; set; } /// <summary> /// 資料清理后出發事件 /// </summary> public event Action DataClearEvent; } /// <summary> /// 快取策略 /// </summary> public enum ObsloteType { /// <summary> /// 永久 /// </summary> Never, /// <summary> /// 絕對過期 /// </summary> Absolutely, /// <summary> /// 相對過期 /// </summary> Relative } }
PS:值得一提的是為了執行緒安全所以我們加了lock鎖,但是加鎖的同時也限制了并發,降低了性能,故此處我們采用資料分拆+lock鎖的方式,將資料分拆存放到多個資料容器中,同時使用多個鎖,這樣容器之間就可以并發了,
下面來看下快取的使用:
using System; using System.Collections.Generic; using System.Threading.Tasks; namespace MyCache { /// <summary> /// 系統性能優化的第一步,就是使用快取, /// </summary> class Program { static void Main(string[] args) { try { { //多執行緒問題 List<Task> taskList = new List<Task>(); for (int i = 0; i < 1_000_000; i++) { int k = i; taskList.Add(Task.Run(() => CustomCache.Add($"TestKey_{k}", $"TestValue_{k}", 10))); } for (int i = 0; i < 100; i++) { int k = i; taskList.Add(Task.Run(() => { if (k % 10 == 0) { Console.WriteLine(CustomCache.Get<string>($"TestKey_{k}")); } CustomCache.Remove($"TestKey_{k}"); if (k % 10 == 0) { Console.WriteLine($"TestKey_{k}_Exists:{CustomCache.Exists($"TestKey_{k}")}"); } })); } for (int i = 0; i < 100; i++) { int k = i; taskList.Add(Task.Run(() => { CustomCache.Exists($"TestKey_{k}"); })); } Task.WaitAll(taskList.ToArray()); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.ReadKey(); } } }
運行結果如下所示:

雖然現在我們很少會使用到自己寫的快取,但是希望通過本文能夠使大家對本地快取的基本原理有進一步的認識,
Demo原始碼:
鏈接:https://pan.baidu.com/s/1GhLCBEaLqVL4wptjtZBSPQ 提取碼:u9fe
此文由博主精心撰寫轉載請保留此原文鏈接:https://www.cnblogs.com/xyh9039/p/13741834.html
著作權宣告:如有雷同純屬巧合,如有侵權請及時聯系本人修改,謝謝!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/134790.html
標籤:.NET技术
