.net core使用redis基于StackExchange.Redis
程序员文章站
2023-11-12 18:26:28
.net core使用redis基于stackexchange.redis教程,具体如下
一.添加引用包
stackexchange.redis
microsof...
.net core使用redis基于stackexchange.redis教程,具体如下
一.添加引用包
stackexchange.redis
microsoft.extensions.configuration
二.修改配置文件 appsettings.json
{ "redisconfig": { "redis_default": { "connection": "127.0.0.1: 6379", "instancename": "redis1: " }, "redis_6": { "connection": "127.0.0.1: 6379", "defaultdatabase": 6, "instancename": "redis2: " } } }
三.简单封装
redisclient.cs类
public class redisclient : idisposable { private iconfigurationroot _config; private concurrentdictionary<string, connectionmultiplexer> _connections; public redisclient(iconfigurationroot config) { _config = config; _connections = new concurrentdictionary<string, connectionmultiplexer>(); } /// <summary> /// 获取connectionmultiplexer /// </summary> /// <param name="redisconfig">redisconfig配置文件</param> /// <returns></returns> private connectionmultiplexer getconnect(iconfigurationsection redisconfig) { var redisinstancename = redisconfig["instancename"]; var connstr = redisconfig["connection"]; return _connections.getoradd(redisinstancename, p => connectionmultiplexer.connect(connstr)); } /// <summary> /// 检查入参数 /// </summary> /// <param name="configname">redisconfig配置文件中的 redis_default/redis_6 名称</param> /// <returns></returns> private iconfigurationsection checkeconfig(string configname) { iconfigurationsection redisconfig = _config.getsection("redisconfig").getsection(configname); if (redisconfig == null) { throw new argumentnullexception($"{configname}找不到对应的redisconfig配置!"); } var redisinstancename = redisconfig["instancename"]; var connstr = redisconfig["connection"]; if (string.isnullorempty(redisinstancename)) { throw new argumentnullexception($"{configname}找不到对应的instancename"); } if (string.isnullorempty(connstr)) { throw new argumentnullexception($"{configname}找不到对应的connection"); } return redisconfig; } /// <summary> /// 获取数据库 /// </summary> /// <param name="configname"></param> /// <param name="db">默认为0:优先代码的db配置,其次config中的配置</param> /// <returns></returns> public idatabase getdatabase(string configname = null, int? db = null) { int defaultdb = 0; iconfigurationsection redisconfig = checkeconfig(configname); if (db.hasvalue) { defaultdb = db.value; } else { var strdefalutdatabase = redisconfig["defaultdatabase"]; if (!string.isnullorempty(strdefalutdatabase) && int32.tryparse(strdefalutdatabase, out var intdefaultdatabase)) { defaultdb = intdefaultdatabase; } } return getconnect(redisconfig).getdatabase(defaultdb); } public iserver getserver(string configname = null, int endpointsindex = 0) { iconfigurationsection redisconfig = checkeconfig(configname); var connstr = redisconfig["connection"]; var confoption = configurationoptions.parse((string)connstr); return getconnect(redisconfig).getserver(confoption.endpoints[endpointsindex]); } public isubscriber getsubscriber(string configname = null) { iconfigurationsection redisconfig = checkeconfig(configname); return getconnect(redisconfig).getsubscriber(); } public void dispose() { if (_connections != null && _connections.count > 0) { foreach (var item in _connections.values) { item.close(); } } } }
因为redisclient 中使用了concurrentdictionary
所以,有了redisclientsingleton.cs类
public class redisclientsingleton { private static redisclient _redisclinet; private redisclientsingleton() { } private static object _lockobj = new object(); public static redisclient getinstance(iconfigurationroot config) { if (_redisclinet == null) { lock (_lockobj) { if (_redisclinet == null) { _redisclinet = new redisclient(config); } } } return _redisclinet; } }
四.测试
1、控制台程序
1)创建一个controller程序
2)添加引用包
microsoft.extensions.configuration
microsoft.extensions.configuration.json
3)添加appsettings.json
添加以下
"redisconfig": { "redis_default": { "connection": "127.0.0.1:6379", "instancename": "redis1:" }, "redis_6": { "connection": "127.0.0.1:6379", "defaultdatabase": 6, "instancename": "redis2:" } }
4)修改program类
①添加引用using microsoft.extensions.configuration;
②修改main
static void main(string[] args) { var builder = new configurationbuilder() .setbasepath(directory.getcurrentdirectory()) .addjsonfile("appsettings.json"); iconfigurationroot configuration = builder.build(); var redisclient = redisclientsingleton.getinstance(configuration); var redisdatabase = redisclient.getdatabase("redis_default"); redisdatabase.stringset("teststrkey", "teststrvalue"); }
③运行后可以看到
2、web程序
①创建一个core的mvc项目
②修改startup.cs文件
修改configureservices函数
public void configureservices(iservicecollection services) { ... services.addsingleton(configuration); ... }
其中的configuration是 public iconfigurationroot configuration { get; }
③创建一个testcontroller
在修改构造函数
simpleredisext _redisext;
public tokentranslatecontroller(iconfigurationroot config)//, iconfigurationroot config) { _config = config; idatabase redisdatabase = redisclientsingleton.getinstance(_config).getdatabase("redis_default"); } public string post(string value) { redisdatabase.stringset("testmvcredis", value); }
④运行value = testmvcredisvalue 后得到
随手写的,写得比较糙。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
ASP.NET Core中使用默认MVC路由的配置
-
ASP.NET Core 3.0 WebApi中使用Swagger生成API文档简介
-
abp(net core)+easyui+efcore实现仓储管理系统——使用 WEBAPI实现CURD (十五)
-
.Net Core中使用ref和Span
提高程序性能的实现代码 -
详解在ASP.NET Core 中使用Cookie中间件
-
.net core3.0部署Linux服务器 使用Docker容器和Nginx反代理教程
-
使用VS2019在WSL中开发调试.NET Core
-
使用VUE+iView+.Net Core上传图片的方法示例
-
asp.net core系列 72 Exceptionless使用介绍
-
asp.net core 使用 AccessControlHelper 控制访问权限