Example #1
0
        private static void SetDemo()
        {
            using (
                var client =
                    new RedisClient(
                        "Qg7EG3uqYBDZBLMPdjkWblKNRhjCb7e9Lo60RJbTHd8=@customer-C.redis.cache.windows.net?ssl=true"))
            {
                const string key = "urn:Set:Customer";
                client.AddRangeToSet(key, new List <string> {
                    "abc", "cde", "def"
                });
                client.AddRangeToSet(key, new List <string> {
                    "abc", "cdsae", "def"
                });
                client.AddItemToSet(key, "One");
            }

            using (
                var client =
                    new RedisClient(
                        "Qg7EG3uqYBDZBLMPdjkWblKNRhjCb7e9Lo60RJbTHd8=@customer-C.redis.cache.windows.net?ssl=true"))
            {
                const string key   = "urn:Set:Customer";
                var          items = client.GetAllItemsFromSet(key);
                foreach (var item in items)
                {
                    Console.WriteLine(item);
                }
            }

            using (
                var client =
                    new RedisClient(
                        "Qg7EG3uqYBDZBLMPdjkWblKNRhjCb7e9Lo60RJbTHd8=@customer-C.redis.cache.windows.net?ssl=true"))
            {
                Console.ReadKey();
                const string key   = "urn:Set:Customer";
                var          items = client.GetAllItemsFromSet(key);
                foreach (var item in items)
                {
                    client.RemoveItemFromSet(key, item);
                }
            }

            using (
                var client =
                    new RedisClient(
                        "Qg7EG3uqYBDZBLMPdjkWblKNRhjCb7e9Lo60RJbTHd8=@customer-C.redis.cache.windows.net?ssl=true"))
            {
                const string key = "urn:Set:Customer";
                client.AddRangeToSet(key, new List <string> {
                    "abcasd", "cadsdsae", "de12f"
                });
                var items = client.GetAllItemsFromSet(key);
                foreach (var item in items)
                {
                    Console.WriteLine(item);
                }
            }
        }
Example #2
0
 public void AddItemsToSet(string set_name, List <string> items)
 {
     using (IRedisClient client = new RedisClient())
     {
         client.AddRangeToSet(set_name, items);
     }
 }
Example #3
0
 public static void AddRangeToSet(string setId, List <string> items)
 {
     using (RedisClient client = GetClient())
     {
         client.AddRangeToSet(setId, items);
     }
 }
 /// <summary>
 /// 添加一个列表到某set集合
 /// </summary>
 /// <param name="setName"></param>
 /// <param name="values"></param>
 public static void AddItemListToSet(string setName, List <string> values)
 {
     using (RedisClient client = new RedisClient(redisAddress, 6379))
     {
         client.AddRangeToSet(setName.ToString(), values);
     }
 }
Example #5
0
 public void addKeyStringValueListStringToSet(String key, List <String> listDataSource)
 {
     using (RedisClient redisClient = new RedisClient(host, port))
     {
         redisClient.AddRangeToSet(key, listDataSource);
         redisClient.Dispose();
     }
 }
Example #6
0
        private void button27_Click(object sender, EventArgs e)
        {
            client.AddItemToSortedSet("sortset:name", "ssn1", 1);
            client.AddItemToSortedSet("sortset:name", "ssn2", 3);
            client.AddItemToSortedSet("sortset:name", "ssn3", 2);
            client.AddRangeToSet("sortset:name", new List<string> { "ssn4","ssn5"});

        }
		public void Add_range_to_set_fails_if_first_command()
		{
			var redis = new RedisClient(TestConfig.SingleHost);

			redis.AddRangeToSet("testset", storeMembers);

			var members = Redis.GetAllItemsFromSet("testset");
			Assert.That(members, Is.EquivalentTo(storeMembers));
		}
        public void Add_range_to_set_fails_if_first_command()
        {
            var redis = new RedisClient(TestConfig.SingleHost);

            redis.AddRangeToSet("testset", storeMembers);

            var members = Redis.GetAllItemsFromSet("testset");

            Assert.That(members, Is.EquivalentTo(storeMembers));
        }
