前言
大家應該都知道,對于傳統的.NET Framework項目而言,System.Runtime.Caching
命名空間是常用的工具了,其中MemoryCache類則常被用于實現內存緩存。
.NET Core 2.0暫時還不支持System.Runtime.Caching dll,這也就意味著MemoryCache相關代碼不再起作用了。
但是好消息是,我們可以使用.NET Core 2.0的新API實現內存緩存功能,簡單修改代碼,解決不兼容問題。下面話不多說了,來一起看看詳細的介紹吧。
解決方案
1.將舊代碼導入項目中,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
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
1
|
using Microsoft.Extensions.Caching.Memory; |
3.改寫代碼,使用新的API實現內存緩存功能
初始化緩存對象方式改寫前:
1
|
static ObjectCache cache = MemoryCache.Default; |
初始化緩存對象方式改寫后:
1
|
static MemoryCache cache = new MemoryCache( new MemoryCacheOptions()); |
讀取內存緩存值方式變化:
1
2
3
4
5
6
7
8
|
private object GetCacheValue( string key) { if (key != null && cache.Contains(key)) { return cache[key]; } return default ( object ); } |
改寫后:
1
2
3
4
5
6
7
8
9
10
11
12
|
private object GetCacheValue( string key) { object val = null ; if (key != null && cache.TryGetValue(key, out val)) { return val; } else { return default ( object ); } } |
設定內存緩存內容方式變化:
1
2
3
4
5
6
7
8
9
10
11
|
public static void SetChacheValue( string key, object value) { if (key != null ) { CacheItemPolicy policy = new CacheItemPolicy { SlidingExpiration = TimeSpan.FromHours(1) }; cache.Set(key, value, policy); } } |
修改后:
1
2
3
4
5
6
7
8
9
10
|
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簡單改動下下就能重用現有絕大部分舊代碼,將其遷移過來繼續起作用。
遷移后的完整代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
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) }); } } } } |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://www.cnblogs.com/mantgh/p/7429551.html