一个常用的RedisHelper类

本文章向大家介绍关于StackExchange.redis的一些总结 , 算是综合一个网上资料与自己的总结写的一个JOSN转换类 , 有一定的参考价值 , 需要的朋友可以参考一下 。
private IDatabase cache; private ConnectionMultiplexer connection;构造
/// <summary>/// 构造/// </summary>public RedisCacheImp(){connection = ConnectionMultiplexer.Connect(SystemConfig.RedisConnectionString);//这个是Redis连接cache = connection.GetDatabase();}public RedisCacheImp(string connectionString){connection = ConnectionMultiplexer.Connect(connectionString);//这个是Redis连接cache = connection.GetDatabase();}//判断键值是否存在public bool IsExist(string key){return cache.KeyExists(key);}/// <summary>/// 设置键值/// </summary>/// <typeparam name="T"></typeparam>/// <param name="key"></param>/// <param name="value"></param>/// <param name="expireTime"></param>/// <returns></returns>public bool SetCache<T>(string key, T value, DateTime? expireTime = null){try{var jsonOption = new JsonSerializerSettings(){ReferenceLoopHandling = ReferenceLoopHandling.Ignore};string strValue = https://www.isolves.com/it/sjk/Redis/2022-03-21/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){LogHelper.Error(ex);}return false;}【一个常用的RedisHelper类】/// <summary>/// 异步的方法/// </summary>/// <typeparam name="T"></typeparam>/// <param name="key"></param>/// <param name="value"></param>/// <param name="expireTime"></param>/// <returns></returns>public async Task<bool> SetCacheAsync<T>(string key, T value, DateTime? expireTime = null){try{var jsonOption = new JsonSerializerSettings(){ReferenceLoopHandling = ReferenceLoopHandling.Ignore};string strValue = https://www.isolves.com/it/sjk/Redis/2022-03-21/JsonConvert.SerializeObject(value, jsonOption);if (string.IsNullOrEmpty(strValue)){return false;}if (expireTime == null){return await cache.StringSetAsync(key, strValue);}else{return await cache.StringSetAsync(key, strValue, (expireTime.Value - DateTime.Now));}}catch (Exception ex){LogHelper.Error(ex);}return false;}/// <summary>/// 删除缓存/// </summary>/// <param name="key"></param>/// <returns></returns>public bool RemoveCache(string key){return cache.KeyDelete(key);}/// <summary>/// 取得Cache/// </summary>/// <typeparam name="T"></typeparam>/// <param name="key"></param>/// <returns></returns>public T GetCache<T>(string key){var t = default(T);try{var value = https://www.isolves.com/it/sjk/Redis/2022-03-21/cache.StringGet(key);if (string.IsNullOrEmpty(value)){return t;}t = JsonConvert.DeserializeObject(value);}catch (Exception ex){LogHelper.Error(ex);}return t;}/// <summary>/// 异步取得信息/// </summary>/// <typeparam name="T"></typeparam>/// <param name="key"></param>/// <returns></returns>public async Task<T> GetCacheAsync<T>(string key){var t = default(T);try{var value = https://www.isolves.com/it/sjk/Redis/2022-03-21/await cache.StringGetAsync(key);if (string.IsNullOrEmpty(value)){return t;}t = JsonConvert.DeserializeObject(value);}catch (Exception ex){LogHelper.Error(ex);}return t;}/// <summary>/// 取得List缓存/// </summary>/// <typeparam name="T"></typeparam>/// <param name="key"></param>/// <returns></returns>public List<T> GetCaches<T>(string key){var t = default(List<T>);try{var value = https://www.isolves.com/it/sjk/Redis/2022-03-21/cache.StringGet(key);if (string.IsNullOrEmpty(value)){return t;}t = (List)JsonConvert.DeserializeObject>(value);}catch (Exception ex){LogHelper.Error(ex);}return t;}/// <summary>/// 异步返回List/// </summary>/// <typeparam name="T"></typeparam>/// <param name="key"></param>/// <returns></returns>public async Task<List<T>> GetCachesAsync<T>(string key){var t = default(List<T>);try{var value =https://www.isolves.com/it/sjk/Redis/2022-03-21/await cache.StringGetAsync(key);if (string.IsNullOrEmpty(value)){return t;}t = (List)JsonConvert.DeserializeObject>(value);}catch (Exception ex){LogHelper.Error(ex);}return t;}public int SetHashFieldCache<T>(string key, string fieldKey, T fieldValue){return SetHashFieldCache<T>(key, new Dictionary<string, T> { { fieldKey, fieldValue } });}


推荐阅读