这几天在面试,这个关于Redis缓存的博客一直没空写,今天总算有点时间了。

从很久很久之前,我就一直想学Redis了,反正看到各大招聘网上都要求Redis,不学就太落后了。

一开始我是按微软官网文档那样配置的,然后发现这也太简单了,不止配置简单,连使用都这么简单,简单得有点过分。如下图所示,它是基于IDistributedCache接口注入的

这么简单,怎么玩,我连判断某个key值存不存在都没办法。

当然了。绝对不是这么简单的。更高级的用法如下,要引入Microsoft.Extensions.Caching.StackExchangeRedis包

ConnectionMultiplexer connection = ConnectionMultiplexer.Connect("127.0.0.1:6379");
IDatabase cache = connection.GetDatabase();
cache.HashSet("key", "hashKey", "value");
cache.SetAdd("key2", "value");

那要怎么用在系统里呢,当然直接使用IDatabase也可以,但不够优雅,而且我还想通过配置文件,来决定是否启用Redis,如果不启用的话,就使用MemoryCache。非常好。想法有了。

先定义一个接口ICacheHelper,这是用来注入的接口,我暂时只定义了string类型跟hash类型的缓存方法

public interface ICacheHelper
{
bool Exists(string key); void Set<T>(string key, T value); T Get<T>(string key); void Delete(string key); void Expire(string key, DateTime dateTime);
void Expire(string key, TimeSpan timeSpan); void HashSet(string key, string hashKey, object hashValue);
T HashGet<T>(string key, string hashKey); bool HashExists(string key, string hashKey); void HashDelete(string key, string hashKey);
}

然后用Redis实现这个接口,RedisCacheHelper类

/// <summary>
/// Redis助手
/// </summary>
public class RedisCacheHelper : ICacheHelper
{
public IDatabase _cache; private ConnectionMultiplexer _connection; private readonly string _instance;
public RedisCacheHelper(RedisCacheOptions options, int database = )
{
_connection = ConnectionMultiplexer.Connect(options.Configuration);
_cache = _connection.GetDatabase(database);
_instance = options.InstanceName;
} public bool Exists(string key)
{
return _cache.KeyExists(_instance + key);
} public void Set<T>(string key, T value)
{
_cache.StringSet(_instance + key, CommonHelper.ObjectToJsonString(value));
} public T Get<T>(string key)
{
return CommonHelper.JsonStringToObject<T>(_cache.StringGet(_instance + key));
} public void Delete(string key)
{
_cache.KeyDelete(_instance + key);
} public void Expire(string key, DateTime dateTime)
{
_cache.KeyExpire(_instance + key, dateTime);
}
public void Expire(string key, TimeSpan timeSpan)
{
_cache.KeyExpire(_instance + key, timeSpan);
}
public void HashSet(string key, string hashKey, object hashValue)
{
string value = CommonHelper.ObjectToJsonString(hashValue);
_cache.HashSet(_instance + key, hashKey, value);
} public T HashGet<T>(string key, string hashKey)
{
var value = _cache.HashGet(_instance + key, hashKey);
return CommonHelper.JsonStringToObject<T>(value);
} public object HashGet(string key, string hashKey, Type type)
{
var value = _cache.HashGet(_instance + key, hashKey);
return CommonHelper.JsonStringToObject(value, type);
} public bool HashExists(string key, string hashKey)
{
return _cache.HashExists(_instance + key, hashKey);
} public void HashDelete(string key, string hashKey)
{
_cache.HashDelete(_instance + key, hashKey);
}
}

再用MemoryCache实现接口,MemoryCacheHelper类

