using System; using System.Collections.Generic; using System.Threading.Tasks; using Newtonsoft.Json; using Serilog; using StackExchange.Redis; using Wood.Cache; using Wood.Util; namespace Wood.RedisCache { public class RedisCacheImp : ICache { private IDatabase cache; private ConnectionMultiplexer connection; public RedisCacheImp() { connection = ConnectionMultiplexer.Connect(GlobalContext.SystemConfig!.RedisConnectionString); cache = connection.GetDatabase(); } public bool SetCache(string key, T value, DateTime? expireTime = null) { try { var jsonOption = new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }; string strValue = JsonConvert.SerializeObject(value, jsonOption); if (string.IsNullOrEmpty(strValue)) { return false; } if (expireTime == null) { return cache.StringSet(key, strValue); } else { return cache.StringSet(key, strValue, (expireTime.Value - DateTime.Now)); } } catch (Exception ex) { Log.Error(ex,"设置缓存失败:key({key})",key); } return false; } public bool RemoveCache(string key) { return cache.KeyDelete(key); } /// /// /// /// /// /// public T? GetCache(string key) { var t = default(T); try { var value = cache.StringGet(key); if (string.IsNullOrEmpty(value)) { return t; } t = JsonConvert.DeserializeObject(value!); } catch (Exception ex) { Log.Error(ex, "获取缓存失败:key({key})", key); } return t; } public bool TryGetCache(string key,out T? val) { val = default(T); try { var value = cache.StringGet(key); if (string.IsNullOrEmpty(value)) { return false; } val = JsonConvert.DeserializeObject(value!); return true; } catch (Exception ex) { Log.Error(ex, "获取缓存失败:key({key})", key); } return false; } #region Hash public int SetHashFieldCache(string key, string fieldKey, T fieldValue) { return SetHashFieldCache(key, new Dictionary { { fieldKey, fieldValue } }); } public int SetHashFieldCache(string key, Dictionary dict) { int count = 0; var jsonOption = new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }; foreach (string fieldKey in dict.Keys) { string fieldValue = JsonConvert.SerializeObject(dict[fieldKey], jsonOption); count += cache.HashSet(key, fieldKey, fieldValue) ? 1 : 0; } return count; } public T GetHashFieldCache(string key, string fieldKey) { var dict = GetHashFieldCache(key, new Dictionary { { fieldKey, default(T)! } }); return dict[fieldKey]; } public Dictionary GetHashFieldCache(string key, Dictionary dict) { foreach (string fieldKey in dict.Keys) { string? fieldValue = cache.HashGet(key, fieldKey); if(fieldValue != null) dict[fieldKey] = JsonConvert.DeserializeObject(fieldValue)!; } return dict; } public Dictionary GetHashCache(string key) { Dictionary dict = new Dictionary(); var hashFields = cache.HashGetAll(key); foreach (HashEntry field in hashFields) { dict[field.Name!] = JsonConvert.DeserializeObject(field.Value!)!; } return dict; } public List GetHashToListCache(string key) { List list = new List(); var hashFields = cache.HashGetAll(key); foreach (HashEntry field in hashFields) { list.Add(JsonConvert.DeserializeObject(field.Value!)!); } return list; } public bool RemoveHashFieldCache(string key, string fieldKey) { Dictionary dict = new Dictionary { { fieldKey, false } }; dict = RemoveHashFieldCache(key, dict); return dict[fieldKey]; } public Dictionary RemoveHashFieldCache(string key, Dictionary dict) { foreach (string fieldKey in dict.Keys) { dict[fieldKey] = cache.HashDelete(key, fieldKey); } return dict; } #endregion public void Dispose() { if (connection != null) { connection.Close(); } GC.SuppressFinalize(this); } } }