.net core使用redis基于StackExchange.Redis
.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开发之webform图片水印和图片验证码的实现方法
下一篇: 程序中常用的种代码
推荐阅读
-
ASP.Net Core中使用枚举类而不是枚举的方法
-
Asp.net Core中如何使用中间件来管理websocket
-
ASP.NET Core Kestrel 中使用 HTTPS (SSL)
-
ASP.NET Core部署前期准备 使用Hyper-V安装Ubuntu Server 16.10
-
使用 xUnit 编写 ASP.NET Core WebAPI单元测试
-
Asp.net core WebApi 使用Swagger生成帮助页实例
-
.net core使用redis基于StackExchange.Redis
-
ASP.NET core Web中使用appsettings.json配置文件的方法
-
ASP.NET Core基于微软微服务eShopOnContainer事件总线EventBus的实现
-
使用.Net Core + Vue + IdentityServer4 + Ocelot 实现一个简单的DEMO +源码