275 lines
8.8 KiB
C#
275 lines
8.8 KiB
C#
using Newtonsoft.Json;
|
|
using StackExchange.Redis;
|
|
|
|
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, string>> 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, string>>();
|
|
}
|
|
else
|
|
{
|
|
redis = ConnectionMultiplexer.Connect($"{host}:{port}");
|
|
db = redis.GetDatabase();
|
|
}
|
|
}
|
|
|
|
public void test()
|
|
{
|
|
Console.WriteLine("--------------------");
|
|
|
|
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<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<T>(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 UpdateList<T>(int index, T value, params string[] keys)
|
|
{
|
|
if (local)
|
|
listType[KeySet(keys)][index] = JsonConvert.SerializeObject(value);
|
|
else
|
|
db.ListSetByIndex(KeySet(keys), index, JsonConvert.SerializeObject(value));
|
|
}
|
|
|
|
public void RemoveList<T>(int index, T value, params string[] keys)
|
|
{
|
|
if (local)
|
|
listType[KeySet(keys)].RemoveAt(index);
|
|
else
|
|
db.ListRemove(KeySet(keys), JsonConvert.SerializeObject(value), index);
|
|
}
|
|
|
|
public void SetList<T>(List<T> 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)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
|
|
public double GetZScore(string value, params string[] keys)
|
|
{
|
|
if (local)
|
|
return zType[KeySet(keys)].Find(n => n.value == value).score;
|
|
else
|
|
return (double)db.SortedSetScore(KeySet(keys), value);
|
|
}
|
|
|
|
public long GetZRank(string value, params string[] keys)
|
|
{
|
|
if (local)
|
|
return zType[KeySet(keys)].FindIndex(n => n.value == value) + 1;
|
|
else
|
|
return (long)db.SortedSetRank(KeySet(keys), value);
|
|
}
|
|
|
|
public List<Rank> GetZ(long rank, params string[] keys)
|
|
{
|
|
List<Rank> result = new List<Rank>();
|
|
if (local)
|
|
{
|
|
List<Rank> ranking = new List<Rank>(zType[KeySet(keys)]);
|
|
if (ranking.Count > rank)
|
|
for (long n = 0; n < rank; n++)
|
|
result.Add(ranking[(int)n]);
|
|
else
|
|
result = new List<Rank>(ranking);
|
|
}
|
|
else
|
|
{
|
|
var topRankEntries = db.SortedSetRangeByRank(KeySet(keys), 0, rank - 1);
|
|
foreach (var entry in topRankEntries)
|
|
result.Add(new Rank(entry, GetZScore(entry, keys)));
|
|
}
|
|
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> GetAllHash(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;
|
|
}
|
|
|
|
public object GetHash(string field ,params string[] keys)
|
|
{
|
|
if (local)
|
|
{
|
|
Dictionary<string, string> hashs = new Dictionary<string, string>(hashType[KeySet(keys)]);
|
|
return hashs[field];
|
|
}
|
|
else
|
|
return db.HashGet(KeySet(keys), field);
|
|
}
|
|
|
|
public void RemoveKey(params string[] keys)
|
|
{
|
|
if (local)
|
|
{
|
|
stringType.Remove(KeySet(keys));
|
|
listType.Remove(KeySet(keys));
|
|
zType.Remove(KeySet(keys));
|
|
hashType.Remove(KeySet(keys));
|
|
}
|
|
else
|
|
db.KeyDelete(KeySet(keys));
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|