• ASP.NET Core 使用redis


    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

    1. 安装StackChange.Redis nuget包
    2. 连接字符串等数据库连接信息放在appsettings 中
    3. 编写数据库访问工具类
    4. 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();
    
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1. 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"
        }
      ]
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    startup 注册服务

    public void ConfigureServices(IServiceCollection services)
    {
        var redisConfiguration = Configuration.GetSection("Redis").Get<RedisConfiguration>();
        services.AddControllersWithViews();
        services.AddStackExchangeRedisExtensions<NewtonsoftSerializer>(redisConfiguration);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    服务注入

            private readonly RedisCacheClient _redisCacheClient;
    
            public TestController(IRedis redisCacheClient)
            {
                _redisCacheClient = redisCacheClient;
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    使用

            public async Task<IActionResult> Index4()
            {
                var productdata = await _redisCacheClient.Db0.GetAsync<Product>("Product");
                return View();
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    .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();
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    发布

    //创建连接
    
    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");
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
  • 相关阅读:
    四、云计算-国产-华为-运维、灾备和迁移+HCIE Cloud相关知识点+笔试题库
    高级前端进阶(五)
    返璞归真:命令行下逛园子,发布博客园 CLI 预览版
    C语言利用计算机找系统的最小通路集的算法
    Failed to configure a DataSource: ‘url‘ attribute is not specified and no em
    如何使用ASO优化来提高应用的安装率
    【Linux】在Ubuntu下安装Zotero
    数据库改造(Oracle->PostgreSQL)
    支付宝小程序关键词优化:引领数字商业的未来
    银行智能运维探索:打造通用指标趋势预测模型
  • 原文地址:https://blog.csdn.net/weixin_46178278/article/details/125593153