redis 专题 (三)set和zset操作

set和zset主要是对集合的操作,这个在数据处理的场景中,我觉得挺实用的。包括对数据的去重、交集、并集等处理,都很方便。

  using (RedisClient client = new RedisClient("127.0.0.1", 6379))
            {
                //删除当前数据库中的所有Key  默认删除的是db0
                client.FlushDb();
                //删除所有数据库中的key 
                client.FlushAll();

                string key = "keyiwang";

                #region 添加键值,自带去重功能
                //同一个key,可以添加多个值;同一个key,同一个值,不会重复写入,可用于投票、点赞之类,达到同一个ip或同一个账号不能重复操作
                //client.AddItemToSet(key, "张三丰");
                //client.AddItemToSet(key, "张三丰");
                //client.AddItemToSet(key, "火工头陀");

                //foreach (var item in client.GetAllItemsFromSet(key))
                //{
                //    Console.WriteLine(item);
                //}

                #endregion

                #region 随机获取key集合中的一个值,获取当前setid所有值
                //client.AddRangeToSet(key, new List<string>() { "张三", "张三", "李四", "王五" });
                ////当前setid中值的数量
                //Console.WriteLine("数量:"+client.GetSetCount(key));
                ////随机获取key中的一个值
                //Console.WriteLine("随机值:"+client.GetRandomItemFromSet(key));
                ////获取当前setid中所有的值
                //var lists = client.GetAllItemsFromSet(key);
                //foreach(var item in lists)
                //{
                //    Console.WriteLine(item);

                //}

                #endregion


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

                //client.AddRangeToSet("key1", new List<string>() { "001", "002", "003", "004" });
                //client.AddRangeToSet("key2", new List<string>() { "001", "003", "005", "006" });

                //var unionlist = client.GetUnionFromSets("key1", "key2"); //合并
                //Console.WriteLine("返回并集结果");
                //foreach(var item in unionlist)
                //{
                //    Console.WriteLine(item);
                //}

                ////把 key1和key2 并集结果存放到newkey集合中
                //client.StoreUnionFromSets("newkey", "key1", "key2");

                //Console.WriteLine("返回并集结果的新集合数据");
                //foreach(var item in client.GetAllItemsFromSet("newkey"))
                //{
                //    Console.WriteLine(item);
                //}

                #endregion

                #region 交集,获取两个集合中共同的元素

                //client.AddRangeToSet("key1", new List<string>() { "001", "002", "003", "004" });
                //client.AddRangeToSet("key2", new List<string>() { "001", "003", "005", "006" });
                //client.AddRangeToSet("key3", new List<string>() { "000", "007", "008", "009" });
                //var intersectList = client.GetIntersectFromSets("key1", "key2");
                //Console.WriteLine("返回交集结果的新集合数据");
                //foreach (var item in intersectList)
                //{
                //    Console.WriteLine(item);
                //}
                ////把 key1和key2 并集结果存放到newkey集合中
                //client.StoreIntersectFromSets("newkey", "key1", "key2");

                //Console.WriteLine("返回交集结果的新集合数据");
                //foreach (var item in client.GetAllItemsFromSet("newkey"))
                //{
                //    Console.WriteLine(item);
                //}

                // 把key1集合中的数据与key2集合中的数据对比,fromkey集合中不存在keys集合中,则把这些不存在的数据放入newkey集合中
                //client.StoreDifferencesFromSet("key3", "key1", "key2");
                //foreach (var item in client.GetAllItemsFromSet("key3"))
                //{
                //    Console.WriteLine(item);
                //}


                #endregion


                #region ZSet的操作
                //zset就是set的一个分支版本,它增加了一个分值,比如权重。能够按照分值进行排序。

                //  client.AddRangeToSortedSet(key, new List<string>() { "aa", "bb" }, 100);


                client.AddItemToSortedSet(key, "关羽", 2);
                client.AddItemToSortedSet(key, "刘备", 1);
                client.AddItemToSortedSet(key, "张飞", 3);
                ////获取当前value的结果,排序,升序
                //foreach(var item in client.GetAllItemsFromSortedSet(key))
                //{
                //    Console.WriteLine(item);
                //}
                //获取分值2到3范围内的数据,查询出来就是关羽和张飞满足
                //var list= client.GetRangeFromSortedSetByHighestScore(key, 2, 3);


               var list = client.GetRangeFromSortedSet(key, 0, 1); //按下标索引 0,先排序,默认是升序,升序完后取第N-M个数(含NM),类似分页
                foreach (var item in list)
                {
                    Console.WriteLine(item);
                }

                #endregion

                //更多zset用法,参考https://www.cnblogs.com/knowledgesea/p/5032101.html




            }

