最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

.NET?MemoryCache如何清除全部緩存

 更新時(shí)間:2021年12月22日 10:35:39   作者:Agile.Zhou  
本文主要介紹了.NET?MemoryCache如何清除全部緩存,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最近有個(gè)需求需要定時(shí)清理服務(wù)器上所有的緩存。本來以為很簡(jiǎn)單的調(diào)用一下 MemoryCache.Clear 方法就完事了。誰知道 MemoryCache 類以及 IMemoryCache 擴(kuò)展方法都沒有 Clear 方法。這可給難住了,于是想找到所有的 Keys 來一個(gè)個(gè) Remove ,誰知道居然也沒有獲取所有 Key 的方法。于是研究了一下 ,找到一些方法,下面介紹兩個(gè)方法:

自定義 CacheWrapper 包裝類

MemoryCache 構(gòu)造 Entry 的時(shí)候支持傳入 CancellationChangeToken 對(duì)象,當(dāng) CancellationChangeToken.Cancel 觸發(fā)的時(shí)候會(huì)自動(dòng)使該對(duì)象過期。那么我們只要對(duì) MemoryCache 類包裝一下很容易實(shí)現(xiàn)一個(gè)自己的 Cache 類。

 public class CacheWrapper
    {
        private readonly IMemoryCache _memoryCache;
        private CancellationTokenSource _resetCacheToken = new();

        public CacheWrapper(IMemoryCache memoryCache)
        {
            _memoryCache = memoryCache;
        }

        public void Add(object key, object value, MemoryCacheEntryOptions memoryCacheEntryOptions)
        {
            using var entry = _memoryCache.CreateEntry(key);
            entry.SetOptions(memoryCacheEntryOptions);
            entry.Value = value;

            // add an expiration token that allows us to clear the entire cache with a single method call
            entry.AddExpirationToken(new CancellationChangeToken(_resetCacheToken.Token));
        }

        public object Get(object key)
        {
            return _memoryCache.Get(key);
        }

        public void Remove(object key)
        {
            _memoryCache.Remove(key);
        }

        public void Clear()
        {
            _resetCacheToken.Cancel(); // this triggers the CancellationChangeToken to expire every item from cache

            _resetCacheToken.Dispose(); // dispose the current cancellation token source and create a new one
            _resetCacheToken = new CancellationTokenSource();
        }
    }

然后單元測(cè)試測(cè)試一下:

        [TestMethod()]
        public void ClearTest()
        {
            var memCache = new MemoryCache(new MemoryCacheOptions());
            var wrapper = new CacheWrapper(memCache);

            for (int i = 0; i < 10; i++)
            {
                wrapper.Add(i.ToString(), new object(), new MemoryCacheEntryOptions());
            }

            Assert.IsNotNull(wrapper.Get("1"));
            Assert.IsNotNull(wrapper.Get("9"));

            wrapper.Clear();

            for (int i = 0; i < 10; i++)
            {
                Assert.IsNull(wrapper.Get(i.ToString()));
            }

            for (int i = 0; i < 10; i++)
            {
                wrapper.Add(i.ToString(), new object(), new MemoryCacheEntryOptions());
            }

            Assert.IsNotNull(wrapper.Get("1"));
            Assert.IsNotNull(wrapper.Get("9"));

            wrapper.Clear();

            for (int i = 0; i < 10; i++)
            {
                Assert.IsNull(wrapper.Get(i.ToString()));
            }

        }

測(cè)試通過。

Compact 方法

以上 CacheWrapper 類雖然可以實(shí)現(xiàn)我們想要的功能,但是對(duì)于原來的程序有侵入,需要使用 CacheWrapper 類替換默認(rèn)的 MemoryCache 類,不是太好。于是不死心繼續(xù)研究,后來直接看了 MemoryCache 的代碼(源碼在這),開源真香。發(fā)現(xiàn) MemoryCache 有個(gè) Compact 方法好像在干刪除的勾當(dāng)。也怪我英文不好,這單詞是壓縮的意思,居然才發(fā)現(xiàn)。。。。于是我們的清除所有對(duì)象的需求不就輕而易舉了么?

 /// Remove at least the given percentage (0.10 for 10%) of the total entries (or estimated memory?), according to the following policy:
        /// 1. Remove all expired items.
        /// 2. Bucket by CacheItemPriority.
        /// 3. Least recently used objects.
        /// ?. Items with the soonest absolute expiration.
        /// ?. Items with the soonest sliding expiration.
        /// ?. Larger objects - estimated by object graph size, inaccurate.
MemoryCache.Compact(double percentage);

Compact 方法會(huì)對(duì)緩存的對(duì)象進(jìn)行壓縮,參數(shù)是個(gè)double,0.1 表示壓縮 10% ,那么傳 1.0 就是壓縮 100%,那不就是 Clear All 么。所以我可以使用 Compact(1.0) 來清除所有的緩存對(duì)象。單元測(cè)試跑一下:

