mh_frame_sps/Apt.Infrastructure.Utility/Redis/RedisCache.cs

204 lines
5.6 KiB
C#
Raw Permalink Normal View History

2026-04-07 13:47:52 +08:00
using Newtonsoft.Json;
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace APT.Infrastructure.Utility
{
public class RedisCache
{
public string GetString(string key)
{
return GetStringAsync(key).Result;
}
public async Task<string> GetStringAsync(string key)
{
try
{
string ReturnString = null;
var val = await RedisHelper.GetAsync(key);
if (!string.IsNullOrEmpty(val))
{
ReturnString = val;
}
return ReturnString;
}
catch (Exception ex)
{
//LoggerManager.GetLogger().Error(ex.ToString());
return null;
}
}
public T Get<T>(string key)
{
return GetAsync<T>(key).Result;
}
public async Task<T> GetAsync<T>(string key)
{
try
{
var value = await GetStringAsync(key);
if (!string.IsNullOrEmpty(value))
{
return JsonConvert.DeserializeObject<T>(value);
}
return default(T);
}
catch (Exception ex)
{
//LoggerManager.GetLogger().Error(ex.ToString());
return default(T);
}
}
public T GetWithConfig<T>(string key) where T : class
{
return this.GetWithConfigAsync<T>(key).Result;
}
public async Task<T> GetWithConfigAsync<T>(string key) where T : class
{
try
{
var redisclass = CsRedisHelper<T>.GetRedisClass();
var value = await GetStringAsync($"{redisclass.ClassName}_{key}");
if (!string.IsNullOrEmpty(value))
{
return JsonConvert.DeserializeObject<T>(value);
}
return default(T);
}
catch (Exception ex)
{
//LoggerManager.GetLogger().Error(ex.ToString());
return default(T);
}
}
public void SetString(string key, string value, TimeSpan? expiredSpan = null)
{
this.SetStringAsync(key, value, expiredSpan).Wait();
}
public async Task SetStringAsync(string key, string value, TimeSpan? expiredSpan = null)
{
try
{
if (expiredSpan == null)
expiredSpan = new TimeSpan(90, 0, 0, 0);//默认90天
var expir = GetExpir(expiredSpan);
await RedisHelper.SetAsync(key, value, expir);
}
catch (Exception ex)
{
//LoggerManager.GetLogger().Error(ex.ToString());
}
}
public void SetString<T>(string key, T value, TimeSpan? expiredSpan = null)
{
this.SetStringAsync<T>(key, value, expiredSpan).Wait();
}
public async Task SetStringAsync<T>(string key, T value, TimeSpan? expiredSpan = null)
{
try
{
if (value == null)
{
return;
}
if (expiredSpan == null)
expiredSpan = new TimeSpan(90, 0, 0, 0);//默认90天
var val = SerializeObject(value);
var expir = GetExpir(expiredSpan);
await RedisHelper.SetAsync(key, val, expir);
}
catch (Exception ex)
{
//LoggerManager.GetLogger().Error(ex.ToString());
}
}
public bool Remove(string key)
{
return this.RemoveAsync(key).Result;
}
public async Task<bool> RemoveAsync(string key)
{
try
{
bool ReturnBool = false;
if (key != "" || key != null)
{
await RedisHelper.DelAsync(key);
if (Exists(key) == false)
{
ReturnBool = true;
}
}
return ReturnBool;
}
catch (Exception ex)
{
//LoggerManager.GetLogger().Error(ex.ToString());
return false;
}
}
public bool Exists(string key)
{
try
{
bool ReturnBool = true;
var val = RedisHelper.Get(key);
if (string.IsNullOrEmpty(val))
{
ReturnBool = false;
}
return ReturnBool;
}
catch (Exception ex)
{
//LoggerManager.GetLogger().Error(ex.ToString());
return false;
}
}
private string SerializeObject<T>(T t)
{
return JsonConvert.SerializeObject(t, Formatting.Indented,
new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
}
private int GetExpir(TimeSpan? expiredSpan)
{
var days = expiredSpan.Value.Days;
var hours = expiredSpan.Value.Hours;
var Minutes = expiredSpan.Value.Minutes;
var seconds = expiredSpan.Value.Seconds;
var expir = days * 24 * 60 * 60 + hours * 60 * 60 + Minutes * 60 + seconds;
return expir;
}
}
}