diff --git a/Server/System/Redis.cs b/Server/System/Redis.cs index d33b62b..402dafa 100644 --- a/Server/System/Redis.cs +++ b/Server/System/Redis.cs @@ -18,7 +18,7 @@ namespace Server.System Dictionary stringType; Dictionary> listType; Dictionary> zType; - Dictionary> hashType; + Dictionary> hashType; public Redis(string host = "127.0.0.1", int port = 6379) { if (host == "localhost") @@ -27,7 +27,7 @@ namespace Server.System stringType = new Dictionary(); listType = new Dictionary>(); zType = new Dictionary>(); - hashType = new Dictionary>(); + hashType = new Dictionary>(); } else { @@ -41,6 +41,19 @@ namespace Server.System public void test() { Console.WriteLine("----------"); + Dictionary 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("----------"); } @@ -175,6 +188,51 @@ namespace Server.System 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 { { field, JsonConvert.SerializeObject(value) } }); + else + { + Dictionary 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 hash, params string[] keys) + { + foreach (var item in hash) + SetHash(item.Key, item.Value, keys); + } + + public Dictionary GetHash(params string[] keys) + { + Dictionary hash = new Dictionary(); + if (local) + { + Dictionary hashs = new Dictionary(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) { return string.Join(":", keys);