0.引入.net core环境下Redis的NuGet包,StackExchange.Redis,现目前最新的2.0.519。
- 帮助类Code:
1 using System; 2 using System.Collections.Generic; 3 using StackExchange.Redis; 4 using Newtonsoft.Json; 5 using YJT.Web.lib; 6 using YJT.Common.Log; 7 8 namespace YJT.Web.Redis 9 { 10 ///
11 /// Redis帮助类 12 /// 13 public class RedisHelper 14 { 15 //单例模式 16 public static RedisCommon Default { get { return new RedisCommon(); } } 17 public static RedisCommon One { get { return new RedisCommon(1, UtilConf.Configuration["RedisConfig:ReadWriteHosts"] ?? "127.0.0.1:6789"); } } 18 public static RedisCommon Two { get { return new RedisCommon(2, UtilConf.Configuration["RedisConfig:ReadWriteHosts"] ?? "127.0.0.1:6789"); } } 19 public static RedisCommon Three { get { return new RedisCommon(3, UtilConf.Configuration["RedisConfig:ReadWriteHosts"] ?? "127.0.0.1:6789"); } } 20 } 21 22 /// 23 /// Redis操作类 24 /// 老版用的是ServiceStack.Redis 25 /// .Net Core使用StackExchange.Redis的nuget包 26 /// 27 public class RedisCommon 28 { 29 //redis数据库连接字符串 30 private string _conn = UtilConf.Configuration["RedisConfig:ReadWriteHosts"] ?? "127.0.0.1:6789"; 31 private int _db = 0; 32 33 //静态变量 保证各模块使用的是不同实例的相同链接 34 private static ConnectionMultiplexer connection; 35 36 /// 37 /// 构造函数 38 /// 39 public RedisCommon() { } 40 /// 41 /// 构造函数 42 /// 43 /// 44 /// 45 public RedisCommon(int db, string connectStr) 46 { 47 _db = db; 48 _conn = connectStr; 49 } 50 51 /// 52 /// 缓存数据库,数据库连接 53 /// 54 public ConnectionMultiplexer CacheConnection 55 { 56 get 57 { 58 try 59 { 60 if (connection == null || !connection.IsConnected) 61 { 62 connection = new Lazy (() => ConnectionMultiplexer.Connect(_conn)).Value; 63 } 64 } 65 catch (Exception ex) 66 { 67 Log.Debug("RedisHelper->CacheConnection 出错\r\n", ex.Message.ToString()); 68 return null; 69 } 70 return connection; 71 } 72 } 73 74 /// 75 /// 缓存数据库 76 /// 77 public IDatabase CacheRedis => CacheConnection.GetDatabase(_db); 78 79 80 #region --KEY/VALUE存取-- 81 /// 82 /// 单条存值 83 /// 84 /// key 85 /// The value. 86 /// true if XXXX,false otherwise. 87 public bool StringSet(string key, string value) 88 { 89 return CacheRedis.StringSet(key, value); 90 } 91 92 /// 93 /// 保存单个key value 94 /// 95 /// Redis Key 96 /// 保存的值 97 /// 过期时间 98 /// 99 public bool StringSet(string key, string value, TimeSpan? expiry = default(TimeSpan?)) 100 { 101 return CacheRedis.StringSet(key, value, expiry); 102 } 103 104 /// 105 /// 保存多个key value 106 /// 107 /// key 108 /// 109 public bool StringSet(KeyValuePair [] arr) 110 { 111 return CacheRedis.StringSet(arr); 112 } 113 114 /// 115 /// 批量存值 116 /// 117 /// key 118 /// The value. 119 /// true if XXXX,false otherwise. 120 public bool StringSetMany(string[] keysStr, string[] valuesStr) 121 { 122 var count = keysStr.Length; 123 var keyValuePair = new KeyValuePair [count]; 124 for (int i = 0; i < count; i++) 125 { 126 keyValuePair[i] = new KeyValuePair (keysStr[i], valuesStr[i]); 127 } 128 129 return CacheRedis.StringSet(keyValuePair); 130 } 131 132 /// 133 /// 保存一个对象 134 /// 135 /// 136 /// 137 /// 138 /// 139 public bool SetStringKey (string key, T obj, TimeSpan? expiry = default(TimeSpan?)) 140 { 141 string json = JsonConvert.SerializeObject(obj); 142 return CacheRedis.StringSet(key, json, expiry); 143 } 144 145 /// 146 /// 追加值 147 /// 148 /// 149 /// 150 public void StringAppend(string key, string value) 151 { 152 ////追加值,返回追加后长度 153 long appendlong = CacheRedis.StringAppend(key, value); 154 } 155 156 /// 157 /// 获取单个key的值 158 /// 159 /// Redis Key 160 /// 161 public RedisValue GetStringKey(string key) 162 { 163 return CacheRedis.StringGet(key); 164 } 165 166 /// 167 /// 根据Key获取值 168 /// 169 /// 键值 170 /// System.String. 171 public string StringGet(string key) 172 { 173 try 174 { 175 return CacheRedis.StringGet(key); 176 } 177 catch (Exception ex) 178 { 179 Log.Debug("RedisHelper->StringGet 出错\r\n", ex.Message.ToString()); 180 return null; 181 } 182 } 183 184 /// 185 /// 获取多个Key 186 /// 187 /// Redis Key集合 188 /// 189 public RedisValue[] GetStringKey(List listKey) 190 { 191 return CacheRedis.StringGet(listKey.ToArray()); 192 } 193 194 /// 195 /// 批量获取值 196 /// 197 public string[] StringGetMany(string[] keyStrs) 198 { 199 var count = keyStrs.Length; 200 var keys = new RedisKey[count]; 201 var addrs = new string[count]; 202 203 for (var i = 0; i < count; i++) 204 { 205 keys[i] = keyStrs[i]; 206 } 207 try 208 { 209 210 var values = CacheRedis.StringGet(keys); 211 for (var i = 0; i < values.Length; i++) 212 { 213 addrs[i] = values[i]; 214 } 215 return addrs; 216 } 217 catch (Exception ex) 218 { 219 Log.Debug("RedisHelper->StringGetMany 出错\r\n", ex.Message.ToString()); 220 return null; 221 } 222 } 223 224 /// 225 /// 获取一个key的对象 226 /// 227 /// 228 /// 229 /// 230 public T GetStringKey (string key) 231 { 232 return JsonConvert.DeserializeObject (CacheRedis.StringGet(key)); 233 } 234 #endregion 235 236 237 #region --删除设置过期-- 238 /// 239 /// 删除单个key 240 /// 241 /// redis key 242 /// 是否删除成功 243 public bool KeyDelete(string key) 244 { 245 return CacheRedis.KeyDelete(key); 246 } 247 248 /// 249 /// 删除多个key 250 /// 251 /// rediskey 252 /// 成功删除的个数 253 public long KeyDelete(RedisKey[] keys) 254 { 255 return CacheRedis.KeyDelete(keys); 256 } 257 258 /// 259 /// 判断key是否存储 260 /// 261 /// redis key 262 /// 263 public bool KeyExists(string key) 264 { 265 return CacheRedis.KeyExists(key); 266 } 267 268 /// 269 /// 重新命名key 270 /// 271 /// 就的redis key 272 /// 新的redis key 273 /// 274 public bool KeyRename(string key, string newKey) 275 { 276 return CacheRedis.KeyRename(key, newKey); 277 } 278 279 /// 280 /// 删除hasekey 281 /// 282 /// 283 /// 284 /// 285 public bool HaseDelete(RedisKey key, RedisValue hashField) 286 { 287 return CacheRedis.HashDelete(key, hashField); 288 } 289 290 /// 291 /// 移除hash中的某值 292 /// 293 /// 294 /// 295 /// 296 /// 297 public bool HashRemove(string key, string dataKey) 298 { 299 return CacheRedis.HashDelete(key, dataKey); 300 } 301 302 /// 303 /// 设置缓存过期 304 /// 305 /// 306 /// 307 public void SetExpire(string key, DateTime datetime) 308 { 309 CacheRedis.KeyExpire(key, datetime); 310 } 311 #endregion 312 } 313 } - using引用备注:
using Newtonsoft.Json;//为第三方转json 对象使用的,再熟悉不过了吧
using YJT.Common.Log;//是一个记录日志的帮助的类,你可以用你自己的来记录日志。
using YJT.Web.lib;//此引用是获取.net core中的appsettings.json中配置的信息。 UtilConf.Configuration["RedisConfig:ReadWriteHosts"]获取 - 获取appsettings.json的UtilConf.cs帮助类:
1 using Microsoft.Extensions.Configuration; 2 using System; 3 using System.Collections.Generic; 4 using System.IO; 5 using System.Text; 6 7 namespace YJT.Web.lib 8 { 9 ///
10 /// 读配置文件 11 /// 12 public class UtilConf 13 { 14 private static IConfiguration config; 15 16 /// 17 /// 加载配置文件 18 /// 19 public static IConfiguration Configuration 20 { 21 get 22 { 23 if (config != null) return config; 24 config = new ConfigurationBuilder() 25 .SetBasePath(Directory.GetCurrentDirectory()) 26 .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) 27 .Build(); 28 return config; 29 } 30 set => config = value; 31 } 32 } 33 } 此类主要是获取.net core 中的json配置信息。如:UtilConf.Configuration["RedisConfig:ReadWriteHosts"]
- StackExchange.Redis下的IDatabase接口还有丰富的操作方法,可自行研究补充帮助类
分享至此,欢迎留言评论~~~