欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.NetCore?MemoryCache使用詳解

 更新時間:2022年06月18日 10:26:45   作者:深巷舊夏天  
這篇文章主要介紹了.NetCore?MemoryCache使用,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

引用類庫

1.Install-Package Microsoft.Extensions.Caching.Memory

MemoryCacheOptions 緩存配置

1.ExpirationScanFrequency獲取或設置對過期項的連續(xù)掃描之間的最短時間間隔

2.SizeLimit 緩存是沒有大小的的,此值設置緩存的份數(shù)

3.CompactionPercentage獲取或設置在超過最大大小時壓縮緩存的數(shù)量,優(yōu)先壓縮優(yōu)先級較低的緩存,0.2代表20%

services.AddMemoryCache(options => {
                // 緩存最大為100份
                //##注意netcore中的緩存是沒有單位的,緩存項和緩存的相對關系
                options.SizeLimit = 2;
                //緩存滿了時候壓縮20%的優(yōu)先級較低的數(shù)據(jù)
                options.CompactionPercentage = 0.2;
                //兩秒鐘查找一次過期項
                options.ExpirationScanFrequency = TimeSpan.FromSeconds(2);
            });

MemoryCacheEntryOptions 單個緩存項配置

1.AbsoluteExpiration 絕對過期時間

2.AbsoluteExpirationRelativeToNow 相對于現(xiàn)在的絕對過期時間

3.SlidingExpiration 滑動過期時間,在時間段范圍內 緩存被再次訪問,過期時間將會被重置

4.Priority 優(yōu)先級

5.Size 緩存份數(shù)

public bool Add(string key, object value, int ExpirtionTime = 20)
        {
            if (!string.IsNullOrEmpty(key))
            {
                MemoryCacheEntryOptions cacheEntityOps = new MemoryCacheEntryOptions()
                {
                    //滑動過期時間 20秒沒有訪問則清除
                    SlidingExpiration = TimeSpan.FromSeconds(ExpirtionTime),
                    //設置份數(shù)
                    Size = 1,
                    //優(yōu)先級
                    Priority = CacheItemPriority.Low,
                };
                //過期回掉
                cacheEntityOps.RegisterPostEvictionCallback((keyInfo, valueInfo, reason, state) =>
                {
                    Console.WriteLine($"回調函數(shù)輸出【鍵:{keyInfo},值:{valueInfo},被清除的原因:{reason}】");
                });
                _cache.Set(key, value, cacheEntityOps);
            }
            return true;
        }

完整代碼

1.接口

public interface ICacheService
    {
        /// <summary>
        ///  新增
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="ExpirtionTime"></param>
        /// <returns></returns>
        bool Add(string key, object value, int ExpirtionTime = 20);
        /// <summary>
        /// 獲取
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        string GetValue(string key);
        /// <summary>
        /// 驗證緩存項是否存在
        /// </summary>
        /// <param name="key">緩存Key</param>
        /// <returns></returns>
        bool Exists(string key);
        /// <summary>
        /// 移除
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        bool Remove(string key);
    }

2. 實現(xiàn)ICacheService

/// <summary>
    /// 緩存接口實現(xiàn)
    /// </summary>
    public class MemoryCacheService : ICacheService
    {
        protected IMemoryCache _cache;
        public MemoryCacheService(IMemoryCache cache)
        {
            _cache = cache;
        }
        public bool Add(string key, object value, int ExpirtionTime = 20)
        {
            if (!string.IsNullOrEmpty(key))
            {
                MemoryCacheEntryOptions cacheEntityOps = new MemoryCacheEntryOptions()
                {
                    //滑動過期時間 20秒沒有訪問則清除
                    SlidingExpiration = TimeSpan.FromSeconds(ExpirtionTime),
                    //設置份數(shù)
                    Size = 1,
                    //優(yōu)先級
                    Priority = CacheItemPriority.Low,
                };
                //過期回掉
                cacheEntityOps.RegisterPostEvictionCallback((keyInfo, valueInfo, reason, state) =>
                {
                    Console.WriteLine($"回調函數(shù)輸出【鍵:{keyInfo},值:{valueInfo},被清除的原因:{reason}】");
                });
                _cache.Set(key, value, cacheEntityOps);
            }
            return true;
        }
        public bool Remove(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return false;
            }
            if (Exists(key))
            {
                _cache.Remove(key);
                return true;
            }
            return false;
        }
        public string GetValue(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return null;
            }
            if (Exists(key))
            {
                return _cache.Get(key).ToString();
            }
            return null;
        }
        public bool Exists(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return false;
            }
            object cache;
            return _cache.TryGetValue(key, out cache);
        }
    }

大神貼1:http://www.dbjr.com.cn/article/195870.htm

大神貼2:http://www.dbjr.com.cn/article/252078.htm

到此這篇關于.NetCore MemoryCache使用的文章就介紹到這了,更多相關.NetCore MemoryCache使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論