• Redis数据类型SortedSET


    Sorted Set有点像Set和Hash的结合体。
    和Set一样,它里面的元素是唯一的,类型是String,所以它可以理解为就是一个Set。
    但是Set里面的元素是无序的,而Sorted Set里面的元素都带有一个浮点值,叫做分数(score),所以这一点和Hash有点像,因为每个元素都映射到了一个值。
    Sorted Set是有序的,规则如下:
    如果A.score > B.score,那么A > B。
    如果A.score == B.score,那么A和B的大小就通过比较字符串来决定了,而A和B的字符串是不会相等的,因为Sorted Set里面的值都是唯一的。

    ZADD

    ZADD可以添加元素到Sorted Set,就和Set的SADD命令差不多

    ZRANGE,ZREVRANGE

    ZRANGE默认按分数由低到高把Sorted Set的元素显示出来

     

    想按分数要从高到低显示,需要使用ZREVRANGE

     

    也可以一同把分数显示出来,使用参数WITHSCORES

    ZRANGEBYSCORE

     ZRANGEBYSCORE可以按范围显示Sorted Set,格式是zrangebyscore key 分数下限 分数上限

    可以看到结果也包括了分数下限和分数上限这两个边

    ZREMRANGEBYSCORE

    ZREMRANGEBYSCORE可以按范围移除元素

    该命令返回的是移除元素的个数。

    其中-inf和inf分别表示负无穷和正无穷。

    ZRANK,ZREVRANK

     ZRANK命令可以获得元素的排名, ZREVRANK 反之

    词典分数

     Sorted Set里分数相同的元素是按照词典分数(可以理解为比较字符串)进行排序的

    ZRANGEBYLEX

    ZRANGEBYLEX可以按词典范围展示Sorted Set

    可以看到该命令把开头字目为A到F(不包括F)的元素都显示了出来

     还有ZREVRANGEBYLEXZREMRANGEBYLEXZLEXCOUNT等针对词典的命令,请自行探索。

    C#操作SortedSET

     class Program
        {
            static void Main(string[] args)
            {
                ConnectionMultiplexer connectionMultiplexer = ConnectionMultiplexer.Connect("120.132.116.153:6379");
                IDatabase db = connectionMultiplexer.GetDatabase(0);
                db.KeyDelete("players");
                db.KeyDelete("names");
                //ZADD
                db.SortedSetAdd("players", "a1", 10);
                db.SortedSetAdd("players", new SortedSetEntry[]
                {
                    new SortedSetEntry("b2",21),
                    new SortedSetEntry("b3",22),
                    new SortedSetEntry("a2",11),
                    new SortedSetEntry("a5",14),
                    new SortedSetEntry("a6",15),
                    new SortedSetEntry("a3",12),
                    new SortedSetEntry("a4",13),
                    new SortedSetEntry("b4",23),
                    new SortedSetEntry("b5",24),
                    new SortedSetEntry("b6",25),
                });
                //ZRANGE
                var lists = db.SortedSetRangeByRank("players", 0, -1);
                foreach (var item in lists)
                {
                    Console.WriteLine($"{item}");
                }
                Console.WriteLine("----------------------------");
    
                //ZREVRANGE WITHSCORES 
                var lists1 = db.SortedSetRangeByRankWithScores("players", 0, -1, Order.Descending);
                foreach (var item in lists1)
                {
                    Console.WriteLine($"{item}");
                }
                Console.WriteLine("----------------------------");
    
                //ZRANGEBYSCORE WITHSCORES 
                var list2 = db.SortedSetRangeByScoreWithScores("players", 10, 15);
                foreach (var item in list2)
                {
                    Console.WriteLine($"{item}");
                }
                Console.WriteLine("----------------------------");
    
                //ZREMRANGEBYSCORE
                Console.WriteLine($"{db.SortedSetRemoveRangeByScore("players", double.NegativeInfinity, 15)}");
                var lists3 = db.SortedSetRangeByRankWithScores("players", 0, -1);
                foreach (var item in lists3)
                {
                    Console.WriteLine($"{item}");
                }
                Console.WriteLine("----------------------------");
                //ZRANK
                Console.WriteLine($"a1排名顺序:{db.SortedSetRank("players", "b2")}");
                Console.WriteLine($"a1排名倒序:{db.SortedSetRank("players", "b2", Order.Descending)}");
                Console.WriteLine("----------------------------");
              
                db.SortedSetAdd("names", new SortedSetEntry[]
                {
                    new SortedSetEntry("AB",0),
                    new SortedSetEntry("BERRD",0),
                    new SortedSetEntry("CBsdad",0),
                    new SortedSetEntry("DBasd",0),
                    new SortedSetEntry("EBasd",0),
                    new SortedSetEntry("FBad",0),
                    new SortedSetEntry("HBasd",0),
                    new SortedSetEntry("3Basd",0),
                    new SortedSetEntry("7Bfh",0),
                    new SortedSetEntry("6Bfgh",0),
                });
                //ZRANGEBYLEX
                var lists4 = db.SortedSetRangeByValue("names", "C", "F");
                foreach (var item in lists4)
                {
                    Console.WriteLine($"{item}");
                }
                Console.ReadLine();
            }
        }

  • 相关阅读:
    sed 拓展 awk 拓展
    9.6/9.7 awk
    9.4/9.5 sed
    正则介绍_grep上& grep中 & grep下
    shell特殊符号cut命令& sort_wc_uniq命令 & tee_tr_split命令 & shell特殊符号
    管道符和作业控制 & shell变量& 环境变量配置文件
    shell介绍 & 命令历史 & 命令补全和别名& 通配符& 输入输出重定向
    yum更换国内源 & yum下载rpm包 & 源码包安装
    网络编程入门(下)
    网络编程入门(上)
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/10722748.html
Copyright © 2020-2023  润新知