当前位置:网站首页>. 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

  1. install StackChange.Redis nuget package
  2. Database connection information such as connection string is placed in appsettings in
  3. Writing database access tool classes
  4. 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();        }
  1. 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");}
原网站

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041155517018.html