Example #9
0
        public HashSet <String> GetIntersectWithSet(string set_name, List <String> items)
        {
            HashSet <String> intersects;
            String           temp_set = "some-temp-set";   //random guid can be generated here

            using (IRedisClient client = new RedisClient())
            {
                client.AddRangeToSet(temp_set, items);
                intersects = client.GetIntersectFromSets(set_name, temp_set);
                foreach (string item in items)
                {
                    client.RemoveItemFromSet(temp_set, item);
                }
            }
            return(intersects);
        }
Example #10
0
        public void addNewLineToRedis(string s)
        {
            //s = s.Trim();

            if (s.Split(' ').Length < 3)
            {
                return;
            }

            Dictionary <string, List <string> > chains = prepareDictionary(s);

            if (chains == null)
            {
                return;
            }

            foreach (KeyValuePair <string, List <string> > kvp in chains)
            {
                rclient.AddRangeToSet(kvp.Key, kvp.Value);
            }
        }
Example #11
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();
            };
        }
Example #12
0
 /// <summary>
 /// key集合中添加list集合
 /// </summary>
 public void Add(string key, List <string> list)
 {
     RedisClient.AddRangeToSet(key, list);
 }
        public static void Show()
        {
            using (RedisClient client = new RedisClient("127.0.0.1", 6379))
            {               //删除当前数据库中的所有Key  默认删除的是db0
                client.FlushDb();
                //删除所有数据库中的key
                client.FlushAll();
                #region Set 不重复集合
                string key = "clay_set";
                //投票
                #region 添加键值                 //就是自动去重,再带去重的功能
                var litaibai = new UserInfo()
                {
                    Id = 1, Name = "李太白"
                };
                //client.AddItemToList(key, JsonConvert.SerializeObject(litaibai));
                //client.AddItemToList(key, JsonConvert.SerializeObject(litaibai));
                //client.AddItemToSet(key, JsonConvert.SerializeObject(litaibai));
                //client.AddItemToSet(key, JsonConvert.SerializeObject(litaibai));
                //client.AddItemToSet(key, JsonConvert.SerializeObject(litaibai));
                //client.AddItemToSet(key, JsonConvert.SerializeObject(litaibai));
                //Console.WriteLine("***完成了");
                #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("tokey"))
                //{
                //	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

                #endregion

                #region  sorted set
                //string zsett_key = "clay_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 DicString)
                //{
                //	Console.WriteLine(r.Key + ":" + r.Value);
                //}

                //int  int32

                #endregion
            }
        }
Example #14
0
 public void AddRangeToSet(string setId, List <string> items)
 {
     redisClient.AddRangeToSet(setId, items);
 }
