thewar_server/Server/System/Redis.cs

170 lines
4.8 KiB
C#

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StackExchange.Redis;
using static System.Formats.Asn1.AsnWriter;
namespace Server.System
{
public class Redis
{
ConnectionMultiplexer redis;
IDatabase db;
bool local = false;
Dictionary<string, string> stringType;
Dictionary<string, List<string>> listType;
Dictionary<string, List<rank>> zType;
Dictionary<string, Dictionary<string, object>> hashType;
public Redis(string host = "127.0.0.1", int port = 6379)
{
if(host == "localhost")
{
local = true;
stringType = new Dictionary<string, string>();
listType = new Dictionary<string, List<string>>();
zType = new Dictionary<string, List<rank>>();
hashType = new Dictionary<string, Dictionary<string, object>>();
}
else
{
redis = ConnectionMultiplexer.Connect($"{host}:{port}");
db = redis.GetDatabase();
}
test();
}
public void test()
{
SetString("test", "key1");
Console.WriteLine("123123");
Console.WriteLine(GetString<string>("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<T>(params string[] keys)
{
if (local)
return JsonConvert.DeserializeObject<T>(stringType[KeySet(keys)]);
else
return JsonConvert.DeserializeObject<T>(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<string> newdata = [JsonConvert.SerializeObject(value)];
listType.Add(KeySet(keys), newdata);
}
}
else
db.ListRightPush(KeySet(keys), JsonConvert.SerializeObject(value));
}
public void SetList(List<object> value, params string[] keys)
{
foreach(var item in value)
{
SetList(item, keys);
}
}
public T GetList<T>(long index, params string[] keys)
{
if (local)
return JsonConvert.DeserializeObject<T>(listType[KeySet(keys)][(int)index]);
else
return JsonConvert.DeserializeObject<T>(db.ListGetByIndex(KeySet(keys), index));
}
public List<T> GetList<T>(params string[] keys)
{
List<T> result = new List<T>();
if (local)
{
List<string> strings = listType[KeySet(keys)];
foreach (var item in strings)
{
result.Add(JsonConvert.DeserializeObject<T>(item));
}
}
else
{
RedisValue[] strings = db.ListRange(KeySet(keys));
foreach (var item in strings)
{
result.Add(JsonConvert.DeserializeObject<T>(item));
}
}
return result;
}
public void Setz(string value,double score,params string[] keys)
{
Setz(new rank(value, score), keys);
}
public void Setz(rank rank, params string[] keys)
{
if (local)
{
}
else
{
db.SortedSetAdd(KeySet(keys), rank.value, rank.score);
}
}
private string KeySet(string[] keys)
{
return string.Join(":", keys);
}
//public T Test<T>(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;
}
}
}
}