This article mainly introduces to you about the. NET Core 2.0 Migration Tips of the memorycache problem fix the relevant information, the text through the sample code introduced in very detailed, to everyone's study or work has a certain reference learning value, The friends who need to study together with the small series.
Objective
As you all know, namespaces are a common tool for traditional. NET Framework projects System.Runtime.Caching
, where the MemoryCache class is often used to implement memory caching.
. NET Core 2.0 temporarily does not support System.Runtime.Caching DLLs, which means that memorycache related code no longer works.
But the good news is that we can use the new API for. NET Core 2.0 to implement the memory caching feature, simply modify the code to address incompatibility issues. The following words do not say much, come together to see the detailed introduction.
Solution Solutions
1. Import the old code into the project as follows:
Using system;using system.runtime.caching;namespace testwebapp.service{public class Memorycacheservice { static ObjectCache cache = Memorycache.default; <summary>///Get Cache value///</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>///Add cached content///</summary>// <param name= "key" ></param> <param name= "value" ></param> public static void Setchachevalue (string key, object value) { C21/>if (key = null) { cacheitempolicy policy = new CacheItemPolicy { slidingexpiration = Timespan.fromhours (1) }; Cache. Set (key, value, policy); } } }}
After importing you will find that the VS will not be able System.Runtime.Caching
to find the namespace, the original code can not be directly compiled to use.
2. Add Microsoft.Extensions.Caching.Memory
a reference to the namespace, which provides the MemoryCache class for the. NET core default implementation, and the new memory cache API
Using Microsoft.Extensions.Caching.Memory;
3. Rewrite the code and implement the memory caching function with the new API
Initialize cache object mode before overwriting:
static ObjectCache cache = Memorycache.default;
After the Initialize cache object mode is overwritten:
static MemoryCache cache = new MemoryCache (new Memorycacheoptions ());
Read memory Cache value change mode:
Private Object Getcachevalue (string key) {if (key! = null && cache. Contains (key)) { return cache[key];} return default (object);}
After rewriting:
Private Object Getcachevalue (string key) {object val = null; if (key! = null && cache. TryGetValue (key, out Val)) { return val;} else { return default (object);}}
To set the memory cache content mode change:
public static void Setchachevalue (string key, object value) {if (key! = null) { cacheitempolicy policy = new CacheItem Policy { slidingexpiration = timespan.fromhours (1) }; Cache. Set (key, value, policy); }}
After modification:
public static void Setchachevalue (string key, object value) {if (key! = null) { cache. Set (key, value, new memorycacheentryoptions { slidingexpiration = timespan.fromhours (1) });}}
Conclusion
After using the Microsoft.Extensions.Caching.Memory
new API to rewrite the old code, you will find that the original memory cache time-out policies are all corresponding to the new API, including absoluteexpiration, slidingexpiration and so on.
So we can still easily use. NET Core new API simple changes under the can reuse most of the existing old code, migrate it to continue to work.
The complete code after the migration is as follows:
using microsoft.extensions.caching.memory;using System;namespace testmemorycachewebapp.services{public class Memorycacheservice {static MemoryCache cache = new MemoryCache (New Memoryca Cheoptions ()); <summary>///Get Cache value///</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 to default (object); }}///<summary>///Add cached content///</summary>//<param name= "key" ></param>//<param Nam E= "value" ></param> public static void Setchachevalue (string key, object value) {if (key! = null) {Cach E.set (key, value, new Memorycacheentryoptions {slidingexpiration = timespan.fromhours (1)}); } } }}