下面是摘自别人的方法汇总:

把 RedisBase.Core 替换成我们自己的redis客户端对象即可。

.Net操作Redis数据类型Set

public class DoRedisSet:DoRedisBase
    {
        #region 添加
        /// <summary>
        /// key集合中添加value值
        /// </summary>
        public void Add(string key, string value)
        {
            RedisBase.Core.AddItemToSet(key,value);
        }
        /// <summary>
        /// key集合中添加list集合
        /// </summary>
        public void Add(string key, List<string> list)
        {
            RedisBase.Core.AddRangeToSet(key, list);
        }
        #endregion
        #region 获取
        /// <summary>
        /// 随机获取key集合中的一个值
        /// </summary>
        public string GetRandomItemFromSet(string key)
        {
           return RedisBase.Core.GetRandomItemFromSet(key);
        }
        /// <summary>
        /// 获取key集合值的数量
        /// </summary>
        public long GetCount(string key)
        {
            return RedisBase.Core.GetSetCount(key);
        }
        /// <summary>
        /// 获取所有key集合的值
        /// </summary>
        public HashSet<string> GetAllItemsFromSet(string key)
        {
            return RedisBase.Core.GetAllItemsFromSet(key);
        }
        #endregion
        #region 删除
        /// <summary>
        /// 随机删除key集合中的一个值
        /// </summary>
        public string PopItemFromSet(string key)
        {
            return RedisBase.Core.PopItemFromSet(key);
        }
        /// <summary>
        /// 删除key集合中的value
        /// </summary>
        public void RemoveItemFromSet(string key, string value)
        {
            RedisBase.Core.RemoveItemFromSet(key,value);
        }
        #endregion
        #region 其它
        /// <summary>
        /// 从fromkey集合中移除值为value的值,并把value添加到tokey集合中
        /// </summary>
        public void MoveBetweenSets(string fromkey,string tokey,string value)
        {
             RedisBase.Core.MoveBetweenSets(fromkey,tokey,value);
        }
        /// <summary>
        /// 返回keys多个集合中的并集,返还hashset
        /// </summary>
        public HashSet<string> GetUnionFromSets(string[] keys)
        {
           return RedisBase.Core.GetUnionFromSets(keys);
        }
        /// <summary>
        /// keys多个集合中的并集,放入newkey集合中
        /// </summary>
        public void StoreUnionFromSets(string newkey, string[] keys)
        {
             RedisBase.Core.StoreUnionFromSets(newkey,keys);
        }
        /// <summary>
        /// 把fromkey集合中的数据与keys集合中的数据对比,fromkey集合中不存在keys集合中,则把这些不存在的数据放入newkey集合中
        /// </summary>
        public void StoreDifferencesFromSet(string newkey, string fromkey, string[] keys)
        {
            RedisBase.Core.StoreDifferencesFromSet(newkey,fromkey,keys);
        }
        #endregion
    }

.Net操作Redis数据类型有序集合Zset 

zset和set的区别在于 zset给我们按照分数进行了排序。

