參考類別庫
1.Install-Package Microsoft.Extensions.Caching.Memory
MemoryCacheOptions 快取配置
1.ExpirationScanFrequency 獲取或設定對過期項的連續掃描之間的最短時間間隔
2.SizeLimit 快取是沒有大小的的,此值設定快取的份數
3.CompactionPercentage 獲取或設定在超過最大大小時壓縮快取的數量,優先壓縮優先級較低的快取,0.2代表20%
services.AddMemoryCache(options => { // 快取最大為100份 //##注意netcore中的快取是沒有單位的,快取項和快取的相對關系 options.SizeLimit = 2; //快取滿了時候壓縮20%的優先級較低的資料 options.CompactionPercentage = 0.2; //兩秒鐘查找一次過期項 options.ExpirationScanFrequency = TimeSpan.FromSeconds(2); });
MemoryCacheEntryOptions 單個快取項配置
1.AbsoluteExpiration 絕對過期時間
2. AbsoluteExpirationRelativeToNow 相對于現在的絕對過期時間
3.SlidingExpiration 滑動過期時間,在時間段范圍內 快取被再次訪問,過期時間將會被重置
4.Priority 優先級
5.Size 快取份數
public bool Add(string key, object value, int ExpirtionTime = 20) { if (!string.IsNullOrEmpty(key)) { MemoryCacheEntryOptions cacheEntityOps = new MemoryCacheEntryOptions() { //滑動過期時間 20秒沒有訪問則清除 SlidingExpiration = TimeSpan.FromSeconds(ExpirtionTime), //設定份數 Size = 1, //優先級 Priority = CacheItemPriority.Low, }; //過期回掉 cacheEntityOps.RegisterPostEvictionCallback((keyInfo, valueInfo, reason, state) => { Console.WriteLine($"回呼函式輸出【鍵:{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. 實作 ICacheService
/// <summary> /// 快取介面實作 /// </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), //設定份數 Size = 1, //優先級 Priority = CacheItemPriority.Low, }; //過期回掉 cacheEntityOps.RegisterPostEvictionCallback((keyInfo, valueInfo, reason, state) => { Console.WriteLine($"回呼函式輸出【鍵:{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:https://www.cnblogs.com/mylinx/p/10443494.html
大神貼2:https://www.cnblogs.com/wyy1234/p/10519681.html#_label1_0
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/83045.html
標籤:.NET Core
