Code without saying more:

//Set add single element 
            stopwatch.Start();
            var isok = RedisCacheHelper.Instance.SetAdd("setkey", "10");
            stopwatch.Stop();
            Console. Writeline ("set add single element consumption time:" + stopwatch. Elapsedmilliseconds. Tostring());


            //Set add multiple elements sting type set 
            List strlist = new List () {"1", "2", "A", "B", "you", "good"};
            stopwatch.Start();
            var getlong = RedisCacheHelper.Instance.SetAdd("setkeylist", strlist);
            stopwatch.Stop();
            Console. Writeline ("set adds multiple elements and consumes time:" + stopwatch. Elapsedmilliseconds. Tostring());
            Console. Writeline ("return set length:" + getlong);


            //Get 10 pieces of data from the database
            stopwatch.Start();
            var getlist = TestRedis.GetOrderFormAll(10);
            stopwatch.Stop();
            Console. Writeline ("take time to get 10 pieces of data from database:" + stopwatch. Elapsedmilliseconds. Tostring());

            //Set add multiple object collection serialization 
            stopwatch.Start();
            var getvalue = RedisCacheHelper.Instance.SetAddList("setkeyobjlist", getlist);
            stopwatch.Stop();
            Console. Writeline ("set adding multiple object sets consumes time:" + stopwatch. Elapsedmilliseconds. Tostring());
            Console. Writeline ("return set length:" + getValue);

 

 

//Get the length of set set set 
            var getleng = RedisCacheHelper.Instance.SetLength("setkeylist");
            Console. Writeline ("get the length of setkeylist:" + getleng);
            var getobjleng = RedisCacheHelper.Instance.SetLength("setkeyobjlist");
            Console. Writeline ("get the length of setkeyobjlist:" + getobjleng);


            //Check whether the element belongs to set collection 
            var getisA = RedisCacheHelper.Instance.ExistsInSet("setkeylist", "A");
            var getisC = RedisCacheHelper.Instance.ExistsInSet("setkeylist", "C");
            Console. Writeline ("check whether a belongs to setkeylist set:" + getisa);
            Console. Writeline ("check whether C belongs to setkeylist set:" + getisc);


            //Get all set elements according to key  
            stopwatch.Start();
            var getlist = RedisCacheHelper.Instance.GetMembers("setkeylist");
            stopwatch.Stop();
            Console. Writeline ("get all set elements consumed time:" + stopwatch. Elapsedmilliseconds. Tostring());
            foreach (var item in getlist)
            {
                Console.WriteLine(item);
            }


            //Get all object elements and deserialize according to key
            stopwatch.Start();
            var getobjlist = RedisCacheHelper.Instance.GetAllMembers("setkeyobjlist");
            stopwatch.Stop();
            Console. Writeline ("get all object set elapsed time:" + stopwatch. Elapsedmilliseconds. Tostring());
            foreach (var item in getobjlist)
            {
                Console. Writeline ("print area:" + item. Area);
            }

//Finding intersection, difference and union
            string[] arry2 = { "setkeylist", "setkeylist2" };
            Var D1 = rediscachehelper. Instance. Getcombinations (setoperation. Union, arry2. Tolist()); // Union
            Var D2 = rediscachehelper. Instance. Getcombinations (setoperation. Intersect, arry2. Tolist()); // intersection
            Var D3 = rediscachehelper. Instance. Getcombinations (setoperation. Difference, arry2. Tolist()); // difference set
            foreach (var item in d1)
            {
                Console. Writeline ("the union of setkeylist and setkeylist2 is:" + item);
            }
         foreach (var item in d2)
            {
                Console. Writeline ("intersection of setkeylist and setkeylist2 is:" + item);
            }
foreach (var item in d3)
            {
                Console. Writeline ("the difference sets of setkeylist and setkeylist2 are:" + item);
            }

//According to the key, get one element in the set randomly and get the repeated content,
            for (int i = 0; i < 5; i++)
            {
                stopwatch.Start();
                var getone = RedisCacheHelper.Instance.GetRandomMember("setkeyobjlist");
                stopwatch.Stop();
                //Console. Writeline ("get one element in the set randomly according to the key to consume time:" + stopwatch. Elapsedmilliseconds. Tostring());
                Console. Writeline ("print:" + getone);
            }

            //Randomly obtain n elements in the set according to the key and obtain them at one time, without duplicate values
            stopwatch.Start();
            var getobjlist = RedisCacheHelper.Instance.GetRandomMembers("setkeyobjlist", 5);  
            stopwatch.Stop();
            Console. Writeline ("get n elements in the set randomly according to the key to consume time:" + stopwatch. Elapsedmilliseconds. Tostring());
            foreach (var item in getobjlist)
            {
                Console. Writeline ("print:" + item);
            }

 

