.NET Core 2.0遷移小技巧之MemoryCache問題修復(fù)解決的方法
前言
大家應(yīng)該都知道,對(duì)于傳統(tǒng)的.NET Framework項(xiàng)目而言,System.Runtime.Caching命名空間是常用的工具了,其中MemoryCache類則常被用于實(shí)現(xiàn)內(nèi)存緩存。
.NET Core 2.0暫時(shí)還不支持System.Runtime.Caching dll,這也就意味著MemoryCache相關(guān)代碼不再起作用了。
但是好消息是,我們可以使用.NET Core 2.0的新API實(shí)現(xiàn)內(nèi)存緩存功能,簡單修改代碼,解決不兼容問題。下面話不多說了,來一起看看詳細(xì)的介紹吧。
解決方案
1.將舊代碼導(dǎo)入項(xiàng)目中,如下:
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>
/// 添加緩存內(nèi)容
/// </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);
}
}
}
}
導(dǎo)入后你會(huì)發(fā)現(xiàn)VS會(huì)提示無法找到System.Runtime.Caching命名空間,原有的代碼無法直接編譯使用。

2.添加對(duì)Microsoft.Extensions.Caching.Memory命名空間的引用,它提供了.NET Core默認(rèn)實(shí)現(xiàn)的MemoryCache類,以及全新的內(nèi)存緩存API
using Microsoft.Extensions.Caching.Memory;
3.改寫代碼,使用新的API實(shí)現(xiàn)內(nèi)存緩存功能
初始化緩存對(duì)象方式改寫前:
static ObjectCache cache = MemoryCache.Default;
初始化緩存對(duì)象方式改寫后:
static MemoryCache cache = new MemoryCache(new MemoryCacheOptions());
讀取內(nèi)存緩存值方式變化:
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);
}
}
設(shè)定內(nèi)存緩存內(nèi)容方式變化:
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)
});
}
}
結(jié)論
在使用了Microsoft.Extensions.Caching.Memory下的新API改寫了舊代碼后,你會(huì)發(fā)現(xiàn)原有的各種內(nèi)存緩存超時(shí)策略全都是有對(duì)應(yīng)新API的,包括AbsoluteExpiration, SlidingExpiration等等。
所以我們還是可以很輕松的使用.NET Core新API簡單改動(dòng)下下就能重用現(xiàn)有絕大部分舊代碼,將其遷移過來繼續(xù)起作用。
遷移后的完整代碼如下:
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>
/// 添加緩存內(nèi)容
/// </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)
});
}
}
}
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Chrome內(nèi)核下由ashx輸出的js代碼不起作用的解決方法
Chrome內(nèi)核下由ashx輸出的js代碼不起作用的解決方法,需要的朋友可以參考一下2013-03-03
獲取asp.net服務(wù)器控件的客戶端ID和Name的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄@取asp.net服務(wù)器控件的客戶端ID和Name的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05
深入解析.NET 許可證編譯器 (Lc.exe) 的原理與源代碼剖析
許可證編譯器 (Lc.exe) 的作用是讀取包含授權(quán)信息的文本文件,并產(chǎn)生一個(gè)可作為資源嵌入到公用語言運(yùn)行庫可執(zhí)行文件中的 .licenses 文件2013-07-07
IE10下Gridview后臺(tái)設(shè)置行高不起作用解決方法
GridView1.HeaderStyle.Height=17發(fā)現(xiàn)在IE10 中不起作用,經(jīng)過反復(fù)測(cè)試修改為e.Row.Cells[0].Height=17即可解決問題,有類似問題的朋友可以參考下哈2013-04-04
Consul的搭建和.Net5的注冊(cè)和獲取方法(Win10簡單版)
Consul?是一個(gè)用來實(shí)現(xiàn)分布式系統(tǒng)服務(wù)發(fā)現(xiàn)與配置的開源工具。接下來通過本文給大家介紹win10下Consul的搭建和.Net5的注冊(cè)和獲取方法,感興趣的朋友一起看看吧2022-01-01
asp.net DropDownList自定義控件,讓你的分類更清晰
記得上次做論壇,一個(gè)功能就是合并2個(gè)子板塊的主題,用級(jí)聯(lián)的2個(gè)DropDownList也是可以完成,那樣我們要合并的時(shí)候總共就有4個(gè)DropDownList控件,覺得界面友好2011-10-10
ASP.net在頁面所有內(nèi)容生成后、輸出內(nèi)容前對(duì)頁面內(nèi)容進(jìn)行操作
ASP.net在頁面所有內(nèi)容生成后、輸出內(nèi)容前對(duì)頁面內(nèi)容進(jìn)行操作...2007-04-04