public class DoRedisZSet : DoRedisBase
    {
        #region 添加
        /// <summary>
        /// 添加key/value,默认分数是从1.多*10的9次方以此递增的,自带自增效果
        /// </summary>
        public bool AddItemToSortedSet(string key, string value)
        {
           return RedisBase.Core.AddItemToSortedSet(key, value);
        }
        /// <summary>
        /// 添加key/value,并设置value的分数
        /// </summary>
        public bool AddItemToSortedSet(string key, string value, double score)
        {
            return RedisBase.Core.AddItemToSortedSet(key, value, score);
        }
        /// <summary>
        /// 为key添加values集合,values集合中每个value的分数设置为score
        /// </summary>
        public bool AddRangeToSortedSet(string key,List<string> values,double score)
        {
            return RedisBase.Core.AddRangeToSortedSet(key, values, score);
        }
        /// <summary>
        /// 为key添加values集合,values集合中每个value的分数设置为score
        /// </summary>
        public bool AddRangeToSortedSet(string key, List<string> values, long score)
        {
            return RedisBase.Core.AddRangeToSortedSet(key, values, score);
        }
        #endregion
        #region 获取
        /// <summary>
        /// 获取key的所有集合
        /// </summary>
        public List<string> GetAllItemsFromSortedSet(string key)
        {
            return RedisBase.Core.GetAllItemsFromSortedSet(key);
        }
        /// <summary>
        /// 获取key的所有集合,倒叙输出
        /// </summary>
        public List<string> GetAllItemsFromSortedSetDesc(string key)
        {
            return RedisBase.Core.GetAllItemsFromSortedSetDesc(key);
        }
        /// <summary>
        /// 获取可以的说有集合,带分数
        /// </summary>
        public IDictionary<string, double> GetAllWithScoresFromSortedSet(string key)
        {
            return RedisBase.Core.GetAllWithScoresFromSortedSet(key);
        }
        /// <summary>
        /// 获取key为value的下标值
        /// </summary>
        public long GetItemIndexInSortedSet(string key, string value)
        {
            return RedisBase.Core.GetItemIndexInSortedSet(key, value);
        }
        /// <summary>
        /// 倒叙排列获取key为value的下标值
        /// </summary>
        public long GetItemIndexInSortedSetDesc(string key, string value)
        {
            return RedisBase.Core.GetItemIndexInSortedSetDesc(key, value);
        }
        /// <summary>
        /// 获取key为value的分数
        /// </summary>
        public double GetItemScoreInSortedSet(string key,string value)
        {
            return RedisBase.Core.GetItemScoreInSortedSet(key,value);
        }
        /// <summary>
        /// 获取key所有集合的数据总数
        /// </summary>
        public long GetSortedSetCount(string key)
        {
            return RedisBase.Core.GetSortedSetCount(key);
        }
        /// <summary>
        /// key集合数据从分数为fromscore到分数为toscore的数据总数
        /// </summary>
        public long GetSortedSetCount(string key,double fromScore,double toScore)
        {
            return RedisBase.Core.GetSortedSetCount(key,fromScore,toScore);
        }
        /// <summary>
        /// 获取key集合从高分到低分排序数据,分数从fromscore到分数为toscore的数据
        /// </summary>
        public List<string> GetRangeFromSortedSetByHighestScore(string key, double fromscore, double toscore)
        {
            return RedisBase.Core.GetRangeFromSortedSetByHighestScore(key, fromscore, toscore);
        }
        /// <summary>
        /// 获取key集合从低分到高分排序数据,分数从fromscore到分数为toscore的数据
        /// </summary>
        public List<string> GetRangeFromSortedSetByLowestScore(string key, double fromscore, double toscore)
        {
            return RedisBase.Core.GetRangeFromSortedSetByLowestScore(key, fromscore, toscore);
        }
        /// <summary>
        /// 获取key集合从高分到低分排序数据,分数从fromscore到分数为toscore的数据,带分数
        /// </summary>
        public IDictionary<string, double> GetRangeWithScoresFromSortedSetByHighestScore(string key, double fromscore, double toscore)
        {
            return RedisBase.Core.GetRangeWithScoresFromSortedSetByHighestScore(key, fromscore, toscore);
        }
        /// <summary>
        ///  获取key集合从低分到高分排序数据,分数从fromscore到分数为toscore的数据,带分数
        /// </summary>
        public IDictionary<string, double> GetRangeWithScoresFromSortedSetByLowestScore(string key, double fromscore, double toscore)
        {
            return RedisBase.Core.GetRangeWithScoresFromSortedSetByLowestScore(key, fromscore, toscore);
        }
        /// <summary>
        ///  获取key集合数据,下标从fromRank到分数为toRank的数据
        /// </summary>
        public List<string> GetRangeFromSortedSet(string key, int fromRank, int toRank)
        {
            return RedisBase.Core.GetRangeFromSortedSet(key, fromRank, toRank);
        }
        /// <summary>
        /// 获取key集合倒叙排列数据,下标从fromRank到分数为toRank的数据
        /// </summary>
        public List<string> GetRangeFromSortedSetDesc(string key, int fromRank, int toRank)
        {
            return RedisBase.Core.GetRangeFromSortedSetDesc(key, fromRank, toRank);
        }
        /// <summary>
        /// 获取key集合数据,下标从fromRank到分数为toRank的数据,带分数
        /// </summary>
        public IDictionary<string, double> GetRangeWithScoresFromSortedSet(string key, int fromRank, int toRank)
        {
            return RedisBase.Core.GetRangeWithScoresFromSortedSet(key, fromRank, toRank);
        }
        /// <summary>
        ///  获取key集合倒叙排列数据,下标从fromRank到分数为toRank的数据,带分数
        /// </summary>
        public IDictionary<string, double> GetRangeWithScoresFromSortedSetDesc(string key, int fromRank, int toRank)
        {
            return RedisBase.Core.GetRangeWithScoresFromSortedSetDesc(key, fromRank, toRank);
        }
        #endregion
        #region 删除
        /// <summary>
        /// 删除key为value的数据
        /// </summary>
        public bool RemoveItemFromSortedSet(string key,string value)
        {
            return RedisBase.Core.RemoveItemFromSortedSet(key, value);
        }
        /// <summary>
        /// 删除下标从minRank到maxRank的key集合数据
        /// </summary>
        public long RemoveRangeFromSortedSet(string key,int minRank,int maxRank)
        {
            return RedisBase.Core.RemoveRangeFromSortedSet(key,minRank,maxRank);
        }
        /// <summary>
        /// 删除分数从fromscore到toscore的key集合数据
        /// </summary>
        public long RemoveRangeFromSortedSetByScore(string key, double fromscore, double toscore)
        {
            return RedisBase.Core.RemoveRangeFromSortedSetByScore(key, fromscore, toscore);
        }
        /// <summary>
        /// 删除key集合中分数最大的数据
        /// </summary>
        public string PopItemWithHighestScoreFromSortedSet(string key)
        {
            return RedisBase.Core.PopItemWithHighestScoreFromSortedSet(key);
        }
        /// <summary>
        /// 删除key集合中分数最小的数据
        /// </summary>
        public string PopItemWithLowestScoreFromSortedSet(string key)
        {
            return RedisBase.Core.PopItemWithLowestScoreFromSortedSet(key);
        }
        #endregion
        #region 其它
        /// <summary>
        /// 判断key集合中是否存在value数据
        /// </summary>
        public bool SortedSetContainsItem(string key, string value)
        {
            return RedisBase.Core.SortedSetContainsItem(key,value);
        }
        /// <summary>
        /// 为key集合值为value的数据,分数加scoreby,返回相加后的分数
        /// </summary>
        public double IncrementItemInSortedSet(string key,string value,double scoreBy)
        {
            return RedisBase.Core.IncrementItemInSortedSet(key,value,scoreBy);
        }
        /// <summary>
        /// 获取keys多个集合的交集,并把交集添加的newkey集合中,返回交集数据的总数
        /// </summary>
        public long StoreIntersectFromSortedSets(string newkey, string[] keys)
        {
            return RedisBase.Core.StoreIntersectFromSortedSets(newkey,keys);
        }
        /// <summary>
        /// 获取keys多个集合的并集,并把并集数据添加到newkey集合中,返回并集数据的总数
        /// </summary>
        public long StoreUnionFromSortedSets(string newkey, string[] keys)
        {
            return RedisBase.Core.StoreUnionFromSortedSets(newkey, keys);
        }
        #endregion
    }

 

posted @ 2020-07-13 14:18  黄明辉  阅读(3387)  评论(0编辑  收藏  举报