.Net Core3.0 WebApi 项目框架搭建 十七:使用NewLife.Redis替换掉原来的Redis
.Net Core3.0 WebApi 项目框架搭建:目录
介绍
- NewLife.Redis主要作者及经验介绍来源:大石头
- 源码: https://github.com/NewLifeX/NewLife.Redis
- Nuget:NewLife.Redis
- NewLife.Redis是一个Redis客户端组件,以高性能处理大数据实时计算为目标。
- Redis协议基础实现Redis/RedisClient位于X组件,包含基础字符串操作。完整实现由独立开源项目NewLife.Redis提供。
NewLife.Redis
为扩展实现,主要增加列表结构、哈希结构、队列等高级功能。 - 采取连接池加同步阻塞架构,具有超低延迟(200~600us)以及超高吞吐量的特点。在物流行业大数据实时计算中广泛应有,经过日均100亿次调用量验证。
特性
-
在ZTO大数据实时计算广泛应用,200多个Redis实例稳定工作一年多,每天处理近1亿包裹数据,日均调用量80亿次
-
低延迟,Get/Set操作平均耗时200~600us(含往返网络通信)
-
大吞吐,自带连接池,最大支持1000并发
更新Redis接口
public interface IRedisCacheManager { #region #endregion #region 普通 /// <summary> /// 获取值,并序列化 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="key"></param> /// <returns></returns> TEntity Get<TEntity>(string key); /// <summary> /// 插入数据到redis /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="cacheTime"></param> void Set(string key, object value, TimeSpan cacheTime); /// <summary> /// 插入数据到redis /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="cacheTime"></param> void Set<TEntity>(string key, TEntity value, TimeSpan cacheTime); /// <summary> /// 判断是否存在 /// </summary> /// <param name="key"></param> /// <returns></returns> bool ContainsKey(string key); /// <summary> /// 移除某一个key /// </summary> /// <param name="key"></param> void Remove(string key); /// <summary> /// 全部清除 /// </summary> void Clear(); /// <summary> /// 设置超时 /// </summary> /// <param name="key"></param> /// <param name="timeSpan"></param> void SetExpire(string key, TimeSpan timeSpan); /// <summary> /// 根据关键字移除 /// </summary> /// <param name="key"></param> /// <returns></returns> Task RemoveByKey(string key, int count); /// <summary> /// 根据关键字移除所有 /// </summary> /// <param name="key"></param> /// <param name="count"></param> /// <returns></returns> Task RemoveAllByKey(string key, int count = 999); #endregion #region HashMap /// <summary> /// 插入hashmap /// </summary> /// <param name="name"></param> /// <param name="key"></param> /// <param name="value"></param> /// <param name="expire"></param> /// <returns></returns> bool HashSet<T>(string key, Dictionary<string, T> dic, int expire = -1); /// <summary> /// /插入hashmap带返回结果 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="dic"></param> /// <param name="expire"></param> /// <returns></returns> bool HashSetWithResult<T>(string key, Dictionary<string, T> dic, int expire = -1); /// <summary> /// 获取hashmap /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="fields"></param> /// <returns></returns> List<T> HashGet<T>(string key, params string[] fields); /// <summary> /// 获取hashmap /// </summary> /// <param name="name"></param> /// <param name="key"></param> /// <param name="value"></param> /// <param name="expire"></param> /// <returns></returns> string[] HashGet(string key, params string[] fields); #endregion #region 队列 /// <summary> /// 添加到队列 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> int AddQueue<T>(string key, T[] value); /// <summary> /// 获取一批队列消息 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> List<T> GetQueue<T>(string key, int Count); /// <summary> /// 获取一个队列消息 /// </summary> /// <param name="key"></param> /// <returns></returns> Task<T> GetQueueOneAsync<T>(string key); #endregion #region 可靠队列 /// <summary> /// 获取可靠队列实例 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> RedisReliableQueue<T> GetReliableQueue<T>(string key); /* /// <summary> /// 批量获取可靠队列消息 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queue"></param> /// <param name="count"></param> /// <returns></returns> Task<List<T>> GetReliableQueueMsgAsync<T>(RedisReliableQueue<T> queue, int count); /// <summary> /// 获取一条可靠队列消息 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queue"></param> /// <returns></returns> Task<T> GetReliableQueueMsgAsync<T>(RedisReliableQueue<T> queue); /// <summary> /// 回滚所有未确认消息 /// </summary> /// <param name="key"></param> /// <param name="retryInterval">重新处理确认队列中死信的间隔。默认60s</param> /// <returns></returns> Task<int> RollbackAllAck(string key, int retryInterval = 60); /// <summary> /// 确认消息 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> Task<int> Acknowledge<T>(RedisReliableQueue<T> queue, T value); */ /// <summary> /// 批量添加消息到可靠队列 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> int AddReliableQueueList<T>(string key, List<T> value); /// <summary> /// 添加消息到可靠队列 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> int AddReliableQueue<T>(string key, T value); /// <summary> /// 回滚所有未确认消息 /// </summary> /// <param name="key"></param> /// <param name="retryInterval">重新处理确认队列中死信的间隔。默认60s</param> /// <returns></returns> int RollbackAllAck(string key, int retryInterval = 60); #endregion #region 列表 /// <summary> /// 插入列表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="values"></param> /// <param name="timeSpan"></param> /// <returns></returns> int AddList(string key, IEnumerable<string> values, TimeSpan timeSpan); /// <summary> /// 列表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="values"></param> /// <returns></returns> int AddList(string key, IEnumerable<string> values); #endregion #region SortSet /// <summary> /// 获取SortSet实例 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> RedisSortedSet<T> GetRedisSortedSet<T>(string key); /// <summary> /// 批量添加Sorted /// 将所有指定成员添加到键为key有序集合(sorted set)里面。 添加时可以指定多个分数/成员(score/member)对。 /// 如果指定添加的成员已经是有序集合里面的成员,则会更新改成员的分数(scrore)并更新到正确的排序位置。 /// ZADD 命令在key后面分数/成员(score/member)对前面支持一些参数,他们是: /// XX: 仅仅更新存在的成员,不添加新成员。 /// NX: 不更新存在的成员。只添加新成员。 /// CH: 修改返回值为发生变化的成员总数,原始是返回新添加成员的总数(CH 是 changed 的意思)。 /// 更改的元素是新添加的成员,已经存在的成员更新分数。 所以在命令中指定的成员有相同的分数将不被计算在内。 /// 注:在通常情况下,ZADD返回值只计算新添加成员的数量。 /// INCR: 当ZADD指定这个选项时,成员的操作就等同ZINCRBY命令,对成员的分数进行递增操作。 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="options"></param> /// <returns></returns> int SortedSetAdd(string key, Dictionary<String, Double> value, string options = null); /// <summary>为有序集key的成员member的score值加上增量increment</summary> /// <param name="member"></param> /// <param name="score"></param> /// <returns></returns> double SortedSetIncrement<T>(string key, T member, double score); /// <summary>删除并返回有序集合key中的最多count个具有最高得分的成员</summary> /// <param name="count"></param> /// <returns></returns> IDictionary<T, double> SortedSetPopMax<T>(string key, int count = 1); /// <summary>删除并返回有序集合key中的最多count个具有最低得分的成员</summary> /// <param name="count"></param> /// <returns></returns> IDictionary<T, double> SortedSetPopMin<T>(string key, int count = 1); /// <summary> /// 返回有序集key中,score值在min和max之间(默认包括score值等于min或max)的成员个数 /// </summary> /// <param name="min"></param> /// <param name="max"></param> /// <returns></returns> int SortedSetFindCount<T>(string key, double min, double max); /// <summary>返回指定范围的列表</summary> /// <param name="start"></param> /// <param name="stop"></param> /// <returns></returns> T[] SortedSetRange<T>(string key, int start, int stop); /// <summary>返回指定范围的成员分数对</summary> /// <param name="start"></param> /// <param name="stop"></param> /// <returns></returns> IDictionary<T, double> SortedSetRangeWithScores<T>(string key, int start, int stop); /// <summary>返回指定分数区间的成员列表,低分到高分排序</summary> /// <param name="min">低分,包含</param> /// <param name="max">高分,包含</param> /// <param name="offset">偏移</param> /// <param name="count">个数</param> /// <returns></returns> T[] SortedSetRangeByScore<T>(string key, double min, double max, int offset, int count); /// <summary>返回指定分数区间的成员列表,低分到高分排序</summary> /// <param name="min">低分,包含</param> /// <param name="max">高分,包含</param> /// <param name="offset">偏移</param> /// <param name="count">个数</param> /// <returns></returns> Task<T[]> SortedSetRangeByScoreAsync<T>(string key, double min, double max, int offset, int count); /// <summary>返回指定分数区间的成员分数对,低分到高分排序</summary> /// <param name="min">低分,包含</param> /// <param name="max">高分,包含</param> /// <param name="offset">偏移</param> /// <param name="count">个数</param> /// <returns></returns> IDictionary<T, double> SortedSetRangeByScoreWithScores<T>(string key, double min, double max, int offset, int count); /// <summary>返回有序集key中成员member的排名。其中有序集成员按score值递增(从小到大)顺序排列</summary> /// <param name="member"></param> /// <returns></returns> int SortedSetRank<T>(string key, T member); /// <summary>模糊搜索,支持?和*</summary> /// <param name="pattern"></param> /// <param name="count"></param> /// <param name="position"></param> /// <returns></returns> IEnumerable<KeyValuePair<T, double>> SortedSetSearch<T>(string key, string pattern, int count, int position = 0); #endregion #region Set /// <summary> /// 添加Set /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> void SetAdd<T>(string key, T value); /// <summary> /// 批量添加添加Set /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> void SetAddList<T>(string key, T[] value); /// <summary> /// 移除Set某个值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> bool SetRemove<T>(string key, T value); /// <summary> /// 判断值是否存在 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> bool SetContains<T>(string key, T value); /// <summary> /// 清空Set /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> void SetClear<T>(string key); /// <summary> /// 复制Set /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="array"></param> /// <param name="arrayIndex"></param> /// <returns></returns> void SetCopyTo<T>(string key, T[] array, int arrayIndex); #endregion }
添加实现类
Common类库中,新建Redis文件夹,新建NewLifeRedisCacheManager.cs
public class NewLifeRedisCacheManager : IRedisCacheManager { private readonly string redisConnenctionString; public volatile FullRedis redisConnection; private readonly object redisConnectionLock = new object(); private readonly int db; public NewLifeRedisCacheManager() { string redisConfiguration = Appsettings.App(new string[] { "AppSettings", "RedisCaching", "ConnectionString" });//获取连接字符串 string Db = Appsettings.App(new string[] { "AppSettings", "RedisCaching", "Db" }); if (string.IsNullOrWhiteSpace(redisConfiguration)) { throw new ArgumentException("redis config is empty", nameof(redisConfiguration)); } FullRedis.Register(); this.redisConnenctionString = redisConfiguration; this.db = Db.ToInt(); this.redisConnection = GetRedisConnection(); } public NewLifeRedisCacheManager(string redisConfiguration, int Db = 0) { if (string.IsNullOrWhiteSpace(redisConfiguration)) { throw new ArgumentException("redis config is empty", nameof(redisConfiguration)); } FullRedis.Register(); this.redisConnenctionString = redisConfiguration; this.db = Db.ToInt(); this.redisConnection = GetRedisConnection(); } /// <summary> /// 核心代码,获取连接实例 /// 通过双if 夹lock的方式,实现单例模式 /// </summary> /// <returns></returns> private FullRedis GetRedisConnection() { //如果已经连接实例,直接返回 if (this.redisConnection != null) { return this.redisConnection; } //加锁,防止异步编程中,出现单例无效的问题 lock (redisConnectionLock) { if (this.redisConnection != null) { //释放redis连接 this.redisConnection.Dispose(); } try { //this.redisConnection = FullRedis.Create(redisConnenctionString); this.redisConnection = FullRedis.Create(redisConnenctionString); this.redisConnection.Timeout = 5000; } catch (Exception ex) { throw new Exception("Redis服务未启用,请开启该服务"); } } return this.redisConnection; } #region 普通 public TEntity Get<TEntity>(string key) { return redisConnection.Get<TEntity>(key); } public void Set<TEntity>(string key, TEntity value, TimeSpan cacheTime) { if (value != null) { redisConnection.Set(key, value, cacheTime); } } public void Set(string key, object value, TimeSpan cacheTime) { redisConnection.Set(key, value, cacheTime); } public bool ContainsKey(string key) { return redisConnection.ContainsKey(key); } public void Remove(string key) { redisConnection.Remove(key); } public void Clear() { redisConnection.Clear(); } public void SetExpire(string key, TimeSpan timeSpan) { redisConnection.SetExpire(key, timeSpan); } public async Task RemoveByKey(string key, int count) { await Task.Run(() => { var keys = redisConnection.Search(key, count).ToList(); foreach (var k in keys) redisConnection.Remove(k); }); } public async Task RemoveAllByKey(string key, int count = 999) { await Task.Run(() => { while (true) { var keyList = redisConnection.Search(key, count).ToList(); if (keyList.Count > 0) { foreach (var k in keyList) redisConnection.Remove(k); } else { break; } } }); } #endregion #region HashMap public bool HashSetWithResult<T>(string key, Dictionary<string, T> dic, int expire = -1) { var hash = redisConnection.GetDictionary<T>(key) as RedisHash<string, T>; var result = hash.HMSet(dic); return result; } public bool HashSet<T>(string key, Dictionary<string, T> dic, int expire = -1) { var hash = redisConnection.GetDictionary<T>(key) as RedisHash<string, T>; var result = hash.HMSet(dic); return result; } public string[] HashGet(string key, params string[] fields) { var hash = redisConnection.GetDictionary<string>(key) as RedisHash<string, string>; var result = hash.HMGet(fields); return result; } public List<T> HashGet<T>(string key, params string[] fields) { var hash = redisConnection.GetDictionary<T>(key) as RedisHash<string, T>; var result = hash.HMGet(fields); return result.ToList(); } #endregion #region 队列 public int AddQueue<T>(string key, T[] value) { var queue = redisConnection.GetQueue<T>(key); return queue.Add(value); } public List<T> GetQueue<T>(string key, int Count = 1) { var queue = redisConnection.GetQueue<T>(key); var result = queue.Take(Count).ToList(); return result; } public async Task<T> GetQueueOneAsync<T>(string key) { var queue = redisConnection.GetQueue<T>(key); return await queue.TakeOneAsync(1); } #endregion #region 可靠队列 public RedisReliableQueue<T> GetReliableQueue<T>(string key) { var queue = redisConnection.GetReliableQueue<T>(key); return queue; } /* public async Task<List<T>> GetReliableQueueMsgAsync<T>(RedisReliableQueue<T> queue, int count) { return await Task.Run(() => { return queue.Take(count).ToList(); }); } public async Task<T> GetReliableQueueMsgAsync<T>(RedisReliableQueue<T> queue) { return await Task.Run(() => { return queue.TakeOne(); }); } public async Task<int> Acknowledge<T>(RedisReliableQueue<T> queue, T value) { return await Task.Run(() => { var data = value.ToJson(); var count = queue.Acknowledge(data); return count; }); } */ public int RollbackAllAck(string key, int retryInterval = 60) { var queue = redisConnection.GetReliableQueue<string>(key); queue.RetryInterval = retryInterval; return queue.RollbackAllAck(); } public int AddReliableQueueList<T>(string key, List<T> value) { var queue = redisConnection.GetReliableQueue<T>(key); return queue.Add(value.ToArray()); } public int AddReliableQueue<T>(string key, T value) { var queue = redisConnection.GetReliableQueue<T>(key); return queue.Add(value); } #endregion #region 列表 public int AddList(string key, IEnumerable<string> values, TimeSpan timeSpan) { var list = redisConnection.GetList<string>(key) as RedisList<string>; redisConnection.SetExpire(key, timeSpan); return list.AddRange(values); } public int AddList(string key, IEnumerable<string> values) { var list = redisConnection.GetList<string>(key) as RedisList<string>; return list.AddRange(values); } public async Task<string> GetListAsync(string key, string value) { return await Task.Run(() => { var list = redisConnection.GetList<string>(key) as RedisList<string>; return list[list.IndexOf(value)]; }); } #endregion #region SortSet public RedisSortedSet<T> GetRedisSortedSet<T>(string key) { var zset = new RedisSortedSet<T>(redisConnection, key); return zset; } public int SortedSetAdd(string key, Dictionary<string, double> value, string options = null) { var zset = new RedisSortedSet<string>(redisConnection, key); return zset.Add(options, value); } public IDictionary<T, double> SortedSetPopMax<T>(string key, int count = 1) { var zset = new RedisSortedSet<T>(redisConnection, key); return zset.PopMax(count); } public IDictionary<T, double> SortedSetPopMin<T>(string key, int count = 1) { var zset = new RedisSortedSet<T>(redisConnection, key); return zset.PopMin(count); } public int SortedSetFindCount<T>(string key, double min, double max) { var zset = new RedisSortedSet<T>(redisConnection, key); return zset.FindCount(min, max); } public T[] SortedSetRange<T>(string key, int start, int stop) { var zset = new RedisSortedSet<T>(redisConnection, key); return zset.Range(start, stop); } public IDictionary<T, double> SortedSetRangeWithScores<T>(string key, int start, int stop) { var zset = new RedisSortedSet<T>(redisConnection, key); return zset.RangeWithScores(start, stop); } public T[] SortedSetRangeByScore<T>(string key, double min, double max, int offset, int count) { var zset = new RedisSortedSet<T>(redisConnection, key); return zset.RangeByScore(min, max, offset, count); } public async Task<T[]> SortedSetRangeByScoreAsync<T>(string key, double min, double max, int offset, int count) { var zset = new RedisSortedSet<T>(redisConnection, key); return await zset.RangeByScoreAsync(min, max, offset, count); } public IDictionary<T, double> SortedSetRangeByScoreWithScores<T>(string key, double min, double max, int offset, int count) { var zset = new RedisSortedSet<T>(redisConnection, key); return zset.RangeByScoreWithScores(min, max, offset, count); } public int SortedSetRank<T>(string key, T member) { var zset = new RedisSortedSet<T>(redisConnection, key); return zset.Rank(member); } public IEnumerable<KeyValuePair<T, double>> SortedSetSearch<T>(string key, string pattern, int count, int position = 0) { var zset = new RedisSortedSet<T>(redisConnection, key); return zset.Search(pattern, count, position); } public double SortedSetIncrement<T>(string key, T member, double score) { var zset = new RedisSortedSet<T>(redisConnection, key); return zset.Increment(member, score); } #endregion #region Set public void SetAdd<T>(string key, T value) { var set = redisConnection.GetSet<T>(key); set.Add(value); } public void SetAddList<T>(string key, T[] value) { var set = redisConnection.GetSet<string>(key); var set2 = set as RedisSet<T>; set2.SAdd(value); } public bool SetRemove<T>(string key, T value) { var set = redisConnection.GetSet<T>(key); return set.Remove(value); } public bool SetContains<T>(string key, T value) { var set = redisConnection.GetSet<T>(key); return set.Contains(value); } public void SetClear<T>(string key) { var set = redisConnection.GetSet<T>(key); set.Clear(); } public void SetCopyTo<T>(string key, T[] array, int arrayIndex) { var set = redisConnection.GetSet<T>(key); set.CopyTo(array, arrayIndex); } #endregion }
启动文件注入
services.AddSingleton<IRedisCacheManager, NewLifeRedisCacheManager>();
本文来自博客园,作者:HuTiger,转载请注明原文链接:https://www.cnblogs.com/huguodong/p/13949444.html,欢迎加入qq群927460240学习讨论