(精华)2020年8月16日 redis数据库 StackExchange.Redis中ZSet类型(C#版)

RedisZSetService 类请参考第一篇的封装

redis 数据结构ZSet的使用

using (RedisZSetService service = new RedisZSetService())
{
     
service.FlushAll();//清理全部数据

service.Add("advanced", "1");
service.Add("advanced", "2");
service.Add("advanced", "5");
service.Add("advanced", "4");
service.Add("advanced", "7");
service.Add("advanced", "5");
service.Add("advanced", "9");

var result1 = service.GetAll("advanced");
var result2 = service.GetAllDesc("advanced");

service.AddItemToSortedSet("Sort", "BY", 123234);
service.AddItemToSortedSet("Sort", "走自己的路", 123);
service.AddItemToSortedSet("Sort", "redboy", 45);
service.AddItemToSortedSet("Sort", "大蛤蟆", 7567);
service.AddItemToSortedSet("Sort", "路人甲", 9879);
var result3 = service.GetAllWithScoresFromSortedSet("Sort");
}

案例

/// 
    /// 直播平台:很多频道---不同的房间/主播
    ///           刷小礼物!
    ///           
    ///     
    /// 排行榜:频道排行!平台的排行!房间排行榜!  需要实时展示数据!
    /// Redis--Zset;排序
    /// 
    /// 
    public class RankManager
    {
     

        //模拟的房间的听众
        private static List<string> userList = new List<string>()
        {
     
            "棒棒糖","苏洋","思索","奥利给","Alex","君临天下"
        };
         
        public static void Show()
        {
     
            using (RedisZSetService service = new RedisZSetService())
            {
     
                service.FlushAll();
                Task.Run(() => //刷礼物---每隔2秒钟就刷一个礼物
                {
     
                    while (true)
                    {
     
                        foreach (var user in userList)
                        {
     
                            Thread.Sleep(20);
                            service.IncrementItemInSortedSet("xut", user, new Random().Next(1, 100));
                        }
                        Thread.Sleep(20 * 100);
                    }
                });
                //实时排行榜
                Task.Run(() =>
                {
     
                    while (true)
                    {
     
                        Console.WriteLine("**************xut房间排行榜******************");
                        Thread.Sleep(5 * 1000);
                        var dic = service.GetAllWithScoresFromSortedSet("Richard");
                        int i = 1;
                        foreach (var item in dic)
                        {
     
                            Console.WriteLine($"第{i++}名   {item.Key} 分数{item.Value}");
                        }
                    }
                });
                Console.Read();
            }

        }
    }

扩展版

using (RedisZSetService service = new RedisZSetService())
                {
     
                    service.KeyFulsh();
                    service.Add("score", "111");
                    service.Add("score", "112");
                    service.Add("score", "113");
                    service.Add("score", "114");
                    service.Add("score", "115");
                    service.Add("score", "111");

                    service.AddItemToSortedSet("user", "Richard1", 1);
                    service.AddItemToSortedSet("user", "Richard2", 2);
                    service.AddItemToSortedSet("user", "Richard3", 5);
                    service.AddItemToSortedSet("user", "Richard4", 3);
                    service.AddItemToSortedSet("user", "1Richard2", 4);


                    var list = service.GetAll("score");
                    var listDesc = service.GetAllDesc("score");

                    var user = service.GetAll("user");
                    var userDesc = service.GetAllDesc("user");
                }

你可能感兴趣的:(redis数据库,redis)