add key=RedisServers value=172.20.2.90:9379,password=Aa+123456789 /using StackExchange.Redis;using System;using System.Collections.Generic;using System.Linq;namespace APP.Common{/// summary...
<add key="RedisServers" value="172.20.2.90:9379,password=Aa+123456789" />
using StackExchange.Redis; using System; using System.Collections.Generic; using System.Linq; namespace APP.Common { /// <summary> /// StackExchangeRedis帮助类 /// </summary> public sealed class RedisHelper { /// <summary> /// Redis服务器地址 /// </summary> private static readonly string ConnectionString = System.Configuration.ConfigurationManager.AppSettings["RedisServers"]; /// <summary> /// 静态变量锁 /// </summary> private static object _locker = new Object(); /// <summary> /// 静态实例 /// </summary> private static ConnectionMultiplexer _instance = null; /// <summary> /// 使用一个静态属性来返回已连接的实例,如下列中所示。这样,一旦 ConnectionMultiplexer 断开连接,便可以初始化新的连接实例。 /// </summary> private static ConnectionMultiplexer Instance { get { try { if (_instance == null) { lock (_locker) { if (_instance == null || !_instance.IsConnected) { _instance = ConnectionMultiplexer.Connect(ConnectionString); //注册如下事件 _instance.ConnectionFailed += MuxerConnectionFailed; _instance.ConnectionRestored += MuxerConnectionRestored; _instance.ErrorMessage += MuxerErrorMessage; _instance.ConfigurationChanged += MuxerConfigurationChanged; _instance.HashSlotMoved += MuxerHashSlotMoved; _instance.InternalError += MuxerInternalError; } } } } catch (Exception ex) { LogHelper.Error(typeof(RedisHelper), string.Format("redis初始化异常,连接字符串={0}", ConnectionString), ex); } return _instance; } } /// <summary> /// 获取redis数据库对象 /// </summary> /// <returns></returns> private static IDatabase GetDatabase() { return Instance.GetDatabase(); } /// <summary> /// 检查Key是否存在 /// </summary> /// <param name="key"></param> /// <returns></returns> public static bool Exists(string key) { if (string.IsNullOrWhiteSpace(key)) { return false; } try { return GetDatabase().KeyExists(key); } catch (Exception ex) { LogHelper.Error(typeof(RedisHelper), string.Format("检查Key是否存在异常,缓存key={0}", key), ex); } return false; } /// <summary> /// 设置String类型的缓存对象(如果value是null或者空字符串则设置失败) /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="ts">过期时间</param> public static bool SetString(string key, string value, TimeSpan? ts = null) { if (string.IsNullOrWhiteSpace(value)) { return false; } try { return GetDatabase().StringSet(key, value, ts); } catch (Exception ex) { LogHelper.Error(typeof(RedisHelper), string.Format("设置string类型缓存异常,缓存key={0},缓存值={1}", key, value), ex); } return false; } /// <summary> /// 根据key获取String类型的缓存对象 /// </summary> /// <param name="key"></param> /// <returns></returns> public static string GetString(string key) { try { return GetDatabase().StringGet(key); } catch (Exception ex) { LogHelper.Error(typeof(RedisHelper), string.Format("获取string类型缓存异常,缓存key={0}", key), ex); } return null; } /// <summary> /// 删除缓存 /// </summary> /// <param name="key">key</param> /// <returns></returns> public static bool KeyDelete(string key) { try { return GetDatabase().KeyDelete(key); } catch (Exception ex) { LogHelper.Error(typeof(RedisHelper), "删除缓存异常,缓存key={0}" + key, ex); return false; } } /// <summary> /// 设置Hash类型缓存对象(如果value没有公共属性则不设置缓存) /// 会使用反射将object对象所有公共属性作为Hash列存储 /// </summary> /// <param name="key"></param> /// <param name="value"></param> public static void SetHash(string key, object value) { if (null == value) { return; } try { List<HashEntry> list = new List<HashEntry>(); Type type = value.GetType(); var propertyArray = type.GetProperties(); foreach (var property in propertyArray) { string propertyName = property.Name; string propertyValue = property.GetValue(value).ToString(); list.Add(new HashEntry(propertyName, propertyValue)); } if (list.Count < 1) { return; } IDatabase db = GetDatabase(); db.HashSet(key, list.ToArray()); } catch (Exception ex) { LogHelper.Error(typeof(RedisHelper), string.Format("设置Hash类型缓存异常,缓存key={0},缓存值={1}", key, Utils.SerializeObject(value)), ex); } } /// <summary> /// 设置Hash类型缓存对象(用于存储对象) /// </summary> /// <param name="key">Key</param> /// <param name="value">字典,key是列名 value是列的值</param> public static void SetHash(string key, Dictionary<string, string> value) { if (null == value || value.Count < 1) { return; } try { HashEntry[] array = (from item in value select new HashEntry(item.Key, item.Value)).ToArray(); IDatabase db = GetDatabase(); db.HashSet(key, array); } catch (Exception ex) { LogHelper.Error(typeof(RedisHelper), string.Format("设置Hash类型缓存异常,缓存key={0},缓存对象值={1}", key, string.Join(",", value)), ex); } } /// <summary> /// 根据key和列数组从缓存中拿取数据(如果fieldList为空或者个数小于0返回null) /// </summary> /// <param name="key">缓存Key</param> /// <param name="fieldList">列数组</param> /// <returns>根据列数组构造一个字典,字典中的列与入参列数组相同,字典中的值是每一列的值</returns> public static Dictionary<string, string> GetHash(string key, List<string> fieldList) { if (null == fieldList || fieldList.Count < 1) { return null; } try { Dictionary<string, string> dic = new Dictionary<string, string>(); RedisValue[] array = (from item in fieldList select (RedisValue)item).ToArray(); IDatabase db = GetDatabase(); RedisValue[] redisValueArray = db.HashGet(key, array); for (int i = 0; i < redisValueArray.Length; i++) { string field = fieldList[i]; string value = redisValueArray[i]; dic.Add(field, value); } return dic; } catch (Exception ex) { LogHelper.Error(typeof(RedisHelper), string.Format("获取Hash类型缓存异常,缓存key={0},列数组={1}", key, string.Join(",", fieldList)), ex); } return null; } /// <summary> /// 使用Redis incr 记录某个Key的调用次数 /// </summary> /// <param name="key"></param> public static long SaveInvokeCount(string key) { try { return GetDatabase().StringIncrement(key); } catch { return -1; } } /// <summary> /// 配置更改时 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void MuxerConfigurationChanged(object sender, EndPointEventArgs e) { LogHelper.Warn(typeof(RedisHelper), "MuxerConfigurationChanged=>e.EndPoint=" + e.EndPoint, null); } /// <summary> /// 发生错误时 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void MuxerErrorMessage(object sender, RedisErrorEventArgs e) { LogHelper.Error(typeof(RedisHelper), "MuxerErrorMessage=>e.EndPoint=" + e.EndPoint + ",e.Message=" + e.Message, null); } /// <summary> /// 重新建立连接 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void MuxerConnectionRestored(object sender, ConnectionFailedEventArgs e) { LogHelper.Warn(typeof(RedisHelper), "MuxerConnectionRestored=>e.ConnectionType=" + e.ConnectionType + ",e.EndPoint=" + e.EndPoint + ",e.FailureType=" + e.FailureType, e.Exception); } /// <summary> /// 连接失败 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void MuxerConnectionFailed(object sender, ConnectionFailedEventArgs e) { LogHelper.Error(typeof(RedisHelper), "MuxerConnectionFailed=>e.ConnectionType=" + e.ConnectionType + ",e.EndPoint=" + e.EndPoint + ",e.FailureType=" + e.FailureType, e.Exception); } /// <summary> /// 更改集群 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void MuxerHashSlotMoved(object sender, HashSlotMovedEventArgs e) { LogHelper.Warn(typeof(RedisHelper), "MuxerHashSlotMoved=>" + e.NewEndPoint + ", OldEndPoint" + e.OldEndPoint, null); } /// <summary> /// redis类库错误 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void MuxerInternalError(object sender, InternalErrorEventArgs e) { LogHelper.Error(typeof(RedisHelper), "MuxerInternalError", e.Exception); } } }
//写String 缓存1小时 RedisHelper.SetString(subID, "AXB", new TimeSpan(1, 0, 0, 0)); //写String 缓存5分钟 RedisHelper.SetString(mobile + "_car", equipmentType, TimeSpan.FromMinutes(5)); //写String RedisHelper.SetString(strNum, strCity); //读String string strTime = RedisHelper.GetString(mobile);
沃梦达教程
本文标题为:RedisHelper (C#)
基础教程推荐
猜你喜欢
- C#控件picturebox实现图像拖拽和缩放 2022-12-31
- 解决unity3d导入模型贴图材质丢失的问题 2023-04-14
- C#设计模式之Mediator中介者模式解决程序员的七夕缘分问题示例 2022-11-15
- C#判断DLL文件是32位还是64位的示例代码 2023-05-11
- 详解C# 不能用于文件名的字符 2022-12-06
- 深入理解C#指针之美 2023-04-22
- C#中间语言及ILDASM工具用法 2023-05-16
- c# 字符串操作总结 2023-02-25
- C#条件拼接Expression<Func<T, bool>>的使用 2023-05-22
- C#设置Word文本框中改变文字方向的方法 2023-04-21