当前位置:网站首页>.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");}边栏推荐
- 老掉牙的 synchronized 锁优化,一次给你讲清楚!
- Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
- Implementation mode and technical principle of MT4 cross platform merchandising system (API merchandising, EA merchandising, nj4x Merchandising)
- Vit (vision transformer) principle and code elaboration
- AI 绘画极简教程
- Comparative study of the gods in the twilight Era
- 面向个性化需求的在线云数据库混合调优系统 | SIGMOD 2022入选论文解读
- Efficient! Build FTP working environment with virtual users
- Dgraph: large scale dynamic graph dataset
- CVPR 2022 | TransFusion:用Transformer进行3D目标检测的激光雷达-相机融合
猜你喜欢

Is the outdoor LED screen waterproof?

SQL statement syntax error in test SQL statement deletion in eclipse linked database

Annual comprehensive analysis of China's mobile reading market in 2022

Alibaba cloud award winning experience: build a highly available system with polardb-x

Talk about the design and implementation logic of payment process
![[cloud native | kubernetes] in depth understanding of ingress (12)](/img/34/67eae1e5df89bb0a356a1c29a5e007.png)
[cloud native | kubernetes] in depth understanding of ingress (12)

Golang sets the small details of goproxy proxy proxy, which is applicable to go module download timeout and Alibaba cloud image go module download timeout

A taste of node JS (V), detailed explanation of express module

8个扩展子包!RecBole推出2.0!

PostgreSQL 9.1 soaring Road
随机推荐
Excuse me, have you encountered this situation? CDC 1.4 cannot use timestamp when connecting to MySQL 5.7
SQL语言
7、 Software package management
Dgraph: large scale dynamic graph dataset
iptables基础及Samba配置举例
Full arrangement (medium difficulty)
Practice: fabric user certificate revocation operation process
Transformer principle and code elaboration (tensorflow)
Using scrcpy projection
面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
Reinforcement learning - learning notes 1 | basic concepts
CVPR 2022 | TransFusion:用Transformer进行3D目标检测的激光雷达-相机融合
A data person understands and deepens the domain model
面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
模块化笔记软件综合评测:Craft、Notion、FlowUs
6 分钟看完 BGP 协议。
Apache server access log access Log settings
数据库锁表?别慌,本文教你如何解决
WPF double slider control and forced capture of mouse event focus
「小技巧」给Seurat对象瘦瘦身