.net core 3.1 使用Redis缓存
1、下载安装Redis
下载地址:https://github.com/tporadowski/redis/releases
例如我的系统是win10 64位,则下载:
安装教程,可以参考:https://www.runoob.com/redis/redis-install.html
这里简单说下:
运行cmd ,然后切换至redis所在目录,执行
redis-server.exe redis.windows.conf
执行成功后效果:
然后再打开另外一个cmd,切换至redis所在目录,执行:
redis-cli.exe -h 127.0.0.1 -p 6379
然后在此cmd窗口进行读写测试:
插入值:set redis helloword 获取值:get redis
效果如下:
表示redis运行成功,可以正常进行数据读写操作。
2、可视化
可以使用“Redis Desktop Manager”来实现Redis数据的可视化,下载和使用大家自行百度,这里就不详述了。界面如下:
3、项目引入 StackExchange.Redis
这里有两个Redis帮助类,大家自行看下,摘抄自 https://www.cnblogs.com/LiChen19951127/p/10478153.html 亲测可用,这里就不赘述了,已经很全面了,使用上确实很方便,string list 等都可以存。
1 public class RedisCommon 2 { 3 //public static ILogger Log = UtilLogger<RedisCommon>.Log;//日志记录 4 //redis数据库连接字符串 5 private string _conn = "127.0.0.1:6379"; 6 private int _db = 0; 7 //静态变量 保证各模块使用的是不同实例的相同链接 8 private static ConnectionMultiplexer connection; 9 public RedisCommon() { } 10 11 /// <summary> 12 /// 构造函数 13 /// </summary> 14 /// <param name="db"></param> 15 /// <param name="connectStr"></param> 16 public RedisCommon(int db, string connectStr) 17 { 18 _conn = connectStr; 19 _db = db; 20 } 21 22 /// <summary> 23 /// 缓存数据库,数据库连接 24 /// </summary> 25 public ConnectionMultiplexer CacheConnection 26 { 27 get 28 { 29 try 30 { 31 if (connection == null || !connection.IsConnected) 32 { 33 connection = new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(_conn)).Value; 34 } 35 } 36 catch (Exception ex) 37 { 38 //Log.LogError("RedisHelper->CacheConnection 出错\r\n" + ex.ToString()); 39 return null; 40 } 41 return connection; 42 } 43 } 44 45 /// <summary> 46 /// 缓存数据库 47 /// </summary> 48 public IDatabase CacheRedis => CacheConnection.GetDatabase(_db); 49 50 #region --KEY/VALUE存取-- 51 52 /// <summary> 53 /// 单条存值 54 /// </summary> 55 /// <param name="key">key</param> 56 /// <param name="value">The value.</param> 57 /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> 58 public bool StringSet(string key, string value) 59 { 60 return CacheRedis.StringSet(key, value); 61 } 62 63 /// <summary> 64 /// 保存单个key value 65 /// </summary> 66 /// <param name="key">Redis Key</param> 67 /// <param name="value">保存的值</param> 68 /// <param name="expiry">过期时间</param> 69 /// <returns></returns> 70 public bool StringSet(string key, string value, TimeSpan? expiry = default(TimeSpan?)) 71 { 72 return CacheRedis.StringSet(key, value, expiry); 73 } 74 75 /// <summary> 76 /// 保存多个key value 77 /// </summary> 78 /// <param name="arr">key</param> 79 /// <returns></returns> 80 public bool StringSet(KeyValuePair<RedisKey, RedisValue>[] arr) 81 { 82 return CacheRedis.StringSet(arr); 83 } 84 85 /// <summary> 86 /// 批量存值 87 /// </summary> 88 /// <param name="keysStr">key</param> 89 /// <param name="valuesStr">The value.</param> 90 /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> 91 public bool StringSetMany(string[] keysStr, string[] valuesStr) 92 { 93 var count = keysStr.Length; 94 var keyValuePair = new KeyValuePair<RedisKey, RedisValue>[count]; 95 for (int i = 0; i < count; i++) 96 { 97 keyValuePair[i] = new KeyValuePair<RedisKey, RedisValue>(keysStr[i], valuesStr[i]); 98 } 99 100 return CacheRedis.StringSet(keyValuePair); 101 } 102 103 /// <summary> 104 /// 保存一个对象 105 /// </summary> 106 /// <typeparam name="T"></typeparam> 107 /// <param name="key"></param> 108 /// <param name="obj"></param> 109 /// <returns></returns> 110 public bool SetStringKey<T>(string key, T obj, TimeSpan? expiry = default(TimeSpan?)) 111 { 112 string json = JsonConvert.SerializeObject(obj); 113 return CacheRedis.StringSet(key, json, expiry); 114 } 115 116 /// <summary> 117 /// 追加值 118 /// </summary> 119 /// <param name="key"></param> 120 /// <param name="value"></param> 121 public void StringAppend(string key, string value) 122 { 123 ////追加值,返回追加后长度 124 long appendlong = CacheRedis.StringAppend(key, value); 125 } 126 127 /// <summary> 128 /// 获取单个key的值 129 /// </summary> 130 /// <param name="key">Redis Key</param> 131 /// <returns></returns> 132 public RedisValue GetStringKey(string key) 133 { 134 return CacheRedis.StringGet(key); 135 } 136 137 /// <summary> 138 /// 根据Key获取值 139 /// </summary> 140 /// <param name="key">键值</param> 141 /// <returns>System.String.</returns> 142 public string StringGet(string key) 143 { 144 try 145 { 146 return CacheRedis.StringGet(key); 147 } 148 catch (Exception ex) 149 { 150 //Log.LogError("RedisHelper->StringGet 出错\r\n" + ex.ToString()); 151 return null; 152 } 153 } 154 155 /// <summary> 156 /// 获取多个Key 157 /// </summary> 158 /// <param name="listKey">Redis Key集合</param> 159 /// <returns></returns> 160 public RedisValue[] GetStringKey(List<RedisKey> listKey) 161 { 162 return CacheRedis.StringGet(listKey.ToArray()); 163 } 164 165 /// <summary> 166 /// 批量获取值 167 /// </summary> 168 public string[] StringGetMany(string[] keyStrs) 169 { 170 var count = keyStrs.Length; 171 var keys = new RedisKey[count]; 172 var addrs = new string[count]; 173 174 for (var i = 0; i < count; i++) 175 { 176 keys[i] = keyStrs[i]; 177 } 178 try 179 { 180 181 var values = CacheRedis.StringGet(keys); 182 for (var i = 0; i < values.Length; i++) 183 { 184 addrs[i] = values[i]; 185 } 186 return addrs; 187 } 188 catch (Exception ex) 189 { 190 //Log.LogError("RedisHelper->StringGetMany 出错\r\n" + ex.ToString()); 191 return null; 192 } 193 } 194 195 /// <summary> 196 /// 获取一个key的对象 197 /// </summary> 198 /// <typeparam name="T"></typeparam> 199 /// <param name="key"></param> 200 /// <returns></returns> 201 public T GetStringKey<T>(string key) 202 { 203 try 204 { 205 return JsonConvert.DeserializeObject<T>(CacheRedis.StringGet(key)); 206 } 207 catch (Exception ex) 208 { 209 //return new T(); 210 return default(T); 211 } 212 } 213 214 #endregion 215 216 #region --删除设置过期-- 217 218 /// <summary> 219 /// 删除单个key 220 /// </summary> 221 /// <param name="key">redis key</param> 222 /// <returns>是否删除成功</returns> 223 public bool KeyDelete(string key) 224 { 225 return CacheRedis.KeyDelete(key); 226 } 227 228 /// <summary> 229 /// 删除多个key 230 /// </summary> 231 /// <param name="keys">rediskey</param> 232 /// <returns>成功删除的个数</returns> 233 public long KeyDelete(RedisKey[] keys) 234 { 235 return CacheRedis.KeyDelete(keys); 236 } 237 238 /// <summary> 239 /// 判断key是否存储 240 /// </summary> 241 /// <param name="key">redis key</param> 242 /// <returns></returns> 243 public bool KeyExists(string key) 244 { 245 return CacheRedis.KeyExists(key); 246 } 247 248 /// <summary> 249 /// 重新命名key 250 /// </summary> 251 /// <param name="key">就的redis key</param> 252 /// <param name="newKey">新的redis key</param> 253 /// <returns></returns> 254 public bool KeyRename(string key, string newKey) 255 { 256 return CacheRedis.KeyRename(key, newKey); 257 } 258 259 /// <summary> 260 /// 删除hasekey 261 /// </summary> 262 /// <param name="key"></param> 263 /// <param name="hashField"></param> 264 /// <returns></returns> 265 public bool HaseDelete(RedisKey key, RedisValue hashField) 266 { 267 return CacheRedis.HashDelete(key, hashField); 268 } 269 270 /// <summary> 271 /// 移除hash中的某值 272 /// </summary> 273 /// <typeparam name="T"></typeparam> 274 /// <param name="key"></param> 275 /// <param name="dataKey"></param> 276 /// <returns></returns> 277 public bool HashRemove(string key, string dataKey) 278 { 279 return CacheRedis.HashDelete(key, dataKey); 280 } 281 282 /// <summary> 283 /// 设置缓存过期 284 /// </summary> 285 /// <param name="key"></param> 286 /// <param name="datetime"></param> 287 public void SetExpire(string key, DateTime datetime) 288 { 289 CacheRedis.KeyExpire(key, datetime); 290 } 291 #endregion 292 }
1 public class RedisHelperNetCore 2 { 3 //单例模式 4 public static RedisCommon Default { get { return new RedisCommon(); } } 5 }
以下是我测试截图:
其中Paging是我自定义实体类,UserInfo是来自数据库的实体类,存取都没有问题。
PS:当前文章我是参考了很多网上的资料,然后进行学习和总结,最后和大家分享,如果有错误的地方还请批评指正。
感谢: