using NetTopologySuite.Index.HPRtree; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using StackExchange.Redis; using static Server.System.Redis; using static System.Formats.Asn1.AsnWriter; 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() { Console.WriteLine("----------"); Setz("test1",1.5d, "ListTest"); Setz("test3",3.5d, "ListTest"); Setz("test2",2.5d, "ListTest"); Console.WriteLine("----------"); } public void SetString(object value, params string[] keys) { if (local) { if (stringType.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(T value, params string[] keys) { if (local) { if (listType.ContainsKey(KeySet(keys))) listType[KeySet(keys)].Add(JsonConvert.SerializeObject(value)); else listType.Add(KeySet(keys), [JsonConvert.SerializeObject(value)]); } else db.ListRightPush(KeySet(keys), JsonConvert.SerializeObject(value)); } public void SetList(List value, params string[] keys) { foreach (var item in value) { SetList(item, keys); } } public T GetList(long index, params string[] keys) { if (local) return JsonConvert.DeserializeObject(listType[KeySet(keys)][(int)index]); else return JsonConvert.DeserializeObject(db.ListGetByIndex(KeySet(keys), index)); } public List GetList(params string[] keys) { List result = new List(); if (local) { List strings = listType[KeySet(keys)]; foreach (var item in strings) { result.Add(JsonConvert.DeserializeObject(item)); } } else { RedisValue[] strings = db.ListRange(KeySet(keys)); foreach (var item in strings) { result.Add(JsonConvert.DeserializeObject(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) { 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 { db.SortedSetAdd(KeySet(keys), rank.value, rank.score); } } private string KeySet(string[] keys) { return string.Join(":", keys); } public class Rank { public string value; public double score; public Rank(string value, double score) { this.value = value; this.score = score; } } } }