Setz세팅

This commit is contained in:
김민서 2024-04-03 10:28:35 +09:00
parent cdb167f19b
commit c1514e2396
1 changed files with 27 additions and 36 deletions

View File

@ -1,6 +1,8 @@
using Newtonsoft.Json; using NetTopologySuite.Index.HPRtree;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using StackExchange.Redis; using StackExchange.Redis;
using static Server.System.Redis;
using static System.Formats.Asn1.AsnWriter; using static System.Formats.Asn1.AsnWriter;
namespace Server.System namespace Server.System
@ -14,16 +16,16 @@ namespace Server.System
Dictionary<string, string> stringType; Dictionary<string, string> stringType;
Dictionary<string, List<string>> listType; Dictionary<string, List<string>> listType;
Dictionary<string, List<rank>> zType; Dictionary<string, List<Rank>> zType;
Dictionary<string, Dictionary<string, object>> hashType; Dictionary<string, Dictionary<string, object>> hashType;
public Redis(string host = "127.0.0.1", int port = 6379) public Redis(string host = "127.0.0.1", int port = 6379)
{ {
if(host == "localhost") if (host == "localhost")
{ {
local = true; local = true;
stringType = new Dictionary<string, string>(); stringType = new Dictionary<string, string>();
listType = new Dictionary<string, List<string>>(); listType = new Dictionary<string, List<string>>();
zType = new Dictionary<string, List<rank>>(); zType = new Dictionary<string, List<Rank>>();
hashType = new Dictionary<string, Dictionary<string, object>>(); hashType = new Dictionary<string, Dictionary<string, object>>();
} }
else else
@ -37,24 +39,22 @@ namespace Server.System
public void test() public void test()
{ {
SetString("test", "key1"); Console.WriteLine("----------");
Console.WriteLine("123123"); Setz("test1",1.5d, "ListTest");
Console.WriteLine(GetString<string>("key1")); Setz("test3",3.5d, "ListTest");
Console.WriteLine("123123"); Setz("test2",2.5d, "ListTest");
Console.WriteLine("----------");
} }
public void SetString(object value, params string[] keys) public void SetString(object value, params string[] keys)
{ {
if (local) if (local)
{ {
if (listType.ContainsKey(KeySet(keys))) if (stringType.ContainsKey(KeySet(keys)))
{
stringType[KeySet(keys)] = JsonConvert.SerializeObject(value); stringType[KeySet(keys)] = JsonConvert.SerializeObject(value);
}
else else
{
stringType.Add(KeySet(keys), JsonConvert.SerializeObject(value)); stringType.Add(KeySet(keys), JsonConvert.SerializeObject(value));
}
} }
else else
db.StringSet(KeySet(keys), JsonConvert.SerializeObject(value)); db.StringSet(KeySet(keys), JsonConvert.SerializeObject(value));
@ -68,27 +68,22 @@ namespace Server.System
return JsonConvert.DeserializeObject<T>(db.StringGet(key: KeySet(keys))); return JsonConvert.DeserializeObject<T>(db.StringGet(key: KeySet(keys)));
} }
public void SetList(object value, params string[] keys) public void SetList<T>(T value, params string[] keys)
{ {
if (local) if (local)
{ {
if (listType.ContainsKey(KeySet(keys))) if (listType.ContainsKey(KeySet(keys)))
{
listType[KeySet(keys)].Add(JsonConvert.SerializeObject(value)); listType[KeySet(keys)].Add(JsonConvert.SerializeObject(value));
}
else else
{ listType.Add(KeySet(keys), [JsonConvert.SerializeObject(value)]);
List<string> newdata = [JsonConvert.SerializeObject(value)];
listType.Add(KeySet(keys), newdata);
}
} }
else else
db.ListRightPush(KeySet(keys), JsonConvert.SerializeObject(value)); db.ListRightPush(KeySet(keys), JsonConvert.SerializeObject(value));
} }
public void SetList(List<object> value, params string[] keys) public void SetList<T>(List<T> value, params string[] keys)
{ {
foreach(var item in value) foreach (var item in value)
{ {
SetList(item, keys); SetList(item, keys);
} }
@ -124,18 +119,21 @@ namespace Server.System
return result; 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) if (local)
{ {
if (zType.ContainsKey(KeySet(keys)))
zType[KeySet(keys)].Add(rank);
else
zType.Add(KeySet(keys), [rank]);
zType[KeySet(keys)].OrderBy(r => r.score).ToList();
} }
else else
{ {
db.SortedSetAdd(KeySet(keys), rank.value, rank.score); db.SortedSetAdd(KeySet(keys), rank.value, rank.score);
@ -147,19 +145,12 @@ namespace Server.System
return string.Join(":", keys); return string.Join(":", keys);
} }
//public T Test<T>(string key, int index) public class Rank
//{
// // Implementation logic here
// // You can access 'value' class via typeof(T)
// return typeof(T) null;
//}
public class rank
{ {
public string value; public string value;
public double score; public double score;
public rank(string value, double score) public Rank(string value, double score)
{ {
this.value = value; this.value = value;
this.score = score; this.score = score;