CSRedisCore的使用很简单,就需要实例化一个CSRedisClient(集群连接池)对象然后初始化一下RedisHelper就可以了,他的方法名与redis-cli基本保持一致。
首先需要本地安装redis: https://www.runoob.com/redis/redis-install.html
安装完redis,密码默认为空,这里我重新设置密码
1、启动redis:redis解压路径下运行
2、再打开一个新的命令窗口:启动客户端
config get requirepass 查看密码
控制台测试代码:
先按照NuGet程序包
static void Main(string[] args) { Service.RedisHelperService redisService = new Service.RedisHelperService(); // 初始化 CSRedis //var csredis = Service.RedisHelperService.Configuration["CsRedisConfig:DefaultConnectString"]; var csredis = "127.0.0.1:6379,password=123456,defaultDatabase=1,prefix="; RedisHelper.Initialization(new CSRedis.CSRedisClient(csredis)); Test(); Console.ReadKey(); } static void Test() { RedisHelper.Set("name", "李奎");//设置值。默认永不过期 //RedisHelper.SetAsync("name", "李奎");//异步操作 Console.WriteLine(RedisHelper.Get<String>("name")); RedisHelper.Set("time", DateTime.Now, 1); Console.WriteLine(RedisHelper.Get<DateTime>("time")); Thread.Sleep(1100); Console.WriteLine(RedisHelper.Get<DateTime>("time")); // 列表
//删除指定key的列表
RedisHelper.Del("list");
RedisHelper.RPush("list", "第一个元素"); RedisHelper.RPush("list", "第二个元素"); RedisHelper.RPush("list", "重复元素"); RedisHelper.RPush("list", "重复元素"); RedisHelper.RPush("list", "重复元素"); RedisHelper.LInsertBefore("list", "第二个元素", "我是新插入的第二个元素!"); Console.WriteLine($"list的长度为{RedisHelper.LLen("list")}"); //Console.WriteLine($"list的长度为{RedisHelper.LLenAsync("list")}");//异步 Console.WriteLine($"list的第二个元素为{RedisHelper.LIndex("list", 1)}"); var len = RedisHelper.LLen("list"); var listData = RedisHelper.LRange("list",0,len-1); //Console.WriteLine($"list的第二个元素为{RedisHelper.LIndexAsync("list",1)}");//异步 Console.WriteLine(" 列表成员begin"); foreach (var item in listData) { Console.WriteLine(item); } Console.WriteLine(" 列表成员end"); // 哈希 RedisHelper.HSet("person", "name", "233"); RedisHelper.HSet("person", "name", "233"); RedisHelper.HSet("person", "name", "233"); RedisHelper.HSet("person", "sex", "男"); RedisHelper.HSet("person", "age", "28"); RedisHelper.HSet("person", "adress", "hefei"); Console.WriteLine($"person这个哈希中的age为{RedisHelper.HGet<int>("person", "age")}"); //Console.WriteLine($"person这个哈希中的age为{RedisHelper.HGetAsync<int>("person", "age")}");//异步 //获取哈希所有的数量和成员 var hashLen = RedisHelper.HLen("person"); var hashdata = RedisHelper.HGetAll("person"); Console.WriteLine("哈希成员begin"); foreach (var item in hashdata) { Console.WriteLine(item.Key+"----"+item.Value); } Console.WriteLine("哈希成员end"); // 集合 RedisHelper.SAdd("students", "zhangsan", "lisi"); RedisHelper.SAdd("students", "likui", "古天乐"); RedisHelper.SAdd("students", "likui", "古天乐"); RedisHelper.SAdd("students", "likui", "古天乐"); RedisHelper.SAdd("students", "wangwu"); RedisHelper.SAdd("students", "zhaoliu"); Console.WriteLine($"students这个集合的大小为{RedisHelper.SCard("students")}"); Console.WriteLine($"students这个集合是否包含wagnwu:{RedisHelper.SIsMember("students", "wangwu")}"); //获取集合所有的数量和成员 var secData = RedisHelper.SMembers("students"); Console.WriteLine("集合成员begin"); foreach (var item in secData) { Console.WriteLine(item); } Console.WriteLine("集合成员end"); }
执行结果