当前位置:网站首页>. Net using redis
. Net using redis
2022-07-04 13:41:00 【Hua Weiyun】
Redis The publish and subscribe function of
redis To configure
Redis Configuration explanation ( Remember to restart after operation Redis service )
Allow remote access
1. Modify two configuration files :redis.windows.conf and redis.windows-service.conf
2. Comment out bind 127.0.0.1
3. Turn off protection mode protected-mode no
password
1. Modify two configuration files :redis.windows.conf and redis.windows-service.conf
2. Turn on requirepass yourPassword
ASP.NET Core Use redis
- install StackChange.Redis nuget package
- Database connection information such as connection string is placed in appsettings in
- Writing database access tool classes
- startup Registration service
Here is the manual parsing of configuration information , Can pass softjson Auto parse
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 Call in
ASP.NET CORE Use in redis cache
install nuget package
- StackExchange.Redis.Extensions.Core
- StackExchange.Redis.Extensions.AspNetCore
- StackExchange.Redis.Extensions.Newtonsoft
appsettings Profile add
"Redis": { "Password": "123456", "AllowAdmin": true, "Ssl": false, "ConnectTimeout": 6000, "ConnectRetry": 2, "Database": 0, "Hosts": [ { "Host": "127.0.0.1", "Port": "6379" } ] }
startup Registration service
public void ConfigureServices(IServiceCollection services){ var redisConfiguration = Configuration.GetSection("Redis").Get<RedisConfiguration>(); services.AddControllersWithViews(); services.AddStackExchangeRedisExtensions<NewtonsoftSerializer>(redisConfiguration);}
With services
private readonly RedisCacheClient _redisCacheClient; public TestController(IRedis redisCacheClient) { _redisCacheClient = redisCacheClient; }
Use
public async Task<IActionResult> Index4() { var productdata = await _redisCacheClient.Db0.GetAsync<Product>("Product"); return View(); }
.net in redis Publish subscription
Mainly use code : subscribe
using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("127.0.0.1:6379")){ ISubscriber sub = redis.GetSubscriber(); // The subscription is called messages The passage of sub.Subscribe("messages", (channel, message) => { // Output received messages Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] {message}"); }); Console.WriteLine(" Subscribed messages"); Console.ReadKey();}
Release
// Create connection using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("127.0.0.1:6379")){ ISubscriber sub = redis.GetSubscriber(); Console.WriteLine(" Please enter any character , Input exit sign out "); string input; do { input = Console.ReadLine(); sub.Publish("messages", input); } while (input != "exit");}
边栏推荐
- 求解:在oracle中如何用一条语句用delete删除两个表中jack的信息
- 一个数据人对领域模型理解与深入
- CA: efficient coordinate attention mechanism for mobile terminals | CVPR 2021
- When MDK uses precompiler in header file, ifdef is invalid
- 使用Scrcpy投屏
- Etcd 存储,Watch 以及过期机制
- XML入门三
- 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)
- 面向个性化需求的在线云数据库混合调优系统 | SIGMOD 2022入选论文解读
- Using nsproxy to forward messages
猜你喜欢
Valentine's Day confession code
Practice: fabric user certificate revocation operation process
When MDK uses precompiler in header file, ifdef is invalid
实时云交互如何助力教育行业发展
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)
Reptile exercises (I)
三星量产3纳米产品引台媒关注:能否短期提高投入产出率是与台积电竞争关键
[cloud native | kubernetes] in depth understanding of ingress (12)
强化学习-学习笔记1 | 基础概念
《预训练周刊》第52期:屏蔽视觉预训练、目标导向对话
随机推荐
Annual comprehensive analysis of China's mobile reading market in 2022
从0到1建设智能灰度数据体系:以vivo游戏中心为例
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
[cloud native | kubernetes] in depth understanding of ingress (12)
Three schemes to improve the efficiency of MySQL deep paging query
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
Dgraph: large scale dynamic graph dataset
Simple understanding of binary search
When MDK uses precompiler in header file, ifdef is invalid
洞见科技解决方案总监薛婧:联邦学习助力数据要素安全流通
阿里云有奖体验:用PolarDB-X搭建一个高可用系统
[AI system frontier dynamics, issue 40] Hinton: my deep learning career and research mind method; Google refutes rumors and gives up tensorflow; The apotheosis framework is officially open source
DGraph: 大规模动态图数据集
Zhongang Mining: in order to ensure sufficient supply of fluorite, it is imperative to open source and save flow
Reading cognitive Awakening
SQL statement syntax error in test SQL statement deletion in eclipse linked database
mysql三级分销代理关系存储
数据库锁表?别慌,本文教你如何解决
PostgreSQL 9.1 soaring Road
Etcd 存储,Watch 以及过期机制