/// <summary>
/// 缓存助手
/// </summary>
public class MemoryCacheHelper : ICacheHelper
{
private readonly IMemoryCache _cache;
public MemoryCacheHelper(IMemoryCache cache)
{
_cache = cache;
} public bool Exists(string key)
{
return _cache.TryGetValue(key, out _);
} public T Get<T>(string key)
{
return _cache.Get<T>(key);
} public void Delete(string key)
{
_cache.Remove(key);
} public void Set<T>(string key, T value)
{
_cache.Set(key, value);
}
public void Expire(string key, DateTime dateTime)
{
var value = _cache.Get(key);
_cache.Set(key, value, dateTime);
} public void Expire(string key, TimeSpan timeSpan)
{
var value = _cache.Get(key);
_cache.Set(key, value, timeSpan);
}
public void HashSet(string key, string hashKey, object hashValue)
{
var hash = _cache.Get<Dictionary<string, object>>(key);
if (hash.ContainsKey(hashKey))
{
hash[key] = hashValue;
}
else
{
hash.Add(hashKey, hashValue);
}
_cache.Set<Dictionary<string, object>>(key, hash);
} public T HashGet<T>(string key, string hashKey)
{
var hash = _cache.Get<Dictionary<string, object>>(key);
if (hash.ContainsKey(hashKey))
{
return (T)hash[hashKey];
}
else
{
return default(T);
}
} public bool HashExists(string key, string hashKey)
{
var hash = _cache.Get<Dictionary<string, object>>(key);
return hash.ContainsKey(hashKey);
} public void HashDelete(string key, string hashKey)
{
var hash = _cache.Get<Dictionary<string, object>>(key);
if (hash.ContainsKey(hashKey))
{
hash.Remove(hashKey);
}
}
}

实现类都有了,那现在就来实现根据配置值来决定是否使用Redis还是MemoryCache,先在appsettings.json里添加这个配置值,当Enable为false时,就不启用Redis,使用MemoryCache,Connection是Redis的连接字符串,InstanceName是缓存的前缀,Database是使用哪个数据库

"Redis": {
"Enable": true,
"Connection": "127.0.0.1:6379",
"InstanceName": "LessSharp:",
"Database":
}

再定义一个选项类 RedisOption

public class RedisOption
{
public bool Enable { get; set; }
public string Connection { get; set; }
public string InstanceName { get; set; }
public int Database { get; set; }
}

然后在Startup.cs类里的ConfigureServices里根据配置值进行注入

var RedisConfiguration = Configuration.GetSection("Redis");
services.Configure<RedisOption>(RedisConfiguration);
RedisOption redisOption = RedisConfiguration.Get<RedisOption>();
if (redisOption != null && redisOption.Enable)
{
var options = new RedisCacheOptions
{
InstanceName = redisOption.InstanceName,
Configuration = redisOption.Connection
};
var redis = new RedisCacheHelper(options, redisOption.Database);
services.AddSingleton(redis);
services.AddSingleton<ICacheHelper>(redis);
}
else
{
services.AddMemoryCache();
services.AddScoped<ICacheHelper, MemoryCacheHelper>();
}

OK,测试后完美

