.NET Core2.0中MemoryCache問題有什么方法修復解決?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
前言
大家應該都知道,對于傳統的.NET Framework項目而言,System.Runtime.Caching
命名空間是常用的工具了,其中MemoryCache類則常被用于實現內存緩存。
.NET Core 2.0暫時還不支持System.Runtime.Caching dll,這也就意味著MemoryCache相關代碼不再起作用了。
但是好消息是,我們可以使用.NET Core 2.0的新API實現內存緩存功能,簡單修改代碼,解決不兼容問題。下面話不多說了,來一起看看詳細的介紹吧。
解決方案
1.將舊代碼導入項目中,如下:
using System; using System.Runtime.Caching; namespace TestWebApp.Service { public class MemoryCacheService { static ObjectCache cache = MemoryCache.Default; /// <summary> /// 獲取緩存值 /// </summary> /// <param name="key"></param> /// <returns></returns> private object GetCacheValue(string key) { if (key != null && cache.Contains(key)) { return cache[key]; } return default(object); } /// <summary> /// 添加緩存內容 /// </summary> /// <param name="key"></param> /// <param name="value"></param> public static void SetChacheValue(string key, object value) { if (key != null) { CacheItemPolicy policy = new CacheItemPolicy { SlidingExpiration = TimeSpan.FromHours(1) }; cache.Set(key, value, policy); } } } }
導入后你會發現VS會提示無法找到System.Runtime.Caching
命名空間,原有的代碼無法直接編譯使用。
2.添加對Microsoft.Extensions.Caching.Memory
命名空間的引用,它提供了.NET Core默認實現的MemoryCache類,以及全新的內存緩存API
using Microsoft.Extensions.Caching.Memory;
3.改寫代碼,使用新的API實現內存緩存功能
初始化緩存對象方式改寫前:
static ObjectCache cache = MemoryCache.Default;
初始化緩存對象方式改寫后:
static MemoryCache cache = new MemoryCache(new MemoryCacheOptions());
讀取內存緩存值方式變化:
private object GetCacheValue(string key) { if (key != null && cache.Contains(key)) { return cache[key]; } return default(object); }
改寫后:
private object GetCacheValue(string key) { object val = null; if (key != null && cache.TryGetValue(key, out val)) { return val; } else { return default(object); } }
設定內存緩存內容方式變化:
public static void SetChacheValue(string key, object value) { if (key != null) { CacheItemPolicy policy = new CacheItemPolicy { SlidingExpiration = TimeSpan.FromHours(1) }; cache.Set(key, value, policy); } }
修改后:
public static void SetChacheValue(string key, object value) { if (key != null) { cache.Set(key, value, new MemoryCacheEntryOptions { SlidingExpiration = TimeSpan.FromHours(1) }); } }
結論
在使用了Microsoft.Extensions.Caching.Memory
下的新API改寫了舊代碼后,你會發現原有的各種內存緩存超時策略全都是有對應新API的,包括AbsoluteExpiration, SlidingExpiration等等。
所以我們還是可以很輕松的使用.NET Core新API簡單改動下下就能重用現有絕大部分舊代碼,將其遷移過來繼續起作用。
遷移后的完整代碼如下:
using Microsoft.Extensions.Caching.Memory; using System; namespace TestMemoryCacheWebApp.Services { public class MemoryCacheService { static MemoryCache cache = new MemoryCache(new MemoryCacheOptions()); /// <summary> /// 獲取緩存值 /// </summary> /// <param name="key"></param> /// <returns></returns> private object GetCacheValue(string key) { object val = null; if (key != null && cache.TryGetValue(key, out val)) { return val; } else { return default(object); } } /// <summary> /// 添加緩存內容 /// </summary> /// <param name="key"></param> /// <param name="value"></param> public static void SetChacheValue(string key, object value) { if (key != null) { cache.Set(key, value, new MemoryCacheEntryOptions { SlidingExpiration = TimeSpan.FromHours(1) }); } } } }
關于.NET Core2.0中MemoryCache問題有什么方法修復解決問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。