From eba597fb5102a091d96ad33a8541f3b082c17e23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=84=9C?= Date: Tue, 2 Apr 2024 15:11:26 +0900 Subject: [PATCH] =?UTF-8?q?redis=20=EC=9E=91=EC=97=85=20=EC=97=85=EB=A1=9C?= =?UTF-8?q?=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/System/Redis.cs | 108 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 1 deletion(-) diff --git a/Server/System/Redis.cs b/Server/System/Redis.cs index 50387f6..483208a 100644 --- a/Server/System/Redis.cs +++ b/Server/System/Redis.cs @@ -1,7 +1,113 @@ -namespace Server.System +using Newtonsoft.Json; +using StackExchange.Redis; + +namespace Server.System { public class Redis { + ConnectionMultiplexer redis; + IDatabase db; + bool local = false; + + Dictionary stringType; + Dictionary> listType; + Dictionary> zType; + Dictionary> hashType; + public Redis(string host = "127.0.0.1", int port = 6379) + { + if(host == "localhost") + { + local = true; + stringType = new Dictionary(); + listType = new Dictionary>(); + zType = new Dictionary>(); + hashType = new Dictionary>(); + } + else + { + redis = ConnectionMultiplexer.Connect($"{host}:{port}"); + db = redis.GetDatabase(); + } + + test(); + } + + public void test() + { + SetString("test", "key1"); + Console.WriteLine("123123"); + Console.WriteLine(GetString("key1")); + Console.WriteLine("123123"); + } + + public void SetString(object value, params string[] keys) + { + if (local) + { + if (listType.ContainsKey(KeySet(keys))) + { + stringType[KeySet(keys)] = JsonConvert.SerializeObject(value); + } + else + { + stringType.Add(KeySet(keys), JsonConvert.SerializeObject(value)); + } + } + else + db.StringSet(KeySet(keys), JsonConvert.SerializeObject(value)); + } + + public T GetString(params string[] keys) + { + if (local) + return JsonConvert.DeserializeObject(stringType[KeySet(keys)]); + else + return JsonConvert.DeserializeObject(db.StringGet(key: KeySet(keys))); + } + + public void SetList(object value, params string[] keys) + { + if (local) + { + if (listType.ContainsKey(KeySet(keys))) + { + listType[KeySet(keys)].Add(JsonConvert.SerializeObject(value)); + } + else + { + List newdata = [JsonConvert.SerializeObject(value)]; + listType.Add(KeySet(keys), newdata); + } + } + else + db.ListRightPush(KeySet(keys), JsonConvert.SerializeObject(value)); + } + + + + private string KeySet(string[] keys) + { + return string.Join(":", keys); + } + + //public T Test(string key, int index) + //{ + // // Implementation logic here + // // You can access 'value' class via typeof(T) + // return typeof(T) null; + //} + + public class rank + { + public string value; + public double score; + + public rank(string value, double score) + { + this.value = value; + this.score = score; + } + } } }