.NET Core 2.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應用遷移到.NET Core(一)
.NET應用遷移到.NET Core(二)風險評估
.NET應用遷移到.NET Core(三)從商業角度看移植過程
.NET應用遷移到.NET Core--調查案例
遷移傳統.net 應用到.net core [視頻]
應用工具 .NET Portability Analyzer 分析遷移dotnet core
.net core 2.0學習筆記(一):開發運行環境搭建
.net core 2.0學習筆記(二):Hello World & 進階
度量.net framework 遷移到.net core的工作量
遷移.net framework 工程到.net core
程序配置&ConfigurationManager
.NET Core 2.0遷移技巧之web.config配置文件
原文地址:http://www.cnblogs.com/mantgh/p/7429551.html
.NET社區新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注
總結
以上是生活随笔為你收集整理的.NET Core 2.0迁移技巧之MemoryCache问题修复的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Razor Page–Asp.Net C
- 下一篇: asp.net ajax控件工具集 Au