StackExchange.Redis 之 Set集合 類型示例

話很少說直接上代碼:redis

            // set添加單個元素 
            stopwatch.Start();
            var isok = RedisCacheHelper.Instance.SetAdd("setkey", "10");
            stopwatch.Stop();
            Console.WriteLine("set添加單個元素消耗時間:" + stopwatch.ElapsedMilliseconds.ToString());


            // set添加多個元素  sting類型集合 
            List<string> strlist = new List<string>() { "1", "2", "A", "B", "", "" };
            stopwatch.Start();
            var getlong = RedisCacheHelper.Instance.SetAdd("setkeylist", strlist);
            stopwatch.Stop();
            Console.WriteLine("set添加多個元素消耗時間:" + stopwatch.ElapsedMilliseconds.ToString());
            Console.WriteLine("返回集合長度" + getlong);


            //從數據庫獲取10條數據
            stopwatch.Start();
            var getlist = TestRedis.GetOrderFormAll(10);
            stopwatch.Stop();
            Console.WriteLine("從數據庫獲取10條數據消耗時間:" + stopwatch.ElapsedMilliseconds.ToString());

            // set添加多個對象集合  序列化 
            stopwatch.Start();
            var getvalue = RedisCacheHelper.Instance.SetAddList("setkeyobjlist", getlist);
            stopwatch.Stop();
            Console.WriteLine("set添加多個對象集合消耗時間:" + stopwatch.ElapsedMilliseconds.ToString());
            Console.WriteLine("返回集合長度" + getvalue);

 

 

            // 獲取set集合的長度 
            var getleng = RedisCacheHelper.Instance.SetLength("setkeylist");
            Console.WriteLine("獲取 setkeylist 的長度:" + getleng);
            var getobjleng = RedisCacheHelper.Instance.SetLength("setkeyobjlist");
            Console.WriteLine("獲取 setkeyobjlist 的長度:" + getobjleng);


            // 檢查元素是否屬於Set集合 
            var getisA = RedisCacheHelper.Instance.ExistsInSet("setkeylist", "A");
            var getisC = RedisCacheHelper.Instance.ExistsInSet("setkeylist", "C");
            Console.WriteLine("檢查 A 是否屬於setkeylist集合:" + getisA);
            Console.WriteLine("檢查 C 是否屬於setkeylist集合:" + getisC);


            // 根據key獲取全部Set元素  
            stopwatch.Start();
            var getlist = RedisCacheHelper.Instance.GetMembers("setkeylist");
            stopwatch.Stop();
            Console.WriteLine("獲取全部Set元素消耗時間:" + stopwatch.ElapsedMilliseconds.ToString());
            foreach (var item in getlist)
            {
                Console.WriteLine(item);
            }


            // 根據key獲取全部對象元素  反序列化
            stopwatch.Start();
            var getobjlist = RedisCacheHelper.Instance.GetAllMembers<OrderForm>("setkeyobjlist");
            stopwatch.Stop();
            Console.WriteLine("獲取全部對象集合消耗時間:" + stopwatch.ElapsedMilliseconds.ToString());
            foreach (var item in getobjlist)
            {
                Console.WriteLine("打印Area: " + item.Area);
            }

            //求 交集、差集、並集
            string[] arry2 = { "setkeylist", "setkeylist2" };
            var d1 = RedisCacheHelper.Instance.GetCombines(SetOperation.Union, arry2.ToList());  //並集
            var d2 = RedisCacheHelper.Instance.GetCombines(SetOperation.Intersect, arry2.ToList());  //交集
            var d3 = RedisCacheHelper.Instance.GetCombines(SetOperation.Difference, arry2.ToList());  //差集
            foreach (var item in d1)
            {
                Console.WriteLine("setkeylist和setkeylist2的並集有:" + item);
            }
         foreach (var item in d2)
            {
                Console.WriteLine("setkeylist和setkeylist2的交集有:" + item);
            }
