Example #1
0
 public static bool AddRangeToSortedSet(string setId, List <string> values, long score)
 {
     using (RedisClient client = GetClient())
     {
         return(client.AddRangeToSortedSet(setId, values, score));
     }
 }
Example #2
0
        /// <summary>
        /// Redis中set和zset
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            using (IRedisClient client = new RedisClient("127.0.0.1", 6379))
            {
                //清空当前数据库
                client.FlushDb();

                //set也是一个集合,自动去重的一个
                var libai = new User()
                {
                    ID = "01", Name = "李白"
                };
                client.AddItemToSet("setId", JsonConvert.SerializeObject(libai));
                client.AddItemToSet("setId", JsonConvert.SerializeObject(libai));
                client.AddItemToSet("setId", JsonConvert.SerializeObject(libai));

                var dufu = new User()
                {
                    ID = "02", Name = "杜甫"
                };
                client.AddItemToSet("setId", JsonConvert.SerializeObject(dufu));
                WriteLine(client.GetSetCount("setId"));


                WriteLine("******************");
                //批量
                client.AddRangeToSet("setIds", new List <string>()
                {
                    "01", "02", "03", "04", "05"
                });
                WriteLine(client.GetSetCount("setIds"));

                var sets = client.GetAllItemsFromSet("setIds");
                foreach (var item in sets)
                {
                    WriteLine(item);
                }

                WriteLine("******************");

                //随机获取set中的值
                for (int i = 0; i < 10; i++)
                {
                    WriteLine(client.GetRandomItemFromSet("setIds"));
                }

                WriteLine("******************");
                //随机删除,返回结果并删除
                for (int i = 0; i < 3; i++)
                {
                    WriteLine(client.PopItemFromSet("setIds"));
                }

                WriteLine("******************");

                client.AddRangeToSet("ids", new List <string>()
                {
                    "01", "02", "03", "04", "05"
                });
                //根据值去删除
                client.RemoveItemFromSet("ids", "01");
                client.RemoveItemFromSet("ids", "02");
                var lst = client.GetAllItemsFromSet("ids");
                foreach (var item in lst)
                {
                    WriteLine(item);
                }

                WriteLine("******************");

                //从原来的集合,移除值到新的一个集合总
                client.AddRangeToSet("one", new List <string>()
                {
                    "a", "b", "c", "d"
                });
                client.MoveBetweenSets("one", "two", "a");
                client.MoveBetweenSets("one", "two", "d");

                WriteLine("********交集**********");

                //交叉并补

                client.AddRangeToSet("a", new List <string>()
                {
                    "1", "3", "5"
                });
                client.AddRangeToSet("b", new List <string>()
                {
                    "2", "4", "5", "6"
                });
                //交集
                var jjlist = client.GetIntersectFromSets("a", "b");
                foreach (var item in jjlist)
                {
                    WriteLine(item);
                }

                WriteLine("********并集**********");
                //并集
                var bjlist = client.GetUnionFromSets("a", "b");
                foreach (var item in bjlist)
                {
                    WriteLine(item);
                }


                WriteLine("********差集**********");
                var cjlist = client.GetDifferencesFromSet("a", "b");
                foreach (var item in cjlist)
                {
                    WriteLine(item);
                }

                WriteLine("***zset***");

                //zset 自动去重,而且多一个权重,或者份数的字段,自动排序

                //当不给分数的时候,默认是最大值
                client.AddItemToSortedSet("zsetid", "a", 90);
                client.AddItemToSortedSet("zsetid", "b", 100);
                client.AddItemToSortedSet("zsetid", "c", 80);

                WriteLine("**************");
                //批量添加
                client.AddRangeToSortedSet("zsetidrange", new List <string>()
                {
                    "a", "b"
                }, 100);


                WriteLine("**************");
                //正序查询
                var zsetlist = client.GetAllItemsFromSortedSet("zsetid");
                foreach (var item in zsetlist)
                {
                    WriteLine(item);
                }

                //倒序查询
                var zsetDesc = client.GetAllItemsFromSortedSetDesc("zsetid");
                foreach (var item in zsetDesc)
                {
                    WriteLine(item);
                }


                WriteLine("**************");

                //根据下标获取
                var dic = client.GetRangeFromSortedSet("zsetid", 0, 1);
                foreach (var item in dic)
                {
                    WriteLine(item);
                }

                WriteLine("**************");
                //返回份数
                var dicScore = client.GetRangeWithScoresFromSortedSet("zsetid", 0, 1);
                foreach (var item in dicScore)
                {
                    WriteLine(item.Key + ":" + item.Value);
                }

                WriteLine("**************");

                //把集合中的交集放入到一个新的集合中
                client.AddItemToSortedSet("x", "a", 1);
                client.AddItemToSortedSet("x", "b", 2);
                client.AddItemToSortedSet("x", "c", 3);
                client.AddItemToSortedSet("x", "d", 4);

                client.AddItemToSortedSet("y", "c", 3);
                client.AddItemToSortedSet("y", "d", 4);

                client.AddItemToSortedSet("z", "c", 3);
                client.AddItemToSortedSet("z", "e", 9);

                var newDic = client.StoreIntersectFromSortedSets("newSet", "x", "y", "z");
                WriteLine(newDic);



                ReadLine();
            };
        }
        public static void Run(string host, int port)
        {
            using (RedisClient client = new RedisClient(host, port))
            {
                Console.WriteLine("Redis connected!");

                //删除当前数据库中的所有Key  默认删除的是select 0
                client.FlushDb();
                //删除所有数据库中的key
                client.FlushAll();

                string key = "redis_set";

                #region 添加键值 //就是自动去重,再带去重的功能

                //var litaibai = new UserInfo() { Id = 1, Name = "李太白" };

                //client.AddItemToSet(key, JsonConvert.SerializeObject(litaibai));
                //client.AddItemToSet(key, JsonConvert.SerializeObject(litaibai));
                //client.AddItemToSet(key, JsonConvert.SerializeObject(litaibai));
                //client.AddItemToSet(key, JsonConvert.SerializeObject(litaibai));

                //var lists = client.GetAllItemsFromSet(key);
                //Console.WriteLine("展示所有的值");
                //foreach (var item in lists)
                //{
                //    Console.WriteLine(item);
                //}

                #endregion

                #region 随机获取key集合中的一个值,获取当前setid中的所有值
                ////批量的去操作set 集合
                ////Console.WriteLine("set 开始了");
                //client.AddRangeToSet(key, new List<string>() { "001", "001", "002", "003", "003", "004" });
                ////当前setid中的值数量
                //Console.WriteLine(client.GetSetCount(key));

                ////随机获取key集合中的一个值 如果有需要取随机数也可以用
                //Console.WriteLine(client.GetRandomItemFromSet(key));
                ////获取当前setid中的所有值
                //var lists = client.GetAllItemsFromSet(key);
                //Console.WriteLine("展示所有的值");
                //foreach (var item in lists)
                //{
                //    Console.WriteLine(item);
                //}
                #endregion

                #region 随机删除key集合中的一个值
                //client.AddRangeToSet(key, new List<string>() { "001", "001", "002" });
                //////随机删除key集合中的一个值 返回当前删掉的这个值
                //Console.WriteLine("随机删除的值" + client.PopItemFromSet(key));
                //var lists = client.GetAllItemsFromSet(key);
                //Console.WriteLine("展示删除之后所有的值");
                //foreach (var item in lists)
                //{
                //    Console.WriteLine(item);
                //}
                #endregion

                #region 根据小key 删除  一个key 对象多个value
                //client.AddRangeToSet(key, new List<string>() { "001", "001", "002" });
                //client.RemoveItemFromSet(key, "001");
                //var lists = client.GetAllItemsFromSet(key);
                //Console.WriteLine("展示删除之后所有的值");
                //foreach (var item in lists)
                //{
                //    Console.WriteLine(item);
                //}
                #endregion

                #region 从fromkey集合中移除值为value的值,并把value添加到tokey集合中
                //client.AddRangeToSet("fromkey", new List<string>() { "003", "001", "002", "004" });
                //client.AddRangeToSet("tokey", new List<string>() { "001", "002" });
                ////从fromkey 中把元素004 剪切到tokey 集合中去
                //client.MoveBetweenSets("fromkey", "tokey", "004");
                //Console.WriteLine("fromkey data ~~~~~~");
                //foreach (var item in client.GetAllItemsFromSet("fromkey"))
                //{
                //    Console.WriteLine(item);
                //}

                //Console.WriteLine("tokey data ~~~~~~");
                //foreach (var item in client.GetAllItemsFromSet("tokey"))
                //{
                //    Console.WriteLine(item);
                //}
                #endregion

                #region 并集  把两个集合合并起来,然后去重

                //client.AddRangeToSet("keyone", new List<string>() { "001", "002", "003", "004" });
                //client.AddRangeToSet("keytwo", new List<string>() { "001", "002", "005" });
                //var unionlist = client.GetUnionFromSets("keyone", "keytwo");
                //Console.WriteLine("返回并集结果");
                //foreach (var item in unionlist)
                //{
                //    Console.WriteLine(item);
                //}
                ////把 keyone 和keytwo 并集结果存放到newkey 集合中
                //client.StoreUnionFromSets("newkey", "keyone", "keytwo");
                //Console.WriteLine("返回并集结果的新集合数据");
                //foreach (var item in client.GetAllItemsFromSet("newkey"))
                //{
                //    Console.WriteLine(item);
                //}
                #endregion

                #region 交集 获取两个集合中共同存在的元素
                //client.AddRangeToSet("keyone", new List<string>() { "001", "002", "003", "004" });
                //client.AddRangeToSet("keytwo", new List<string>() { "001", "002", "005" });
                //var Intersectlist = client.GetIntersectFromSets("keyone", "keytwo");
                //Console.WriteLine("交集的结果");
                //foreach (var item in Intersectlist)
                //{
                //    Console.WriteLine(item);
                //}
                ////把 keyone 和keytwo 交集结果存放到newkey 集合中
                //client.StoreIntersectFromSets("newkey", "keyone", "keytwo");
                //Console.WriteLine("返回交集结果的新集合数据");
                //foreach (var item in client.GetAllItemsFromSet("newkey"))
                //{
                //    Console.WriteLine(item);
                //}
                #endregion

                #region  sorted set
                string zsett_key = "daniel_zset";
                //添加一个kye 如果有相同的值,则会替换(覆盖)进去,不会因为分数保留
                client.AddItemToSortedSet(zsett_key, "cc", 33);
                client.AddItemToSortedSet(zsett_key, "cc", 44);
                client.AddItemToSortedSet(zsett_key, "cc", 22);
                Console.WriteLine("ok");
                //获取当前value的结果
                Console.WriteLine(client.GetItemIndexInSortedSet(zsett_key, "cc"));
                //批量操作多个key ,给多个key 赋值1
                client.AddRangeToSortedSet(zsett_key, new List <string>()
                {
                    "a", "b"
                }, 1);

                foreach (var item in client.GetAllItemsFromSortedSet(zsett_key))
                {
                    Console.WriteLine(item);
                }

                client.AddItemToSortedSet("蜀国", "刘备", 5);
                client.AddItemToSortedSet("蜀国", "关羽", 2);
                client.AddItemToSortedSet("蜀国", "张飞", 3);
                client.AddItemToSortedSet("魏国", "刘备", 5);
                client.AddItemToSortedSet("魏国", "关羽", 2);
                client.AddItemToSortedSet("蜀国", "张飞", 3);

                ////获取 key为蜀国的下标0,到2
                IDictionary <String, double> Dic = client.GetRangeWithScoresFromSortedSet("蜀国", 0, 2);

                foreach (var r in Dic)
                {
                    Console.WriteLine(r.Key + ":" + r.Value);
                }

                //var DicString = client.StoreIntersectFromSortedSets("2", "蜀国", "魏国");
                //var ss = client.GetAllItemsFromSortedSet("2");
                //foreach (var r in ss)
                //{
                //    Console.WriteLine(r);
                //}
                #endregion
            }
        }
 public bool AddRangeToSortedSet(string setId, List <string> values, double score)
 {
     return(redisClient.AddRangeToSortedSet(setId, values, score));
 }
 /// <summary>
 /// 为key添加values集合,values集合中每个value的分数设置为score
 /// </summary>
 public bool AddRangeToSortedSet(string key, List <string> values, long score)
 {
     return(RedisClient.AddRangeToSortedSet(key, values, score));
 }