using FreeRedis; using System; using System.Text.RegularExpressions; using System.Threading.Tasks; using ZhonTai.Common.Extensions; namespace ZhonTai.Admin.Tools.Cache; /// /// Redis缓存 /// public partial class RedisCacheTool : ICacheTool { [GeneratedRegex("\\{.*\\}")] private static partial Regex PatternRegex(); private readonly RedisClient _redisClient; public RedisCacheTool(RedisClient redisClient) { _redisClient = redisClient; } public long Del(params string[] key) { return _redisClient.Del(key); } public Task DelAsync(params string[] key) { return _redisClient.DelAsync(key); } public async Task DelByPatternAsync(string pattern) { if (pattern.IsNull()) return default; pattern = PatternRegex().Replace(pattern, "*"); var keys = await _redisClient.KeysAsync(pattern); if (keys != null && keys.Length > 0) { return await _redisClient.DelAsync(keys); } return default; } public bool Exists(string key) { return _redisClient.Exists(key); } public Task ExistsAsync(string key) { return _redisClient.ExistsAsync(key); } public string Get(string key) { return _redisClient.Get(key); } public T Get(string key) { return _redisClient.Get(key); } public Task GetAsync(string key) { return _redisClient.GetAsync(key); } public Task GetAsync(string key) { return _redisClient.GetAsync(key); } public void Set(string key, object value) { _redisClient.Set(key, value); } public void Set(string key, object value, TimeSpan expire) { _redisClient.Set(key, value, expire); } public Task SetAsync(string key, object value, TimeSpan? expire = null) { return _redisClient.SetAsync(key, value, expire.HasValue ? expire.Value.TotalSeconds.ToInt() : 0); } public async Task GetOrSetAsync(string key, Func> func, TimeSpan? expire = null) { if (await _redisClient.ExistsAsync(key)) { try { return await _redisClient.GetAsync(key); } catch { await _redisClient.DelAsync(key); } } var result = await func.Invoke(); await _redisClient.SetAsync(key, result, expire.HasValue ? expire.Value.TotalSeconds.ToInt() : 0); return result; } }