foreach (var item in d3)
            {
                Console.WriteLine("setkeylist和setkeylist2的差集有:" + item);
            }

            // 根據key隨機獲取Set中的1個元素  循環獲取會獲得重複內容,
            for (int i = 0; i < 5; i++)
            {
                stopwatch.Start();
                var getone = RedisCacheHelper.Instance.GetRandomMember("setkeyobjlist");
                stopwatch.Stop();
                //Console.WriteLine("根據key隨機獲取Set中的1個元素消耗時間:" + stopwatch.ElapsedMilliseconds.ToString());
                Console.WriteLine("打印: " + getone);
            }

            // 根據key隨機獲取Set中的n個元素   一次性獲取,無重複值
            stopwatch.Start();
            var getobjlist = RedisCacheHelper.Instance.GetRandomMembers("setkeyobjlist", 5);  
            stopwatch.Stop();
            Console.WriteLine("根據key隨機獲取Set中的n個元素消耗時間:" + stopwatch.ElapsedMilliseconds.ToString());
            foreach (var item in getobjlist)
            {
                Console.WriteLine("打印: " + item);
            }

 

最後附上Helper幫助類數據庫

        /// <summary>
        /// set添加單個元素   
        /// 具備惟一性   好比在線人數/點贊人數/收藏人數等
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public bool SetAdd(string key, string value, TimeSpan? span = null, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                //設置過時時間
                if (span != null)
                {
                    this.KeyExpire(key, span);
                }
                return _db.SetAdd(key, value);
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// set添加多個元素集合
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public long SetAdd(string key, List<string> 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;
            }
        }

        /// <summary>
        /// set添加多個對象集合   序列化
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public long SetAddList<T>(string key, IEnumerable<T> list, int db = -1) where T : class
        {
            try
            {
                _db = GetDatabase(db);
                List<RedisValue> listRedisValue = new List<RedisValue>();
                foreach (var item in list)
                {
                    string json = JsonConvert.SerializeObject(item);
                    listRedisValue.Add(json);
                }
                return _db.SetAdd(key, listRedisValue.ToArray());
            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// <summary>
        /// 獲取set集合的長度
        /// </summary>
        /// <param name="key"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public long SetLength(string key, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                return _db.SetLength(key);
            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// <summary>
        /// 檢查元素是否屬於Set集合
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public bool ExistsInSet(string key, string value, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                return _db.SetContains(key, value);
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 根據key獲取全部Set元素
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public IEnumerable<string> GetMembers(string redisKey, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                var rValue = _db.SetMembers(redisKey);
                return rValue.Select(ob => ob.ToString());
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 根據key獲取全部Set對象集合  反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="redisKey"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public List<T> GetAllMembers<T>(string redisKey, int db = -1) where T : class
        {
            List<T> result = new List<T>();
            try
            {
                _db = GetDatabase(db);
                var arr = _db.SetMembers(redisKey);
                foreach (var item in arr)
                {
                    if (!item.IsNullOrEmpty)
                    {
                        var t = JsonConvert.DeserializeObject<T>(item);
                        if (t != null)
                        {
                            result.Add(t);
                        }
                    }
                }
            }
            catch (Exception)
            {
                return null;
            }
            return result;
        }


        /// <summary>
        /// 根據key隨機獲取Set中的1個元素   不刪除該元素
        /// 可應用於中獎類的案例
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public string GetRandomMember(string redisKey, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                var rValue = _db.SetRandomMember(redisKey);
                return rValue.ToString();
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 根據key隨機獲取Set中的n個元素   不刪除該元素
        /// 可應用於中獎類的案例
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="count"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public IEnumerable<string> 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;
            }
        }


        /// <summary>
        /// 隨機刪除指定key集合中的一個值,並返回這些值  
        /// 可應用於抽獎類的案例
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public string GetRandomRemovePop(string redisKey, int db = -1)
        {
            try
            {
                _db = GetDatabase(db);
                var rValue = _db.SetPop(redisKey);
                return rValue.ToString();
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 隨機刪除指定key集合中的n個值,並返回這些值  
        /// 可應用於抽獎類的案例
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="count"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public IEnumerable<string> 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;
            }
        }

        /// <summary>
        /// 返回指定rediskey集合的交集、差集、並集  
        /// 只能在同一個庫內查詢,跨庫則不行
        /// </summary>
        /// <param name="operation"></param>
        /// <param name="keyList"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public IEnumerable<string> GetCombines(SetOperation operation, List<string> 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
相關文章
相關標籤/搜索