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();
        }
    }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM