一. 整体介绍

1. 说明

 CSRedis 是 redis.io 官方推荐库,支持 redis-trib集群、哨兵、私有分区与连接池管理技术,简易 RedisHelper 静态类, 它主要又两个程序集。

 (1).CSRedisCore:主库,实现对接redis各种功能

 (2).Caching.CSRedis:分布式缓存 CSRedisCore 实现 Microsoft.Extensions.Caching

相关地址如下:

 GitHub地址:https://github.com/2881099/csredis

 Nuget地址:https://www.nuget.org/packages/CSRedisCore/

2. 主要特点

 (1).调用方法的时候,可以使用CSRedisClient实例化的对象,也可以使用全局类RedisHelper(需要Initialization初始化一下)

注:无论是CSRedisClient实例化的对象还是RedisHelper调用的方法和Redis自身cli指令名字完全相同,这一点非常好!!

 (2).官方推荐配置:CSRedisClient is singleton, RedisHelper static class is recommended (CSRedisClient推荐配置单例模式,RedisHelper推荐静态)

 (3).支持geo类型(>=3.2)、stream类型(>=5.0)

 (4).支持主从、哨兵、cluster

 

二. 如何集成

1. 控制台中使用

 前提:通过Nuget安装程序集:CSRedisCore

(1).用法1:直接实例化CSRedisClient进行使用

(2).用法2:初始化帮助类RedisHelper帮助类进行使用

代码分享:

            {
                //用法1-CSRedisClient实例化的对象(生产环境中把CSRedisClient写成单例类) 
                var rds = new CSRedis.CSRedisClient("119.45.174.xx:6379,password=123456,defaultDatabase=0");
                rds.Set("name1", "ypf");
                var result1 = rds.Get("name1");
                Console.WriteLine($"name1={result1}");

                //用法2-RedisHelper帮助类
                RedisHelper.Initialization(new CSRedis.CSRedisClient("119.45.174.xx:6379,password=123456,defaultDatabase=0"));
                RedisHelper.Set("name2", "ypf2");
                var result2 = RedisHelper.Get("name2");
                Console.WriteLine($"name2={result2}");
            }

ps:CSRedisClient链接字符串的说明

第十四节:基于CSRedisCore程序集调用redis各个功能详解

2. CoreMVC中使用

 前提:通过Nuget安装程序集:CSRedisCore,同样有两种用法

(1).在ConfigureSevice实例化CSRedisClient,并注册成单例模式;然后实例化RedisHelper帮助类

(2).在action如果使用CSRedisClient,需要注入后再使用

(3).再action如果使用RedisHelper帮助类,可以直接使用,不需要注册

代码分享:

配置文件

 "RedisStr": "119.45.174.xx:6379,password=123456,defaultDatabase=0"

ConfigureService:

 public void ConfigureServices(IServiceCollection services)
 {
            //1. 集成Redis的两种方式
            {
                //用法1-CSRedisClient实例化的对象
                var rds = new CSRedis.CSRedisClient(Configuration["RedisStr"]);
                services.AddSingleton(rds);   //注册成全局单例

                //用法2-RedisHelper帮助类
                RedisHelper.Initialization(rds);
            }
            services.AddControllersWithViews();
 }

调用:

    public class HomeController : Controller
    {

        private CSRedisClient _csredis;
        public HomeController(CSRedisClient csredis)
        {
            this._csredis = csredis;    
        }
        public IActionResult Index()
        {

            #region 01-如何集成
            //{
            //    //1. 用法1,需要注入
            //    _csredis.Set("name1", "ypf11");
            //    var result1 = _csredis.Get("name1");
            //    Console.WriteLine($"name1={result1}");


            //    //2. 用法2,直接使用RedisHelp类即可
            //    RedisHelper.Set("name2", "ypf22");
            //    var result2 = RedisHelper.Get("name2");
            //    Console.WriteLine($"name2={result2}");
            //}
            #endregion

            return View();
        }    
    }

3. CoreMVC缓存集成

 前提:通过Nuget安装程序集【Caching.CSRedis】,注册方式和CoreMVC的默认分布式缓存的方式有点区别,这里更加简单粗暴,直接注册IDistributedCache对象即可

(1).在ConfigureService中注册IDistributedCache单例对象

(2).在控制器中注入进行使用即可,包含的方法默认修改的模式相同

代码分享

ConfigureService

 //2. 注册基于Redis的分布式缓存
            {
                var csredis = new CSRedis.CSRedisClient(Configuration["RedisStr"]);
                services.AddSingleton<IDistributedCache>(new Microsoft.Extensions.Caching.Redis.CSRedisCache(csredis));

                //集群模式
                //              var csredis = new CSRedis.CSRedisClient(null,
                //              "127.0.0.1:6371,pass=123,defaultDatabase=11,poolsize=10,ssl=false,writeBuffer=10240,prefix=key前辍",
                //              "127.0.0.1:6372,pass=123,defaultDatabase=12,poolsize=11,ssl=false,writeBuffer=10240,prefix=key前辍",
                //              "127.0.0.1:6373,pass=123,defaultDatabase=13,poolsize=12,ssl=false,writeBuffer=10240,prefix=key前辍",
                //              "127.0.0.1:6374,pass=123,defaultDatabase=14,poolsize=13,ssl=false,writeBuffer=10240,prefix=key前辍");
                //              services.AddSingleton<IDistributedCache>(new Microsoft.Extensions.Caching.Redis.CSRedisCache(csredis));
            }
View Code

相关文章:

  • 2021-10-13
  • 2021-08-09
  • 2021-12-13
  • 2021-10-27
  • 2021-05-28
  • 2022-12-23
  • 2021-06-06
猜你喜欢
  • 2022-12-23
  • 2021-05-18
  • 2021-09-19
  • 2021-12-06
  • 2021-05-07
  • 2022-03-03
相关资源
相似解决方案