This article mainly introduces you to the relevant information about .NET Core 2.0 migration tips - MemoryCache problem repair and solution. The article introduces it in detail through sample code, which has certain reference learning value for everyone's study or work. Friends who need it, please follow the editor to learn together.
Preface
Everyone should know that for traditional .NET Framework projects, System.Runtime.Caching
Namespace is a commonly used tool, and the MemoryCache class is often used to implement memory caching.
.NET Core 2.0 does not yet support System.Runtime.Caching dll, which means that MemoryCache related code no longer works.
But the good news is that we can use the new API of .NET Core 2.0 to implement the memory cache function and simply modify the code to solve the incompatibility problem. Not much to say below, let’s take a look at the detailed introduction.
Solution
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> /// 获取缓存值 /// </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); } } } }
After importing, you will find that VS will prompt that the System.Runtime.Caching
namespace cannot be found, and the original code cannot be directly compiled and used.
2. Add a reference to the Microsoft.Extensions.Caching.Memory
namespace, which provides the MemoryCache class implemented by .NET Core by default, and Brand new memory cache API
using Microsoft.Extensions.Caching.Memory;
3. Rewrite the code and use the new API to implement the memory cache function
Initialize the cache object method before rewriting:
static ObjectCache cache = MemoryCache.Default;
After rewriting the method of initializing cache objects:
static MemoryCache cache = new MemoryCache(new MemoryCacheOptions());
Changes in the method of reading memory cache values:
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); } }
Changes in setting the memory cache content method:
public static void SetChacheValue(string key, object value) { if (key != null) { CacheItemPolicy policy = new CacheItemPolicy { 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 After the new API under Microsoft.Extensions.Caching.Memory
rewrites the old code, you will find that all the original memory cache timeout strategies have corresponding new APIs, including AbsoluteExpiration, SlidingExpiration, etc.
So we can still easily use the new .NET Core API to reuse most of the existing old code with simple changes, and migrate it to continue to work.
The complete code after migration is as follows:
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) }); } } } }
The above is the detailed content of MemoryCache problem fix solutions. For more information, please follow other related articles on the PHP Chinese website!