hash작업 완료

This commit is contained in:
김민서 2024-04-03 14:29:16 +09:00
parent 3fcbe9ab34
commit 170171bb56
1 changed files with 60 additions and 2 deletions

View File

@ -18,7 +18,7 @@ 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, string>> 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")
@ -27,7 +27,7 @@ namespace Server.System
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, string>>();
} }
else else
{ {
@ -41,6 +41,19 @@ namespace Server.System
public void test() public void test()
{ {
Console.WriteLine("----------"); Console.WriteLine("----------");
Dictionary<string, object> hash = GetHash("testhash");
foreach (var item in hash)
{
Console.WriteLine($"{item.Key} : {item.Value}");
}
//hash.Add("nickname", "ganada");
//hash.Add("uuid", "addzip");
//hash.Add("id", 1);
//SetHash(hash, "testhash");
//GetHash("testhash");
Console.WriteLine("----------"); Console.WriteLine("----------");
} }
@ -175,6 +188,51 @@ namespace Server.System
return result; return result;
} }
public void SetHash(string field, object value, params string[] keys)
{
if (local)
{
if (hashType.ContainsKey(KeySet(keys)))
hashType.Add(KeySet(keys), new Dictionary<string, string> { { field, JsonConvert.SerializeObject(value) } });
else
{
Dictionary<string, string> userHash = hashType[KeySet(keys)];
if (userHash.ContainsKey(field))
userHash.Add(field, JsonConvert.SerializeObject(value));
else
userHash[field] = JsonConvert.SerializeObject(value);
}
}
else
{
db.HashSet(KeySet(keys), field, JsonConvert.SerializeObject(value));
}
}
public void SetHash(Dictionary<string, object> hash, params string[] keys)
{
foreach (var item in hash)
SetHash(item.Key, item.Value, keys);
}
public Dictionary<string, object> GetHash(params string[] keys)
{
Dictionary<string, object> hash = new Dictionary<string, object>();
if (local)
{
Dictionary<string, string> hashs = new Dictionary<string, string>(hashType[KeySet(keys)]);
foreach (var entry in hashs)
hash.Add(entry.Key, JsonConvert.DeserializeObject(entry.Value));
}
else
{
HashEntry[] hashEntries = db.HashGetAll(KeySet(keys));
foreach (var entry in hashEntries)
hash.Add(entry.Name, entry.Value);
}
return hash;
}
private string KeySet(string[] keys) private string KeySet(string[] keys)
{ {
return string.Join(":", keys); return string.Join(":", keys);