.NET中的本地緩存(數據分拆+lock鎖)


本章將和大家分享如何使用數據分拆+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

版權聲明:如有雷同純屬巧合,如有侵權請及時聯系本人修改,謝謝!!!


免責聲明!

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



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