自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

巧用Lock解決緩存擊穿的解決方案

數(shù)據(jù)庫 其他數(shù)據(jù)庫
緩存擊穿是指緩存中沒有但數(shù)據(jù)庫中有的數(shù)據(jù)(一般是緩存時(shí)間到期),這時(shí)由于并發(fā)用戶特別多,同時(shí)讀緩存沒讀到數(shù)據(jù),又同時(shí)去數(shù)據(jù)庫去取數(shù)據(jù),引起數(shù)據(jù)庫壓力瞬間增大,造成過大壓力。

背景

緩存擊穿是指緩存中沒有但數(shù)據(jù)庫中有的數(shù)據(jù)(一般是緩存時(shí)間到期),這時(shí)由于并發(fā)用戶特別多,同時(shí)讀緩存沒讀到數(shù)據(jù),又同時(shí)去數(shù)據(jù)庫去取數(shù)據(jù),引起數(shù)據(jù)庫壓力瞬間增大,造成過大壓力。

解決方案

1、設(shè)置熱點(diǎn)數(shù)據(jù)永遠(yuǎn)不過期。

2、加互斥鎖,互斥鎖參考代碼如下:

2.1、根據(jù)key生成object()

private static object GetMemoryCacheLockObject(string key)
        {
            string cacheLockKey = string.Format(MemoryCacheLockObjectFormat, key);
            lock (CacheObject)
            {
                var lockObject = CacheObject[cacheLockKey];
                if (lockObject == null)
                {
                    // 取得每個(gè) Key專屬的 lock object;若同時(shí)有多個(gè) thread要求相同資料,只會(huì)(到資料庫)查第一次,剩下的從 cache讀取
                    lockObject = new object();
                    CacheObject.Set(
                        cacheLockKey,
                        lockObject,
                        new System.Runtime.Caching.CacheItemPolicy()
                        {
                            AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(10)
                        }
                    );
                }


                return lockObject;
            }
        }

2.2、lock住GetMemoryCacheLockObject(key)

public T Get<T>(string key, Func<T> getDataWork, TimeSpan absoluteExpireTime, bool forceRefresh = false, bool returnCopy = true) where T : class
        {
            try
            {
                lock (GetMemoryCacheLockObject(key))
                {
                    /*
System.ArgumentNullException: Value cannot be null.
at System.Threading.Monitor.Enter(Object obj)
at BQoolCommon.Helpers.Cache.MemoryCacheLayer.Get[T](String key, Func`1 getDataWork, TimeSpan absoluteExpireTime, Boolean forceRefresh, Boolean returnCopy) in D:\Source\BQoolCommon\BQoolCommon.Helpers\Cache\MemoryCacheLayer.cs:line 46
                     */
                    T result = CacheObject[key] as T;


                    if (result != null && forceRefresh)
                    {// 是否清除Cache,強(qiáng)制重查
                        result = null;
                    }


                    if (result == null)
                    {
                        //執(zhí)行取得資料的委派作業(yè)
                        result = getDataWork();


                        if (result != null)
                        {
                            Set(key, result, absoluteExpireTime);
                        }
                    }


                    if (returnCopy)
                    {
                        //複製一份新的參考
                        string serialize = JsonConvert.SerializeObject(result);
                        return JsonConvert.DeserializeObject<T>(serialize);
                    }
                    else
                    {
                        return result;
                    }
                }
            }
            catch
            {
                return getDataWork();
            }
        }

總結(jié)說明

1、緩存中有數(shù)據(jù),直接走下述代碼就返回結(jié)果了

T result = CacheObject[key] as T;

  2、緩存中沒有數(shù)據(jù),第1個(gè)進(jìn)入的線程,獲取鎖并從數(shù)據(jù)庫去取數(shù)據(jù),沒釋放鎖之前,其他并行進(jìn)入的線程會(huì)等待,再重新去緩存取數(shù)據(jù)。這樣就防止都去數(shù)據(jù)庫重復(fù)取數(shù)據(jù),重復(fù)往緩存中更新數(shù)據(jù)情況出現(xiàn)。

try
            {
                lock (GetMemoryCacheLockObject(key))
                {
                    /*
System.ArgumentNullException: Value cannot be null.
at System.Threading.Monitor.Enter(Object obj)
at BQoolCommon.Helpers.Cache.MemoryCacheLayer.Get[T](String key, Func`1 getDataWork, TimeSpan absoluteExpireTime, Boolean forceRefresh, Boolean returnCopy) in D:\Source\BQoolCommon\BQoolCommon.Helpers\Cache\MemoryCacheLayer.cs:line 46
                     */
                    T result = CacheObject[key] as T;

3、取得每個(gè) Key專有的 lock object;若同時(shí)有多個(gè) thread要求相同資料,只會(huì)(到數(shù)據(jù)庫)查第一次,剩下的從 cache讀取。

string cacheLockKey = string.Format(MemoryCacheLockObjectFormat, key);
            lock (CacheObject)
            {
                var lockObject = CacheObject[cacheLockKey];
                if (lockObject == null)
                {
                    // 取得每個(gè) Key專屬的 lock object;若同時(shí)有多個(gè) thread要求相同資料,只會(huì)(到資料庫)查第一次,剩下的從 cache讀取
                    lockObject = new object();
責(zé)任編輯:武曉燕 來源: 程序員編程日記
相關(guān)推薦

2021-01-31 10:51:37

緩存lock數(shù)據(jù)

2018-11-12 11:12:46

2023-11-10 14:58:03

2020-03-05 09:09:18

緩存原因方案

2023-10-13 08:11:22

2022-03-08 00:07:51

緩存雪崩數(shù)據(jù)庫

2019-10-08 16:05:19

Redis數(shù)據(jù)庫系統(tǒng)

2023-07-19 07:51:43

Redis緩存高可用

2024-06-24 00:30:00

2023-10-30 07:56:46

Spring緩存

2012-05-27 16:21:31

IDC華為

2018-12-03 12:17:27

Semptian解決方案

2018-12-03 11:59:42

Inventec解決方案

2018-12-03 12:13:21

Mellanox解決方案

2018-12-03 12:26:30

YADRO解決方案

2025-02-04 17:40:44

2016-03-13 17:58:57

2011-11-30 13:08:55

企業(yè)防毒防毒方案拯救三

2011-12-09 11:13:17

2009-12-23 21:06:47

統(tǒng)一通信多媒體聯(lián)絡(luò)中心平臺(tái)華為
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)