.NET Core 2.0迁移技巧之MemoryCache问题修复
2017-08-25 20:19
330 查看
对于传统的.NET Framework项目而言,System.Runtime.Caching命名空间是常用的工具了,其中MemoryCache类则常被用于实现内存缓存。
.NET Core 2.0暂时还不支持System.Runtime.Caching dll,这也就意味着MemoryCache相关代码不再起作用了。
导入后你会发现VS会提示无法找到System.Runtime.Caching命名空间,原有的代码无法直接编译使用。
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/29/c8af9590051b3089b36604fad53cdaaa.png)
初始化缓存对象方式改写后:
读取内存缓存值方式变化:
改写后:
设定内存缓存内容方式变化:
修改后:
所以我们还是可以很轻松的使用.NET Core新API简单改动下下就能重用现有绝大部分旧代码,将其迁移过来继续起作用。
迁移后的完整代码如下:
.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命名空间,原有的代码无法直接编译使用。
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/29/c8af9590051b3089b36604fad53cdaaa.png)
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 Core 2.0迁移技巧之MemoryCache问题修复
- .NET Core 2.0迁移技巧之MemoryCache问题修复
- .NET Core 2.0迁移小技巧之MemoryCache问题修复解决的方法
- .NET Core 2.0迁移技巧之web.config配置文件
- .NET Core 2.0迁移技巧之web.config配置文件
- .NET Core 2.0迁移技巧之web.config配置文件
- .NET Core 2.0迁移技巧之web.config配置文件
- [技巧]修复WIN7计算机管理无法打开问题
- .Net Core迁移到MSBuild的多平台编译问题
- AFNetworking 2.0 编译不过的问题修复
- .NET 4.5+项目迁移.NET Core的问题记录
- .NET Core 2.0迁移小技巧之web.config 配置文件示例详解
- asp.net 2.0常见问题技巧1
- AFNetworking 2.0 编译不过的问题修复
- 学习向 Solaris 操作系统迁移 C 和 C++ 应用程序的有关问题与技巧
- ASP.net 2.0中水晶报表迁移部署问题
- AFNetworking 2.0 编译不过的问题修复
- .net framework 迁移至.net core2.0专题
- .net core 2.0 数据访问-迁移
- Exchange2003的邮件服务器修复迁移碰到的两个小问题