当前位置:网站首页>.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三级分销代理关系存储
- Comprehensive evaluation of modular note taking software: craft, notation, flowus
- 洞见科技解决方案总监薛婧:联邦学习助力数据要素安全流通
- Efficient! Build FTP working environment with virtual users
- Transformer principle and code elaboration (tensorflow)
- Backgroundworker usage example
- Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
- WPF double slider control and forced capture of mouse event focus
- XILINX/system-controller-c/BoardUI/无法连接开发板,任意操作后卡死的解决办法
- Will the concept of "being integrated" become a new inflection point of the information and innovation industry?
猜你喜欢
Practice: fabric user certificate revocation operation process
eclipse链接数据库中测试SQL语句删除出现SQL语句语法错误
Runc hang causes the kubernetes node notready
Talk about the design and implementation logic of payment process
Etcd storage, watch and expiration mechanism
求解:在oracle中如何用一条语句用delete删除两个表中jack的信息
ASP.NET Core入门一
比量子化学方法快六个数量级,一种基于绝热状态的绝热人工神经网络方法,可加速对偶氮苯衍生物及此类分子的模拟
Master the use of auto analyze in data warehouse
It is six orders of magnitude faster than the quantum chemical method. An adiabatic artificial neural network method based on adiabatic state can accelerate the simulation of dual nitrogen benzene der
随机推荐
Runc hang causes the kubernetes node notready
二分查找的简单理解
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
After the game starts, you will be prompted to install HMS core. Click Cancel, and you will not be prompted to install HMS core again (initialization failure returns 907135003)
FS4056 800mA充电ic 国产快充电源ic
Can Console. Clear be used to only clear a line instead of whole console?
SQL语言
Etcd 存储,Watch 以及过期机制
Reptile exercises (I)
Iptables foundation and Samba configuration examples
Reading cognitive Awakening
Will the concept of "being integrated" become a new inflection point of the information and innovation industry?
Configure WebDAV server on Apache
A data person understands and deepens the domain model
Dgraph: large scale dynamic graph dataset
提高MySQL深分页查询效率的三种方案
The old-fashioned synchronized lock optimization will make it clear to you at once!
阿里云有奖体验:用PolarDB-X搭建一个高可用系统
CANN算子:利用迭代器高效实现Tensor数据切割分块处理
一文掌握数仓中auto analyze的使用