当前位置:网站首页>.NET 使用 redis
.NET 使用 redis
2022-07-04 12:42:00 【华为云】
Redis 的发布订阅功能
redis 配置
Redis配置讲解(操作完记得重启Redis服务)
允许远程访问
1.修改两个配置文件:redis.windows.conf 和 redis.windows-service.conf
2.注释掉 bind 127.0.0.1
3.关闭保护模式 protected-mode no
密码
1.修改两个配置文件:redis.windows.conf 和 redis.windows-service.conf
2.开启 requirepass yourPassword
ASP.NET Core 使用redis
- 安装StackChange.Redis nuget包
- 连接字符串等数据库连接信息放在appsettings 中
- 编写数据库访问工具类
- startup注册服务
这里是手动解析配置信息,可以通过softjson自动解析
public void ConfigureServices(IServiceCollection services) { var section = Configuration.GetSection("Redis:Default"); string _connectionString = section.GetSection("Connection").Value; string _instanceName = section.GetSection("InstanceName").Value; int _defaultDB = int.Parse(section.GetSection("DefaultDB").Value ?? "0"); services.AddSingleton(new RedisHelper(_connectionString, _instanceName, _defaultDB)); services.AddControllers(); }
- controller 中调用
ASP.NET CORE 中使用redis缓存
安装nuget 包
- StackExchange.Redis.Extensions.Core
- StackExchange.Redis.Extensions.AspNetCore
- StackExchange.Redis.Extensions.Newtonsoft
appsettings配置文件添加
"Redis": { "Password": "123456", "AllowAdmin": true, "Ssl": false, "ConnectTimeout": 6000, "ConnectRetry": 2, "Database": 0, "Hosts": [ { "Host": "127.0.0.1", "Port": "6379" } ] }
startup 注册服务
public void ConfigureServices(IServiceCollection services){ var redisConfiguration = Configuration.GetSection("Redis").Get<RedisConfiguration>(); services.AddControllersWithViews(); services.AddStackExchangeRedisExtensions<NewtonsoftSerializer>(redisConfiguration);}
服务注入
private readonly RedisCacheClient _redisCacheClient; public TestController(IRedis redisCacheClient) { _redisCacheClient = redisCacheClient; }
使用
public async Task<IActionResult> Index4() { var productdata = await _redisCacheClient.Db0.GetAsync<Product>("Product"); return View(); }
.net 中 redis 的发布订阅
主要使用代码:订阅
using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("127.0.0.1:6379")){ ISubscriber sub = redis.GetSubscriber(); //订阅名为 messages 的通道 sub.Subscribe("messages", (channel, message) => { //输出收到的消息 Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] {message}"); }); Console.WriteLine("已订阅 messages"); Console.ReadKey();}
发布
//创建连接using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("127.0.0.1:6379")){ ISubscriber sub = redis.GetSubscriber(); Console.WriteLine("请输入任意字符,输入exit退出"); string input; do { input = Console.ReadLine(); sub.Publish("messages", input); } while (input != "exit");}
边栏推荐
- 提高MySQL深分页查询效率的三种方案
- 洞见科技解决方案总监薛婧:联邦学习助力数据要素安全流通
- SQL语言
- Comparative study of the gods in the twilight Era
- [FAQ] summary of common causes and solutions of Huawei account service error 907135701
- 【云原生 | Kubernetes篇】深入了解Ingress(十二)
- Concepts and theories related to distributed transactions
- Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
- Dgraph: large scale dynamic graph dataset
- PostgreSQL 9.1 soaring Road
猜你喜欢
8个扩展子包!RecBole推出2.0!
Transformer principle and code elaboration (tensorflow)
提高MySQL深分页查询效率的三种方案
三星量产3纳米产品引台媒关注:能否短期提高投入产出率是与台积电竞争关键
[cloud native | kubernetes] in depth understanding of ingress (12)
Database lock table? Don't panic, this article teaches you how to solve it
一个数据人对领域模型理解与深入
求解:在oracle中如何用一条语句用delete删除两个表中jack的信息
面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
阿里云有奖体验:用PolarDB-X搭建一个高可用系统
随机推荐
爬虫练习题(一)
Dry goods sorting! How about the development trend of ERP in the manufacturing industry? It's enough to read this article
jsonp
在 Apache 上配置 WebDAV 服务器
Use fail2ban to prevent password attempts
Practice: fabric user certificate revocation operation process
ISO 27001 Information Security Management System Certification
Comparative study of the gods in the twilight Era
AI 绘画极简教程
求解:在oracle中如何用一条语句用delete删除两个表中jack的信息
[leetcode] 96 and 95 (how to calculate all legal BST)
使用Scrcpy投屏
"Tips" to slim down Seurat objects
When MDK uses precompiler in header file, ifdef is invalid
A taste of node JS (V), detailed explanation of express module
高效!用虚拟用户搭建FTP工作环境
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
Definition of cognition
MDK在头文件中使用预编译器时,#ifdef 无效的问题
Configure WebDAV server on Apache