From 3fcbe9ab3431b01b099e0947851eb010b439c7f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=84=9C?= Date: Wed, 3 Apr 2024 12:16:39 +0900 Subject: [PATCH] =?UTF-8?q?Rank=20=EC=9E=91=EC=97=85=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/System/Redis.cs | 48 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/Server/System/Redis.cs b/Server/System/Redis.cs index f5765d7..d33b62b 100644 --- a/Server/System/Redis.cs +++ b/Server/System/Redis.cs @@ -2,6 +2,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using StackExchange.Redis; +using System.Collections.Generic; using static Server.System.Redis; using static System.Formats.Asn1.AsnWriter; @@ -40,9 +41,6 @@ namespace Server.System public void test() { Console.WriteLine("----------"); - Setz("test1",1.5d, "ListTest"); - Setz("test3",3.5d, "ListTest"); - Setz("test2",2.5d, "ListTest"); Console.WriteLine("----------"); } @@ -119,12 +117,12 @@ namespace Server.System return result; } - public void Setz(string value, double score, params string[] keys) + public void SetZ(string value, double score, params string[] keys) { - Setz(new Rank(value, score), keys); + SetZ(new Rank(value, score), keys); } - public void Setz(Rank rank, params string[] keys) + public void SetZ(Rank rank, params string[] keys) { if (local) { @@ -140,6 +138,43 @@ namespace Server.System } } + public double GetZScore(string value, params string[] keys) + { + if (local) + return zType[KeySet(keys)].Find(n => n.value == value).score; + else + return (double)db.SortedSetScore(KeySet(keys), value); + } + + public long GetZRank(string value, params string[] keys) + { + if (local) + return zType[KeySet(keys)].FindIndex(n => n.value == value) + 1; + else + return (long)db.SortedSetRank(KeySet(keys), value); + } + + public List GetZ(long rank, params string[] keys) + { + List result = new List(); + if (local) + { + List ranking = new List(zType[KeySet(keys)]); + if (ranking.Count > rank) + for (long n = 0; n < rank; n++) + result.Add(ranking[(int)n]); + else + result = new List(ranking); + } + else + { + var topRankEntries = db.SortedSetRangeByRank(KeySet(keys), 0, rank - 1); + foreach (var entry in topRankEntries) + result.Add(new Rank(entry, GetZScore(entry, keys))); + } + return result; + } + private string KeySet(string[] keys) { return string.Join(":", keys); @@ -158,3 +193,4 @@ namespace Server.System } } } +