Net core 关于缓存的实现
在很多项目中, 需要用到缓存,借鉴网上前辈们的一些经验,自己再进行总结简化了一些, 做出如下的缓存操作,其中包含内存缓存(IMemoryCache) 和 Redis 缓存;
一.前提内容, 导入两个包: Microsoft.Extensions.Caching.Memory 和 Microsoft.Extensions.Caching.Redis ,并在使用的类中using 一下它们. 我这里是用2.1.0版本的;
二. 创建 ICacheService 公共接口 ,我这里写的比较简单, 如若业务需要可自行增加 异步和批量的接口方法.
/// <summary>
/// 缓存接口
/// 分别内存缓存和Redis缓存(2.1.0版本)
/// </summary>
public interface ICacheService
{
/// <summary>
/// 新增
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="ExpirtionTime"></param>
/// <returns></returns>
bool Add(string key, object value, int ExpirtionTime = ); /// <summary>
/// 获取
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
string GetValue(string key);
/// <summary>
/// 验证缓存项是否存在
/// </summary>
/// <param name="key">缓存Key</param>
/// <returns></returns>
bool Exists(string key); /// <summary>
/// 移除
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
bool Remove(string key);
}
三. 再分别创建 MemoryCacheService 和RedisCacheService 类, 并继承 ICacheService 接口.
a. MemoryCacheService 类 , 记得using 一下 Microsoft.Extensions.Caching.Memory
/// <summary>
/// 缓存接口实现
/// </summary>
public class MemoryCacheService : ICacheService
{
protected IMemoryCache _cache; public MemoryCacheService(IMemoryCache cache)
{
_cache = cache;
} public bool Add(string key, object value, int ExpirtionTime = )
{
if (!string.IsNullOrEmpty(key))
{
_cache.Set(key, value , DateTimeOffset.Now.AddMinutes(ExpirtionTime));
}
return true;
} public bool Remove(string key)
{
if (string.IsNullOrEmpty(key))
{
return false;
}
if (Exists(key))
{
_cache.Remove(key);
return true;
}
return false;
} public string GetValue(string key)
{
if (string.IsNullOrEmpty(key))
{
return null;
}
if (Exists(key))
{
return _cache.Get(key).ToString();
}
return null;
} public bool Exists(string key)
{
if (string.IsNullOrEmpty(key))
{
return false;
} object cache;
return _cache.TryGetValue(key, out cache);
} }
b. RedisCacheService 类 , 记得using 一下 Microsoft.Extensions.Caching.Redis
public class RedisCacheService:ICacheService
{
protected RedisCache _redisCache = null; public RedisCacheService(RedisCacheOptions options)
{
_redisCache = new RedisCache(options);
} public bool Add(string key, object value,int ExpirtionTime=)
{
if (!string.IsNullOrEmpty(key))
{
_redisCache.Set(key, Encoding.UTF8.GetBytes(value.ToString()), new DistributedCacheEntryOptions()
{
AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(ExpirtionTime)
});
}
return true;
} public bool Remove(string key)
{
if (string.IsNullOrEmpty(key))
{
return false;
} if (Exists(key))
{
_redisCache.Remove(key);
return true;
}
return false;
} public string GetValue(string key)
{
if (string.IsNullOrEmpty(key))
{
return null;
}
if (Exists(key))
{
return _redisCache.GetString(key);
}
return null;
} public bool Exists(string key)
{
if (string.IsNullOrEmpty(key))
{
return false;
}
return !string.IsNullOrEmpty(_redisCache.GetString(key)) ? true :false;
} }
四. 在 Startup.cs 文件中注入 Redis 和Memory. 这里啰嗦多几句, 因为同一个接口注入了多个实现, 那到调用的时候, 容器是怎么知道调用哪个类呢? 我这里是参考了
ASP.NET Core默认注入方式下如何注入多个实现(多种方式) ,
services.AddTransient<MemoryCacheService>(); //内存缓存认证注入
//注入Redis
services.AddSingleton(new RedisCacheService(new RedisCacheOptions()
{
InstanceName = Configuration.GetSection("Redis:InstanceName").Value,
Configuration= Configuration.GetSection("Redis:Connection").Value
}));
并在appsettings.json配置redis ,
"Redis": {
"Connection": "127.0.0.1:6379",
"InstanceName": "Redis:"
}
服务调用我使用了IServiceProvider .
调用如下:
五.总结
总算是写了一篇让自己看得懂一些的文章了. 行吧...算是一个小进步吧!
Net core 关于缓存的实现的更多相关文章
- Asp.Net Core 2.0 项目实战(8)Core下缓存操作、序列化操作、JSON操作等Helper集合类
本文目录 1. 前沿 2.CacheHelper基于Microsoft.Extensions.Caching.Memory封装 3.XmlHelper快速操作xml文档 4.Serializatio ...
- 讨论过后而引发对EF 6.x和EF Core查询缓存的思考
前言 最近将RabbitMQ正式封装引入到.NET Core 2.0项目当中,之前从未接触过是个高大上的东东跟着老大学习中,其中收获不少,本打算再看看RabbitMQ有时间写写,回来后和何镇汐大哥探讨 ...
- ASP.NET Core 开发-缓存(Caching)
ASP.NET Core 缓存Caching,.NET Core 中为我们提供了Caching 的组件. 目前Caching 组件提供了三种存储方式. Memory Redis SqlServer 学 ...
- .NET Core 的缓存篇之MemoryCache
前言 对于缓存我们都已经很熟悉了,缓存分为很多种,浏览器缓存.试图缓存.服务器缓存.数据库缓存等等一些,那今天我们先介绍一下视图缓存和MemoryCache内存缓存的概念和用法: 视图缓存 在老的版本 ...
- Asp.Net Core 轻松学-在.Net Core 使用缓存和配置依赖策略
前言 几乎在所有的应用程序中,缓存都是一个永恒的话题,恰当的使用缓存可以有效提高应用程序的性能:在某些业务场景下,使用缓存依赖会有很好的体验:在 Asp.Net Core 中,支持了多种缓存组 ...
- .NET Core MemoryCache缓存获取全部缓存键
在Core中不能使用原HttpRuntime.Cache缓存,改为MemoryCache(Microsoft.Extensions.Caching.Memory). 现MemoryCache新版为2. ...
- .net core响应缓存
按照官网资料操作无效,这里使用https://github.com/speige/AspNetCore.ResponseCaching.Extensions的扩展包 安装AspNetCore.Resp ...
- net core WebApi——缓存神器Redis
目录 前言 Redis 使用 RedisUtil 测试 小结 @ 前言 中秋过完不知不觉都已经快两周没动这个工程了,最近业务需要总算开始搞后台云服务了,果断直接net core搞起,在做的中间遇到了不 ...
- CacheManager Net Core Redis 缓存
using CacheManager.Core; using System; using System.Collections.Generic; using System.Text; namespac ...
随机推荐
- 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 二十一║Vue实战:开发环境搭建【详细版】
缘起 哈喽大家好,兜兜转转终于来到了Vue实战环节,前边的 6 篇关于Vue基础文章我刚刚简单看了看,感觉写的还是不行呀,不是很系统,所以大家可能看上去比较累,还是得抽时间去润润色,修改修改语句和样式 ...
- 宁撞金钟一下,不打破鼓三千,IT人要有志气,要进就进大的好的公司
最近我也在帮一些朋友面试,再结合自身的经验,发现了一个意料之外情理之中的事情:个别挣钱能力一般或规模比较小的公司,对候选人的要求普遍比一些大公司反而高,而且工作时间普遍会比一些好公司要长. 比如一个税 ...
- 如何在ASP.NET Core程序启动时运行异步任务(3)
原文:Running async tasks on app startup in ASP.NET Core (Part 3) 作者:Andrew Lock 译者:Lamond Lu 之前我写了两篇有关 ...
- springboot~添加新模块的方法
在springboot项目框架里,把一个项目两大模块,主项目main和测试项目test,而我们的测试项目根据功能又可以再分,比如可以有单元测试,集成测试,业务测试等等. 对于一个初学者来说,建立模块的 ...
- 学习ASP.NET Core Razor 编程系列十七——分组
学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...
- Docker进阶之七:管理应用程序数据
管理应用程序数据:Volume Docker提供三种不同的方式将数据从宿主机挂载到容器中:volumes,bind mounts和tmpfs. volumes:Docker管理宿主机文件系统的一部分( ...
- 从PRISM开始学WPF(三)Prism-Region-更新至Prism7.1
[7.1update]在开始前,我们先看下版本7.1中在本实例中的改动. 首先,项目文件中没有了Bootstrapper.cs,在上一篇的末尾,我们说过了,在7.1中,不见推荐使用Bootstrapp ...
- Flutter 即学即用系列博客——09 EventChannel 实现原生与 Flutter 通信(一)
前言 紧接着上一篇,这一篇我们讲一下原生怎么给 Flutter 发信号,即原生-> Flutter 还是通过 Flutter 官网的 Example 来讲解. 案例 接着上一次,这一次我们让原生 ...
- DSAPI 短域名服务
有时,需要将长域名转换为短域名,或是为了减少字符量,或是为了隐藏真实网址.在DSAPI中,集成了EPS-GS的短域名接口.该功能需要联接互联网,从EPS服务器获取. 代码 DSAPI.网络.短域名服务 ...
- ios键盘弹起 body的高度拉长,页面底部空白问题。ios软键盘将页面抵到上面后,关闭软键盘页面不回弹的问题。
js 监听ios手机键盘弹起和收起的事件 /* js 监听ios手机键盘弹起和收起的事件 */ document.body.addEventListener('focusin', () => { ...