首页 技术 正文
技术 2022年11月12日
0 收藏 933 点赞 4,432 浏览 3170 个字

在很多项目中, 需要用到缓存,借鉴网上前辈们的一些经验,自己再进行总结简化了一些, 做出如下的缓存操作,其中包含内存缓存(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 关于缓存的实现

五.总结

总算是写了一篇让自己看得懂一些的文章了.   行吧…算是一个小进步吧!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,986
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,502
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,346
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,129
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,764
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,841