当前位置:网站首页>.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");}
边栏推荐
- 光环效应——谁说头上有光的就算英雄
- 【AI系统前沿动态第40期】Hinton:我的深度学习生涯与研究心法;Google辟谣放弃TensorFlow;封神框架正式开源
- CA:用于移动端的高效坐标注意力机制 | CVPR 2021
- 诸神黄昏时代的对比学习
- 提高MySQL深分页查询效率的三种方案
- Apache server access log access Log settings
- DGraph: 大规模动态图数据集
- Comprehensive evaluation of modular note taking software: craft, notation, flowus
- Implementation mode and technical principle of MT4 cross platform merchandising system (API merchandising, EA merchandising, nj4x Merchandising)
- MDK在头文件中使用预编译器时,#ifdef 无效的问题
猜你喜欢
Etcd storage, watch and expiration mechanism
Comparative study of the gods in the twilight Era
ASP.NET Core入门一
Concepts and theories related to distributed transactions
美团·阿里关于多模态召回的应用实践
CTF competition problem solution STM32 reverse introduction
runc hang 导致 Kubernetes 节点 NotReady
MDK在头文件中使用预编译器时,#ifdef 无效的问题
提高MySQL深分页查询效率的三种方案
8个扩展子包!RecBole推出2.0!
随机推荐
CANN算子:利用迭代器高效实现Tensor数据切割分块处理
Solution: how to delete the information of Jack in two tables with delete in one statement in Oracle
Transformer principle and code elaboration (tensorflow)
Read the BGP agreement in 6 minutes.
Annual comprehensive analysis of China's mobile reading market in 2022
Detailed explanation of mt4api documentary and foreign exchange API documentary interfaces
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
Concepts and theories related to distributed transactions
面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
[leetcode] 96 and 95 (how to calculate all legal BST)
模块化笔记软件综合评测:Craft、Notion、FlowUs
使用Scrcpy投屏
干货整理!ERP在制造业的发展趋势如何,看这一篇就够了
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
数据库锁表?别慌,本文教你如何解决
8 expansion sub packages! Recbole launches 2.0!
mysql三级分销代理关系存储
老掉牙的 synchronized 锁优化,一次给你讲清楚!
How real-time cloud interaction helps the development of education industry