本文章向大家介绍关于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
/// <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
/// <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
>(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
>(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 } });}
推荐阅读
- 多图 服务器的常用配置方法,简单总结
- 怀孕可以吃马铃薯吗?怀孕吃什么比较好
- 冬瓜大麦排骨汤的常见做法
- 排骨家常菜如何吃好排骨
- 酸辣带鱼的家常做法
- 山药和黑鱼一起炖吗
- 全宇宙最全的Linux运维bash脚本常见用法总结
- 推荐一个视频超分模型RealBasicVSR
- 什么是域名301重定向?
- 含答案 全网最全的Java岗网络编程+异常处理面试题