You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

188 lines
5.5 KiB

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<T>(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);
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public T? GetCache<T>(string key)
{
var t = default(T);
try
{
var value = cache.StringGet(key);
if (string.IsNullOrEmpty(value))
{
return t;
}
t = JsonConvert.DeserializeObject<T>(value!);
}
catch (Exception ex)
{
Log.Error(ex, "获取缓存失败:key({key})", key);
}
return t;
}
public bool TryGetCache<T>(string key,out T? val)
{
val = default(T);
try
{
var value = cache.StringGet(key);
if (string.IsNullOrEmpty(value))
{
return false;
}
val = JsonConvert.DeserializeObject<T>(value!);
return true;
}
catch (Exception ex)
{
Log.Error(ex, "获取缓存失败:key({key})", key);
}
return false;
}
#region Hash
public int SetHashFieldCache<T>(string key, string fieldKey, T fieldValue)
{
return SetHashFieldCache<T>(key, new Dictionary<string, T> { { fieldKey, fieldValue } });
}
public int SetHashFieldCache<T>(string key, Dictionary<string, T> 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<T>(string key, string fieldKey)
{
var dict = GetHashFieldCache<T>(key, new Dictionary<string, T> { { fieldKey, default(T)! } });
return dict[fieldKey];
}
public Dictionary<string, T> GetHashFieldCache<T>(string key, Dictionary<string, T> dict)
{
foreach (string fieldKey in dict.Keys)
{
string? fieldValue = cache.HashGet(key, fieldKey);
if(fieldValue != null)
dict[fieldKey] = JsonConvert.DeserializeObject<T>(fieldValue)!;
}
return dict;
}
public Dictionary<string, T> GetHashCache<T>(string key)
{
Dictionary<string, T> dict = new Dictionary<string, T>();
var hashFields = cache.HashGetAll(key);
foreach (HashEntry field in hashFields)
{
dict[field.Name!] = JsonConvert.DeserializeObject<T>(field.Value!)!;
}
return dict;
}
public List<T> GetHashToListCache<T>(string key)
{
List<T> list = new List<T>();
var hashFields = cache.HashGetAll(key);
foreach (HashEntry field in hashFields)
{
list.Add(JsonConvert.DeserializeObject<T>(field.Value!)!);
}
return list;
}
public bool RemoveHashFieldCache(string key, string fieldKey)
{
Dictionary<string, bool> dict = new Dictionary<string, bool> { { fieldKey, false } };
dict = RemoveHashFieldCache(key, dict);
return dict[fieldKey];
}
public Dictionary<string, bool> RemoveHashFieldCache(string key, Dictionary<string, bool> 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);
}
}
}