.Net Core3.1 MVC + EF Core+ AutoFac+LayUI+Sqlserver的框架搭建--------Redis
实际项目中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 ////// 保存单个key value /// /// 保存的值 /// 过期时间 public bool SetStringKey(string key, string value, TimeSpan? expiry = default(TimeSpan?)) { return db.StringSet(key, value, expiry); } /// /// 获取单个key的值 /// public RedisValue GetStringKey(string key) { return db.StringGet(key); } /// /// 获取一个key的对象 /// public T GetStringKey (string key) { if (db == null) { return default; } var value = db.StringGet(key); if (value.IsNullOrEmpty) { return default; } return JsonConvert.DeserializeObject (value); } /// /// 保存一个对象 /// /// public bool SetStringKey (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 /// /// 将一个泛型List添加到缓存中 /// /// 泛型T /// Key /// list /// 数据库序号,不传默认为0 /// public bool addList (string listkey, List list, int db_index = 0) { if (db == null) { return false; } var value = JsonConvert.SerializeObject(list); return db.StringSet(listkey, value); } /// /// 通过指定Key值获取泛型List /// /// 泛型T /// Key /// 数据库序号,不传默认为0 /// public List getList (string listkey, int db_index = 0) { //var db = redis.GetDatabase(db_index); if (db == null) { return new List (); } if (db.KeyExists(listkey)) { var value = db.StringGet(listkey); if (!string.IsNullOrEmpty(value)) { var list = JsonConvert.DeserializeObject >(value); return list; } else { return new List
(); } } else { return new List (); } } public bool getKeyExists(string listkey, int db_index = 0) { if (db == null) { return false; } if (db.KeyExists(listkey)) { return true; } else { return false; } } /// /// 删除指定List 中满足条件的元素 /// /// Key /// lamdba表达式 /// 数据库序号,不传默认为0 /// public bool delListByLambda (string listkey, Func 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 >(value); if (list.Count > 0) { list = list.SkipWhile
(func).ToList(); value = JsonConvert.SerializeObject(list); return db.StringSet(listkey, value); } else { return false; } } else { return false; } } else { return false; } } /// /// 获取指定List 中满足条件的元素 /// /// Key /// lamdba表达式 /// 数据库序号,不传默认为0 /// public List getListByLambda (string listkey, Func bool> func, int db_index = 0) { if (db == null) { return new List (); } if (db.KeyExists(listkey)) { var value = db.StringGet(listkey); if (!string.IsNullOrEmpty(value)) { var list = JsonConvert.DeserializeObject >(value); if (list.Count > 0) { list = list.Where(func).ToList(); return list; } else { return new List
(); } } else { return new List (); } } else { return new List (); } } } }
然后在startup.cs中的ConfigureServices方法中注册就行了
RedisClient.InitConnect(Configuration); //初始化redis