using JNPF.DependencyInjection;
using Microsoft.Extensions.Options;
namespace JNPF.Common.Cache;
///
/// Redis缓存.
///
public class RedisCache : ICache, ISingleton
{
///
/// 构造函数.
///
public RedisCache(IOptions cacheOptions)
{
CSRedis.CSRedisClient? csredis = new CSRedis.CSRedisClient(string.Format(cacheOptions.Value.RedisConnectionString, cacheOptions.Value.ip, cacheOptions.Value.port, cacheOptions.Value.password));
RedisHelper.Initialization(csredis);
}
///
/// 用于在 key 存在时删除 key.
///
/// 键.
public long Del(params string[] key)
{
return RedisHelper.Del(key);
}
///
/// 用于在 key 存在时删除 key.
///
/// 键.
public Task DelAsync(params string[] key)
{
return RedisHelper.DelAsync(key);
}
///
/// 用于在 key 模板存在时删除.
///
/// key模板.
public async Task DelByPatternAsync(string pattern)
{
if (string.IsNullOrEmpty(pattern))
return default;
// pattern = Regex.Replace(pattern, @"\{.*\}", "*");
string[]? keys = await RedisHelper.KeysAsync(pattern);
if (keys?.Length > 0)
{
return await RedisHelper.DelAsync(keys);
}
return default;
}
///
/// 检查给定 key 是否存在.
///
/// 键.
public bool Exists(string key)
{
return RedisHelper.Exists(key);
}
///
/// 检查给定 key 是否存在.
///
/// 键.
public Task ExistsAsync(string key)
{
return RedisHelper.ExistsAsync(key);
}
///
/// 获取指定 key 的增量值.
///
/// 键.
/// 增量.
///
public long Incrby(string key, long incrBy)
{
return RedisHelper.IncrBy(key, incrBy);
}
///
/// 获取指定 key 的增量值.
///
/// 键.
/// 增量.
///
public Task IncrbyAsync(string key, long incrBy)
{
return RedisHelper.IncrByAsync(key, incrBy);
}
///
/// 获取指定 key 的值.
///
/// 键.
public string Get(string key)
{
return RedisHelper.Get(key);
}
///
/// 获取指定 key 的值.
///
/// byte[] 或其他类型.
/// 键.
public T Get(string key)
{
return RedisHelper.Get(key);
}
///
/// 获取指定 key 的值.
///
/// 键.
///
public Task GetAsync(string key)
{
return RedisHelper.GetAsync(key);
}
///
/// 获取指定 key 的值.
///
/// byte[] 或其他类型.
/// 键.
public Task GetAsync(string key)
{
return RedisHelper.GetAsync(key);
}
///
/// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象.
///
/// 键.
/// 值.
public bool Set(string key, object value)
{
return RedisHelper.Set(key, value);
}
///
/// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象.
///
/// 键.
/// 值.
/// 有效期.
public bool Set(string key, object value, TimeSpan expire)
{
return RedisHelper.Set(key, value, expire);
}
///
/// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象.
///
/// 键.
/// 值.
public Task SetAsync(string key, object value)
{
return RedisHelper.SetAsync(key, value);
}
///
/// 保存.
///
/// 键.
/// 值.
/// 过期时间.
///
public Task SetAsync(string key, object value, TimeSpan expire)
{
return RedisHelper.SetAsync(key, value, expire);
}
///
/// 只有在 key 不存在时设置 key 的值.
///
/// 键.
/// 值.
/// 有效期.
public bool SetNx(string key, object value, TimeSpan expire)
{
if (RedisHelper.SetNx(key, value))
{
RedisHelper.Set(key, value, expire);
return true;
}
else
{
return false;
}
}
///
/// 只有在 key 不存在时设置 key 的值.
///
/// 键.
/// 值.
public bool SetNx(string key, object value)
{
return RedisHelper.SetNx(key, value);
}
///
/// 获取所有key.
///
///
public List GetAllKeys()
{
return RedisHelper.Keys("*").ToList();
}
///
/// 获取缓存过期时间.
///
/// 键值.
///
public DateTime GetCacheOutTime(string key)
{
long second = RedisHelper.PTtl(key);
return DateTime.Now.AddMilliseconds(second);
}
public Task GetHash(string key, string field)
{
return RedisHelper.HGetAsync(key, field);
}
public Task HashExist(string key, string field)
{
return RedisHelper.HExistsAsync(key, field);
}
public Task> HGetAll(string key)
{
return RedisHelper.HGetAllAsync(key);
}
public Task HSet(string key, string field, string value)
{
return RedisHelper.HSetAsync(key, field,value);
}
}