基于Asp.net Core 3.1实现的Redis及MemoryCache缓存助手CacheHelper的更多相关文章

  1. 用VSCode开发一个基于asp.net core 2.0/sql server linux(docker)/ng5/bs4的项目(1)

    最近使用vscode比较多. 学习了一下如何在mac上使用vscode开发asp.netcore项目. 这里是我写的关于vscode的一篇文章: https://www.cnblogs.com/cgz ...

  2. 如何基于asp.net core的Identity框架在mysql上作身份验证处理

    首先了解这个概念,我一开始也是理解和掌握基本的概念,再去做程序的开发.Identity框架是微软自己提供,基于.net core平台,可拓展.轻量 级.面向多个数据库的身份验证框架.IdentityS ...

  3. 基于Asp.Net Core的简单社区项目源代码开源

    2019年3月27号 更新版本 本项目基于 ASP.NET CORE 3.0+EF CORE 3.0开发 使用vs2019 +sqlserver 2017(数据库脚本最低支持sql server 20 ...

  4. AServer - 基于Asp.net core Kestrel的超迷你http服务器

    AServer是基于ASP.NET Core Kestrel封装的一个超迷你http服务器.它可以集成进你的Core程序里,用来快速的响应Http请求,而不需要集成整个ASP.NET Core MVC ...

  5. 基于ASP.NET Core 创建 Web API

    使用 Visual Studio 创建项目. 文件->新建->项目,选择创建 ASP.NET Core Web 应用程序. 基于 ASP.NET Core 2.0 ,选择API,身份验证选 ...

  6. 基于ASP.Net Core开发的一套通用后台框架

    基于ASP.Net Core开发一套通用后台框架 写在前面 这是本人在学习的过程中搭建学习的框架,如果对你有所帮助那再好不过.如果您有发现错误,请告知我,我会第一时间修改. 知其然,知其所以然,并非重 ...

  7. 基于ASP.NET Core 3.0快速搭建Razor Pages Web应用

    前言 虽然说学习新的开发框架是一项巨大的投资,但是作为一个开发人员,不断学习新的技术并快速上手是我们应该掌握的技能,甚至是一个.NET Framework开发人员,学习.NET Core 新框架可以更 ...

  8. [译]基于ASP.NET Core 3.0的ABP v0.21已发布

    基于ASP.NET Core 3.0的ABP v0.21已发布 在微软发布仅仅一个小时后, 基于ASP.NET Core 3.0的ABP v0.21也紧跟着发布了. v0.21没有新功能.它只是升级到 ...

  9. 基于Asp.Net Core,利用ZXing来生成二维码的一般流程

    本文主要介绍如何在.net环境下,基于Asp.Net Core,利用ZXing来生成二维码的一般操作.对二维码工作原理了解,详情见:https://blog.csdn.net/weixin_36191 ...

随机推荐

  1. git的工作区和暂存区

    目录 备注: 知识点 工作区(Working Directory) 版本库(Repository) 备注: 本文参考于廖雪峰老师的博客Git教程.依照其博客进行学习和记录,感谢其无私分享,也欢迎各位查 ...

  2. 那些非cmake生成的VTK工程存在的让人崩溃的坑

    由于cmake和IDE的一些编译选项不同,所以导致我们使用者需要多做一些事情.而且vtk官方也表示: If you are not using CMake to compile your code, ...

  3. This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary lo的解决办法

    创建存储过程时,出错信息: ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA ...

  4. wpf文字模糊

    wpf如果使用了DropShadowEffect,会导致文字模糊,可以在window上设置 this.UseLayoutRounding = true;解决此问题

  5. React Native 适配Android物理返回键,实现连续两次点击退出

    一直使用iPhone作为测试机开发,提交给测试同事Android版本后发现很多适配问题,其中一个非常明显的是,弹出一个modal后,点击Android的返回键,modal不会消失,直接navigati ...

  6. Android上传图片的两种方式

    参考:https://www.jianshu.com/p/f47943880cea

  7. 《HelloGitHub》第 52 期

    兴趣是最好的老师,HelloGitHub 就是帮你找到兴趣! 简介 分享 GitHub 上有趣.入门级的开源项目. 这是一个面向编程新手.热爱编程.对开源社区感兴趣 人群的月刊,月刊的内容包括:各种编 ...

  8. circle踢人(约瑟夫环) c++

    这里更新指针法,真的每句都是坑 (寥寥数十句,句句都是坑) // // Created by snnnow on 2020/4/12. //question:转圈,一共N个人,数到M的出列,求最后一个 ...

  9. Day13_Thymeleaf简介

    学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"乐优商城"获取视频和教程资料! b站在线视频 1.Th ...

  10. Python字符串更新

    Python字符串更新:截取字符串的某一部分 和 其他字符串进行拼接. 注:可以修改字符串的值,但修改的不是内存中的值,而是创建新的字符串. 1.使用字符串常量进行更新: # 使用字符串常量 strs ...