Application scenario:

Redis set is similar to list. It is a list function. The special thing is that it can automatically arrange the duplicate data. When you need to store a list data and do not want to have duplicate data, set is a good choice. Moreover, set provides an important interface to judge whether a member is in a set set. This is also a list Not available.

Implementation mode:

The internal implementation of set is a HashMap whose value is always null. In fact, it can quickly arrange the weight by calculating the hash, which is also the reason that set can provide to judge whether a member is in the set.

 

Finally, the helper help class is attached

/// 
        ///Set add single element   
        ///Unique, such as number of online users / number of likes / number of collectors, etc
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool SetAdd(string key, string value, TimeSpan? span = null, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                //Set expiration time
                if (span != null)
                {
                    this.KeyExpire(key, span);
                }
                return _db.SetAdd(key, value);
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// 
        ///Set add multiple element sets
        /// 
        /// 
        /// 
        /// 
        /// 
        public long SetAdd(string key, List arryList, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                RedisValue[] valueList = arryList.Select(u => (RedisValue)u).ToArray();
                return _db.SetAdd(key, valueList);
            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// 
        ///Set add multiple object collection serialization
        /// 
        /// 
        /// 
        /// 
        /// 
        public long SetAddList(string key, IEnumerable list, int db = -1) where T : class
        {
            try
            {
                _db = GetDatabase(db);
                List listRedisValue = new List();
                foreach (var item in list)
                {
                    string json = JsonConvert.SerializeObject(item);
                    listRedisValue.Add(json);
                }
                return _db.SetAdd(key, listRedisValue.ToArray());
            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// 
        ///Get the length of set set set
        /// 
        /// 
        /// 
        /// 
        public long SetLength(string key, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                return _db.SetLength(key);
            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// 
        ///Check whether the element belongs to set collection
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool ExistsInSet(string key, string value, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                return _db.SetContains(key, value);
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// 
        ///Get all set elements according to key
        /// 
        /// 
        /// 
        /// 
        public IEnumerable GetMembers(string redisKey, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                var rValue = _db.SetMembers(redisKey);
                return rValue.Select(ob => ob.ToString());
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// 
        ///Get all set object sets according to key and deserialize them
        /// 
        /// 
        /// 
        /// 
        /// 
        public List GetAllMembers(string redisKey, int db = -1) where T : class
        {
            List result = new List();
            try
            {
                _db = GetDatabase(db);
                var arr = _db.SetMembers(redisKey);
                foreach (var item in arr)
                {
                    if (!item.IsNullOrEmpty)
                    {
                        var t = JsonConvert.DeserializeObject(item);
                        if (t != null)
                        {
                            result.Add(t);
                        }
                    }
                }
            }
            catch (Exception)
            {
                return null;
            }
            return result;
        }


        /// 
        ///Get one element in the set randomly according to the key and do not delete it
        ///Cases that can be applied to winning category
        /// 
        /// 
        /// 
        /// 
        public string GetRandomMember(string redisKey, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                var rValue = _db.SetRandomMember(redisKey);
                return rValue.ToString();
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// 
        ///Get n elements in the set randomly according to the key and do not delete the element
        ///Cases that can be applied to winning category
        /// 
        /// 
        /// 
        /// 
        /// 
        public IEnumerable GetRandomMembers(string redisKey, long count, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                var rValue = _db.SetRandomMembers(redisKey, count);
                return rValue.Select(ob => ob.ToString());
            }
            catch (Exception)
            {
                throw;
            }
        }


        /// 
        ///Randomly delete a value in the specified key set and return these values  
        ///Cases applicable to lottery
        /// 
        /// 
        /// 
        /// 
        public string GetRandomRemovePop(string redisKey, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                var rValue = _db.SetPop(redisKey);
                return rValue.ToString();
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// 
        ///Randomly delete n values in the specified key set and return these values  
        ///Cases applicable to lottery
        /// 
        /// 
        /// 
        /// 
        /// 
        public IEnumerable GetRandomRemovePops(string redisKey, long count, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                var rValue = _db.SetPop(redisKey, count);
                return rValue.Select(ob => ob.ToString());
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// 
        ///Returns the intersection, difference and union of the specified rediskey set  
        ///You can only query in the same library, not across Libraries
        /// 
        /// 
        /// 
        /// 
        /// 
        public IEnumerable GetCombines(SetOperation operation, List keyList, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                RedisKey[] valueList = keyList.Select(u => (RedisKey)u).ToArray();
                var rValue = _db.SetCombine(operation, valueList);
                return rValue.Select(ob => ob.ToString());
            }
            catch (Exception)
            {
                throw;
            }
        }

View Code