.Net Core3.1 MVC + EF Core+ AutoFac+LayUI+Sqlserver的框架搭建--------Redis
阿新 • • 發佈:2021-11-13
實際專案中Redis快取用到的地方很多,這裡就簡單的封裝一下:
首先在appsettings.json中配置redis
"RedisCaching": { "Enabled": true, "RedisConnectionString": "127.0.0.1:6379" }
然後就是redis通用類:
using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using StackExchange.Redis; using System; using System.Collections.Generic;using System.Linq; using System.Text; namespace Core.Net.Common.Core.Net.Core.Redis { public class RedisClient { static ConnectionMultiplexer redis = null; static IDatabase db = null; public static void InitConnect(IConfiguration Configuration) {try { var RedisConnection = Configuration.GetConnectionString("RedisConnectionString"); redis = ConnectionMultiplexer.Connect(RedisConnection); db = redis.GetDatabase(); } catch (Exception ex) { Console.WriteLine(ex.Message); redis= null; db = null; } } public RedisClient() { } #region String /// <summary> /// 儲存單個key value /// </summary> /// <param name="value">儲存的值</param> /// <param name="expiry">過期時間</param> public bool SetStringKey(string key, string value, TimeSpan? expiry = default(TimeSpan?)) { return db.StringSet(key, value, expiry); } /// <summary> /// 獲取單個key的值 /// </summary> public RedisValue GetStringKey(string key) { return db.StringGet(key); } /// <summary> /// 獲取一個key的物件 /// </summary> public T GetStringKey<T>(string key) { if (db == null) { return default; } var value = db.StringGet(key); if (value.IsNullOrEmpty) { return default; } return JsonConvert.DeserializeObject<T>(value); } /// <summary> /// 儲存一個物件 /// </summary> /// <param name="obj"></param> public bool SetStringKey<T>(string key, T obj, TimeSpan? expiry = default(TimeSpan?)) { if (db == null) { return false; } string json = JsonConvert.SerializeObject(obj); return db.StringSet(key, json, expiry); } #endregion /// <summary> /// 將一個泛型List新增到快取中 /// </summary> /// <typeparam name="T">泛型T</typeparam> /// <param name="listkey">Key</param> /// <param name="list">list</param> /// <param name="db_index">資料庫序號,不傳預設為0</param> /// <returns></returns> public bool addList<T>(string listkey, List<T> list, int db_index = 0) { if (db == null) { return false; } var value = JsonConvert.SerializeObject(list); return db.StringSet(listkey, value); } /// <summary> /// 通過指定Key值獲取泛型List /// </summary> /// <typeparam name="T">泛型T</typeparam> /// <param name="listkey">Key</param> /// <param name="db_index">資料庫序號,不傳預設為0</param> /// <returns></returns> public List<T> getList<T>(string listkey, int db_index = 0) { //var db = redis.GetDatabase(db_index); if (db == null) { return new List<T>(); } if (db.KeyExists(listkey)) { var value = db.StringGet(listkey); if (!string.IsNullOrEmpty(value)) { var list = JsonConvert.DeserializeObject<List<T>>(value); return list; } else { return new List<T>(); } } else { return new List<T>(); } } public bool getKeyExists(string listkey, int db_index = 0) { if (db == null) { return false; } if (db.KeyExists(listkey)) { return true; } else { return false; } } /// <summary> /// 刪除指定List<T>中滿足條件的元素 /// </summary> /// <param name="listkey">Key</param> /// <param name="func">lamdba表示式</param> /// <param name="db_index">資料庫序號,不傳預設為0</param> /// <returns></returns> public bool delListByLambda<T>(string listkey, Func<T, bool> func, int db_index = 0) { if (db == null) { return false; } if (db.KeyExists(listkey)) { var value = db.StringGet(listkey); if (!string.IsNullOrEmpty(value)) { var list = JsonConvert.DeserializeObject<List<T>>(value); if (list.Count > 0) { list = list.SkipWhile<T>(func).ToList(); value = JsonConvert.SerializeObject(list); return db.StringSet(listkey, value); } else { return false; } } else { return false; } } else { return false; } } /// <summary> /// 獲取指定List<T>中滿足條件的元素 /// </summary> /// <param name="listkey">Key</param> /// <param name="func">lamdba表示式</param> /// <param name="db_index">資料庫序號,不傳預設為0</param> /// <returns></returns> public List<T> getListByLambda<T>(string listkey, Func<T, bool> func, int db_index = 0) { if (db == null) { return new List<T>(); } if (db.KeyExists(listkey)) { var value = db.StringGet(listkey); if (!string.IsNullOrEmpty(value)) { var list = JsonConvert.DeserializeObject<List<T>>(value); if (list.Count > 0) { list = list.Where(func).ToList(); return list; } else { return new List<T>(); } } else { return new List<T>(); } } else { return new List<T>(); } } } }
然後在startup.cs中的ConfigureServices方法中註冊就行了
RedisClient.InitConnect(Configuration); //初始化redis.Net Core