using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Channels; using System.Threading.Tasks; using Aspose.Cells.Drawing; using CSRedis; using JNPF; using JNPF.Common.Cache; using JNPF.DependencyInjection; using Microsoft.Extensions.Options; using Newtonsoft.Json; using static CSRedis.CSRedisClient; namespace Tnb.Common.Redis { public class RedisData : ISingleton { private CSRedisClient _instance; public delegate void rcvMsgHandler(string Channel, string Body); public event rcvMsgHandler rcvMsg; public RedisData() { RedisOptions _RedisOptions = App.GetConfig("Redis", true); _instance = new CSRedis.CSRedisClient(string.Format(_RedisOptions.RedisConnectionString, _RedisOptions.ip, _RedisOptions.port, _RedisOptions.password)); //_instance.Subscribe(("devdata_change", msg => //{ // if (rcvMsg != null) // { // rcvMsg(msg.Channel, msg.Body); // } //} //)); } /// /// 用于在 key 存在时删除 key. /// /// 键. public long Del(params string[] key) { return _instance.Del(key); } /// /// 用于在 key 存在时删除 key. /// /// 键. public Task DelAsync(params string[] key) { return _instance.DelAsync(key); } /// /// 用于在 key 模板存在时删除. /// /// key模板. public async Task DelByPatternAsync(string pattern) { if (string.IsNullOrEmpty(pattern)) return default; // pattern = Regex.Replace(pattern, @"\{.*\}", "*"); string[]? keys = await _instance.KeysAsync(pattern); if (keys?.Length > 0) { return await _instance.DelAsync(keys); } return default; } /// /// 检查给定 key 是否存在. /// /// 键. public bool Exists(string key) { return _instance.Exists(key); } /// /// 检查给定 key 是否存在. /// /// 键. public Task ExistsAsync(string key) { return _instance.ExistsAsync(key); } /// /// 获取指定 key 的增量值. /// /// 键. /// 增量. /// public long Incrby(string key, long incrBy) { return _instance.IncrBy(key, incrBy); } /// /// 获取指定 key 的增量值. /// /// 键. /// 增量. /// public Task IncrbyAsync(string key, long incrBy) { return _instance.IncrByAsync(key, incrBy); } /// /// 获取指定 key 的值. /// /// 键. public string Get(string key) { return _instance.Get(key); } /// /// 获取指定 key 的值. /// /// byte[] 或其他类型. /// 键. public T Get(string key) { return _instance.Get(key); } /// /// 获取指定 key 的值. /// /// 键. /// public Task GetAsync(string key) { return _instance.GetAsync(key); } /// /// 获取指定 key 的值. /// /// byte[] 或其他类型. /// 键. public Task GetAsync(string key) { return _instance.GetAsync(key); } /// /// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象. /// /// 键. /// 值. public bool Set(string key, object value) { return _instance.Set(key, value); } /// /// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象. /// /// 键. /// 值. /// 有效期. public bool Set(string key, object value, TimeSpan expire) { return _instance.Set(key, value, expire); } /// /// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象. /// /// 键. /// 值. public Task SetAsync(string key, object value) { return _instance.SetAsync(key, value); } /// /// 保存. /// /// 键. /// 值. /// 过期时间. /// public Task SetAsync(string key, object value, TimeSpan expire) { return _instance.SetAsync(key, value, expire); } /// /// 只有在 key 不存在时设置 key 的值. /// /// 键. /// 值. /// 有效期. public bool SetNx(string key, object value, TimeSpan expire) { if (_instance.SetNx(key, value)) { _instance.Set(key, value, expire); return true; } else { return false; } } /// /// 只有在 key 不存在时设置 key 的值. /// /// 键. /// 值. public bool SetNx(string key, object value) { return _instance.SetNx(key, value); } /// /// 获取所有key. /// /// public List GetAllKeys() { return _instance.Keys("*").ToList(); } /// /// 获取缓存过期时间. /// /// 键值. /// public DateTime GetCacheOutTime(string key) { long second = _instance.PTtl(key); return DateTime.Now.AddMilliseconds(second); } public Task GetHash(string key, string field) { return _instance.HGetAsync(key, field); } public Task HashExist(string key, string field) { return _instance.HExistsAsync(key, field); } public Task> HGetAll(string key) { return _instance.HGetAllAsync(key); } public Task HSet(string key, string field, string value) { return _instance.HSetAsync(key, field, value); } public async Task TryGetValueByKeyField(string key, string field) { if (await _instance.HExistsAsync(key, field)) { string data = await GetHash(key, field); Dictionary json = JsonConvert.DeserializeObject>(data); Type type = typeof(T); if ((!type.IsValueType && type.GetGenericArguments().Length > 0) || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))) { return (T)Convert.ChangeType(json["Value"],type.GetGenericArguments()[0]); } else { return (T)Convert.ChangeType(json["Value"],type); } // return ; } return default(T); } } }