Example #15
0
        static void Main(string[] args)
        {
            using IRedisClient client = new RedisClient("127.0.0.1", 6379);
            #region Set集合
            client.FlushDb();
            //Set集合自动去重
            var libai = new User
            {
                ID   = "001",
                Name = "李白",
                Age  = "24"
            };
            client.AddItemToSet("set0", JsonConvert.SerializeObject(libai));
            libai = new User
            {
                ID   = "001",
                Name = "李白",
                Age  = "25"
            };
            client.AddItemToSet("set0", JsonConvert.SerializeObject(libai));
            client.AddItemToSet("set0", JsonConvert.SerializeObject(libai));
            Console.WriteLine(client.GetSetCount("set0"));
            //批量操作添加
            client.AddRangeToSet("set1", new List <string> {
                "0", "1", "2", "3"
            });
            //获取Set中的元素
            var sets = client.GetAllItemsFromSet("set1");
            Console.WriteLine("获取set中的元素......");
            foreach (var item in sets)
            {
                Console.WriteLine(item);
            }
            //从set中随机获取一个值
            Console.WriteLine("随机获取set中的一个值......");
            Console.WriteLine(client.GetRandomItemFromSet("set1"));
            //随机删除集合中的元素,并返回删除的值
            Console.WriteLine("随机删除set中的一个值并返回删除的值......");
            var count = client.GetSetCount("set1");
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine(client.PopItemFromSet("set1"));
            }
            //删除set集合中的某个值
            client.AddRangeToSet("set1", new List <string> {
                "0", "1", "2", "3"
            });
            client.RemoveItemFromSet("set1", "1");
            sets = client.GetAllItemsFromSet("set1");
            Console.WriteLine("剩下的值.....");
            foreach (var item in sets)
            {
                Console.WriteLine(item);
            }
            //从原来的集合移除放到新的集合中去
            client.MoveBetweenSets("set1", "set2", "3");
            #region 交叉并补
            client.AddRangeToSet("a", new List <string> {
                "1", "2", "3", "4"
            });
            client.AddRangeToSet("b", new List <string> {
                "4", "3", "5", "6"
            });
            var jiaoList = client.GetIntersectFromSets("a", "b");
            Console.WriteLine("交集");
            foreach (var item in jiaoList)
            {
                Console.WriteLine(item);
            }
            var bingList = client.GetUnionFromSets("a", "b");
            Console.WriteLine("并集");
            foreach (var item in bingList)
            {
                Console.WriteLine(item);
            }
            #endregion
            #endregion
            #region  ZSet集合
            //zset 自动去重,而且多了一个权重,或者分数的字段,自动排序
            client.FlushDb();
            Console.Clear();
            client.AddItemToSortedSet("zsetid", "a");//不给分数赋值,默认是最大的
            client.AddItemToSortedSet("zsetid", "b", 100);
            client.AddItemToSortedSet("zsetid", "c");
            client.AddItemToSortedSet("zsetid", "b");
            client.AddItemToSortedSet("zsetid", "d", 120);
            //get all items order by asc
            var zsetList = client.GetAllItemsFromSortedSet("zsetid");
            Console.WriteLine("the zsetList(asc)");
            foreach (var item in zsetList)
            {
                Console.WriteLine(item);
            }
            //get all items order by desc
            Console.WriteLine("zsetList(desc)");
            var descZsetList = client.GetAllItemsFromSortedSetDesc("zsetid");
            foreach (var item in descZsetList)
            {
                Console.WriteLine(item);
            }
            //get items by index(Desc)
            var zsets = client.GetRangeFromSortedSetDesc("zsetid", 0, 1);
            foreach (var item in zsets)
            {
                Console.WriteLine(item);
            }
            //get items by index(Asc)
            zsets = client.GetRangeFromSortedSet("zsetid", 0, 1);
            foreach (var item in zsets)
            {
                Console.WriteLine(item);
            }
            //get the items with scores
            var zsets1 = client.GetAllWithScoresFromSortedSet("zsetid");
            foreach (var item in zsets1)
            {
                Console.WriteLine(item);
            }
            client.AddItemToSortedSet("zsetid1", "a", 80);//不给分数赋值,默认是最大的
            client.AddItemToSortedSet("zsetid1", "b", 100);
            client.AddItemToSortedSet("zsetid1", "c", 234);
            client.AddItemToSortedSet("zsetid1", "b", 543);
            client.AddItemToSortedSet("zsetid1", "d", 120);
            client.AddItemToSortedSet("zsetid1", "e", 120);

            client.AddItemToSortedSet("zsetid2", "b", 543);
            client.AddItemToSortedSet("zsetid2", "d", 120);
            client.AddItemToSortedSet("zsetid2", "f", 120);
            //将多个集合中的交集放入一个新的集合中
            var dics = client.StoreIntersectFromSortedSets("newzsetid", "zsetid", "zsetid1", "zsetid2");
            #endregion
        }
Example #16
0
 public void AddRangeToSet(string set, List <string> values)
 {
     redisClient.AddRangeToSet(set, values);
 }