在NuGet直接搜索StackExchange.Redis,下载引用包;
帮助类
1 /*********************************** 2 * Coder:EminemJK 3 * Date:2018-11-21 4 * 5 * Last Update:2018-12-24 6 **********************************/ 7 8 using System; 9 using System.Collections.Generic; 10 using System.Linq; 11 using StackExchange.Redis; 12 using Newtonsoft.Json; 13 using System.Threading.Tasks; 14 15 namespace Banana.Utility.Redis 16 { 17 public class RedisUtils 18 { 19 /// <summary> 20 /// redis配置文件信息 21 /// </summary> 22 public static string RedisPath = "172.16.3.82:6379"; 23 24 private static object _locker = new Object(); 25 private static ConnectionMultiplexer _instance = null; 26 27 /// <summary> 28 /// 使用一个静态属性来返回已连接的实例,如下列中所示。这样,一旦 ConnectionMultiplexer 断开连接,便可以初始化新的连接实例。 29 /// </summary> 30 public static ConnectionMultiplexer Instance 31 { 32 get 33 { 34 //if (_instance == null) 35 //{ 36 // lock (_locker) 37 // { 38 if (_instance == null || !_instance.IsConnected) 39 { 40 _instance = ConnectionMultiplexer.Connect(RedisPath); 41 //注册如下事件 42 _instance.ConnectionFailed += MuxerConnectionFailed; 43 _instance.ConnectionRestored += MuxerConnectionRestored; 44 _instance.ErrorMessage += MuxerErrorMessage; 45 _instance.HashSlotMoved += MuxerHashSlotMoved; 46 _instance.InternalError += MuxerInternalError; 47 } 48 // } 49 //} 50 return _instance; 51 } 52 } 53 54 #region Keys 55 /// <summary> 56 /// 判断键是否存在 57 /// </summary> 58 /// <param name="dbIndex">数据库</param> 59 /// <param name="key">键值</param> 60 /// <returns></returns> 61 public static bool KeyExists(int dbIndex, string key) 62 { 63 var db = Instance.GetDatabase(dbIndex); 64 return db.KeyExists(key); 65 } 66 67 /// <summary> 68 /// 为指定的键设置失效时间 69 /// </summary> 70 /// <param name="dbIndex">数据库</param> 71 /// <param name="key">键</param> 72 /// <param name="expiry">时间间隔</param> 73 /// <returns></returns> 74 public static bool SetExpire(int dbIndex, string key, TimeSpan? expiry) 75 { 76 var db = Instance.GetDatabase(dbIndex); 77 return db.KeyExpire(key, expiry); 78 } 79 80 /// <summary> 81 /// 为指定的键设置失效时间 82 /// </summary> 83 /// <param name="dbIndex">数据库</param> 84 /// <param name="key">键</param> 85 /// <param name="timeout">时间间隔(秒)</param> 86 /// <returns></returns> 87 public static bool SetExpire(int dbIndex, string key, int timeout = 0) 88 { 89 var db = Instance.GetDatabase(dbIndex); 90 return db.KeyExpire(key, DateTime.Now.AddSeconds(timeout)); 91 } 92 93 /// <summary> 94 /// 删除键 95 /// </summary> 96 /// <param name="dbIndex">数据库</param> 97 /// <param name="key">键</param> 98 /// <returns></returns> 99 public static bool KeyDelete(int dbIndex, string key) 100 { 101 var db = Instance.GetDatabase(dbIndex); 102 return db.KeyDelete(key); 103 } 104 105 /// <summary> 106 /// 键重命名 107 /// </summary> 108 /// <param name="dbIndex">数据库</param> 109 /// <param name="oldKey">旧值</param> 110 /// <param name="newKey">新值</param> 111 /// <returns></returns> 112 public static bool KeyRename(int dbIndex, string oldKey, string newKey) 113 { 114 var db = Instance.GetDatabase(dbIndex); 115 return db.KeyRename(oldKey, newKey); 116 } 117 #endregion 118 119 #region Strings 120 /// <summary> 121 /// 获取字符串数据 122 /// </summary> 123 /// <typeparam name="T"></typeparam> 124 /// <param name="dbIndex">数据库</param> 125 /// <param name="key">Redis键</param> 126 /// <returns></returns> 127 public static string StringGet(int dbIndex, string key) 128 { 129 var db = Instance.GetDatabase(dbIndex); 130 if (db != null) 131 { 132 return db.StringGet(key); 133 } 134 return string.Empty; 135 } 136 137 /// <summary> 138 /// 获取对象类型数据 139 /// </summary> 140 /// <typeparam name="T"></typeparam> 141 /// <param name="dbIndex">数据库</param> 142 /// <param name="key">Redis键</param> 143 /// <returns></returns> 144 public static T StringGet<T>(int dbIndex, string key) where T : class 145 { 146 T data = default(T); 147 var db = Instance.GetDatabase(dbIndex); 148 if (db != null) 149 { 150 var value = db.StringGet(key); 151 if (string.IsNullOrWhiteSpace(value)) 152 return data; 153 return JsonConvert.DeserializeObject<T>(value); 154 } 155 return data; 156 } 157 158 /// <summary> 159 /// 设置值类型的值 160 /// </summary> 161 /// <param name="dbIndex">数据库</param> 162 /// <param name="key">键</param> 163 /// <param name="value">值</param> 164 public static bool StringSet(int dbIndex, string key, RedisValue value, TimeSpan? expiry) 165 { 166 var db = Instance.GetDatabase(dbIndex); 167 return db.StringSet(key, value, expiry); 168 } 169 170 /// <summary> 171 /// 设置对象类型的值 172 /// </summary> 173 /// <param name="dbIndex">数据库</param> 174 /// <param name="key">键</param> 175 /// <param name="value">值</param> 176 public static bool StringSet<T>(int dbIndex, string key, T value, TimeSpan? expiry) where T : class 177 { 178 if (value == default(T)) 179 { 180 return false; 181 } 182 var db = Instance.GetDatabase(dbIndex); 183 return db.StringSet(key, JsonConvert.SerializeObject(value), expiry); 184 } 185 #endregion 186 187 #region Hashes 188 /// <summary> 189 /// Hash是否存在 190 /// </summary> 191 /// <param name="dbIndex">数据库</param> 192 /// <param name="hashId">HashId</param> 193 /// <param name="key">键</param> 194 /// <returns></returns> 195 public static bool HashExists(int dbIndex, string hashId, string key) 196 { 197 var db = Instance.GetDatabase(dbIndex); 198 return db.HashExists(hashId, key); 199 } 200 201 /// <summary> 202 /// Hash长度 203 /// </summary> 204 /// <param name="dbIndex">数据库</param> 205 /// <param name="hashId">HashId</param> 206 /// <returns></returns> 207 public static long HashLength(int dbIndex, string hashId) 208 { 209 var db = Instance.GetDatabase(dbIndex); 210 var length = db.HashLength(hashId); 211 return length; 212 } 213 214 /// <summary> 215 /// 设置哈希值 216 /// </summary> 217 /// <typeparam name="T">哈希值类型</typeparam> 218 /// <param name="dbIndex">数据库</param> 219 /// <param name="hashId">哈希ID</param> 220 /// <param name="key">键</param> 221 /// <param name="t">哈希值</param> 222 /// <returns></returns> 223 public static bool HashSet<T>(int dbIndex, string hashId, string key, T t) 224 { 225 var db = Instance.GetDatabase(dbIndex); 226 var value = JsonConvert.SerializeObject(t); 227 return db.HashSet(hashId, key, value); 228 } 229 230 /// <summary> 231 /// 获取哈希值 232 /// </summary> 233 /// <typeparam name="T">哈希值类型</typeparam> 234 /// <param name="dbIndex">数据库</param> 235 /// <param name="hashId">哈希ID</param> 236
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式