[TestMethod()]
        public void CompactTest()
        {
            var memCache = new MemoryCache(new MemoryCacheOptions());

            for (int i = 0; i < 10; i++)
            {
                memCache.Set(i.ToString(), new object(), new MemoryCacheEntryOptions());
            }

            Assert.IsNotNull(memCache.Get("1"));
            Assert.IsNotNull(memCache.Get("9"));

            memCache.Compact(1);

            for (int i = 0; i < 10; i++)
            {
                Assert.IsNull(memCache.Get(i.ToString()));
            }

            for (int i = 0; i < 10; i++)
            {
                memCache.Set(i.ToString(), new object(), new MemoryCacheEntryOptions());
            }

            Assert.IsNotNull(memCache.Get("1"));
            Assert.IsNotNull(memCache.Get("9"));

            memCache.Compact(1);

            for (int i = 0; i < 10; i++)
            {
                Assert.IsNull(memCache.Get(i.ToString()));
            }
        }

完美通過。

這里簡(jiǎn)單介紹下 Compact 方法。根據(jù)注釋它會(huì)按照已下優(yōu)先級(jí)刪除對(duì)象:

  • 過期的對(duì)象
  • CacheItemPriority 設(shè)置的優(yōu)先級(jí),等級(jí)越高越不容易被刪除
  • 最近最少被使用的對(duì)象
  • 絕對(duì)過期時(shí)間
  • 滑動(dòng)過期時(shí)間
  • 大對(duì)象

到此這篇關(guān)于.NET MemoryCache如何清除全部緩存的文章就介紹到這了,更多相關(guān).NET MemoryCache清除緩存內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#實(shí)現(xiàn)托盤程序并禁止多個(gè)應(yīng)用實(shí)例運(yùn)行的方法

    C#實(shí)現(xiàn)托盤程序并禁止多個(gè)應(yīng)用實(shí)例運(yùn)行的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)托盤程序并禁止多個(gè)應(yīng)用實(shí)例運(yùn)行的方法,涉及C#中NotifyIcon控件的使用及設(shè)置標(biāo)志位控制程序只運(yùn)行一個(gè)的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • C#實(shí)現(xiàn)影院售票系統(tǒng)

    C#實(shí)現(xiàn)影院售票系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)影院售票系統(tǒng),解析了售票系統(tǒng)的難點(diǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • C# 讀寫XML文件實(shí)例代碼

    C# 讀寫XML文件實(shí)例代碼

    在本篇文章里小編給大家整理的是關(guān)于C# 讀寫XML文件最簡(jiǎn)單方法,需要的朋友們可以跟著學(xué)習(xí)參考下。
    2020-03-03
  • C#實(shí)現(xiàn)對(duì)二維數(shù)組排序的方法

    C#實(shí)現(xiàn)對(duì)二維數(shù)組排序的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)對(duì)二維數(shù)組排序的方法,實(shí)例分析了C#數(shù)組遍歷與排序的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • 一文詳解C#?Chart控件

    一文詳解C#?Chart控件

    這篇文章主要介紹了一文學(xué)習(xí)C#?Chart控件,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • C#實(shí)現(xiàn)單例模式的幾種方法總結(jié)

    C#實(shí)現(xiàn)單例模式的幾種方法總結(jié)

    這篇文章主要介紹了C#實(shí)現(xiàn)單例模式的幾種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • 一個(gè)可逆加密的類(使用3DES加密)

    一個(gè)可逆加密的類(使用3DES加密)

    表示三重?cái)?shù)據(jù)加密標(biāo)準(zhǔn)算法的基類,TripleDES 的所有實(shí)現(xiàn)都必須從此基類派生。是從 SymmetricAlgorithm 類里繼承出來。
    2011-07-07
  • windows下C#定時(shí)管理器框架Task.MainForm詳解

    windows下C#定時(shí)管理器框架Task.MainForm詳解

    這篇文章主要為大家詳細(xì)介紹了windows下C#定時(shí)管理器框架Task.MainForm的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 詳解C#中一維數(shù)組的插入

    詳解C#中一維數(shù)組的插入

    本文內(nèi)容給大家分享了在C#中進(jìn)行一維數(shù)組的插入的詳細(xì)實(shí)例代碼,大家可以測(cè)試下。
    2018-03-03
  • C#實(shí)現(xiàn)無損壓縮圖片代碼示例

    C#實(shí)現(xiàn)無損壓縮圖片代碼示例

    這篇文章介紹了C#實(shí)現(xiàn)無損壓縮圖片的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04

最新評(píng)論

饶河县| 梨树县| 天台县| 深圳市| 遵义市| 江山市| 吉水县| 台山市| 大荔县| 皋兰县| 云龙县| 裕民县| 平湖市| 鹤壁市| 碌曲县| 东光县| 靖西县| 仁布县| 洛隆县| 错那县| 高唐县| 镇巴县| 清新县| 宜黄县| 宣城市| 西昌市| 西藏| 防城港市| 关岭| 美姑县| 邯郸县| 新疆| 隆昌县| 海淀区| 兰州市| 柏乡县| 友谊县| 武夷山市| 湖口县| 政和县| 巴里|