RedisRepository分享和纠错
2018-06-22 07:44:32来源:未知 阅读 ()
自用RedisRepository分享和纠错,本文版权归博客园和作者吴双本人共同所有,转载和爬虫必须注明原文地址:www.cnblogs.com/tdws 。
一. 写在前面
毕业工作后,今天终于能回家了,回想了一些这半年来所做的内容,总是觉得还停留在那么基础的水平 ,在解决各种问题的过程中,自己的创新思路比较少,靠搜索来的比较多 。不想做16年的总结了 ,希望17年能学更多的我爱的技术,看更多的开源代码,能学到更多的设计思想和代码思路,能再更新这两年来对代码的理解。
这篇分享,主要是弥补我之前RedisRepository的不足。如果对Redis基础有疑问,可移步http://www.cnblogs.com/tdws/tag/NoSql/ .
半年前由于我StackExchange.Redis文档阅读不足,所分享的RedisRepository有所错误。下面列举我的主要错误:
错误1,没有单例化ConnectionMultiplexer Redis连接对象,并且我天真的以为给单例对象加锁,在并发情况下,会限制了Redis的性能。
错误2,在主从情况下,我以为在发生手动切换的时候,我们要订阅切换事件,并在事件发生后,动态改变连接对象指向的Endpoint。
当我再一次仔细阅读文档时,才明白我的错误,这是一篇迟到的修正,但是我自用的repository自我感觉还是有很多不足之处,所以我真的需要老司机的指点和建议。
修正1,Redis连接对象创建的代价很大,并且单例加锁并不会影响Redis性能,因为在发生网络请求的期间,连接对象并没有在等待中。
修正2,Redis主从时,在哨兵切换主从关系后,StackExchange.Redis会为我们识别新的主从,不需要我们做任何操作。
目前为止我还有两个疑问。
疑问1,在看文档后没有明确结果。当做主从读写分离时, 我们在Endpoint Collection集合中添加多个节点就会自动读写分离?还是说需要 我们在读取命令的方法中指定CommandFlags.PreferSlave? 我认为是后者吧?所以我在我所有的读取方法都指定了PreferSlave。 老司机们怎么说?
疑问2,我使用LuaScript.Prepare(lua)后再Load出来,执行lua总是无效果,并且LuaScript.GetCachedScriptCount()为0. 不过我直接使用ScriptEvaluateAsync却是好用的,老司机如果有好的例子,希望老司机给些指导或者分享。
二. 概念上有必要加强一下
StackExchange.Redis客户端所提供的功能,绝不是使用Socket连接下,用socket对象send点数据,recv点数据这么简单。也不是说创建包含几个redis socket连接的连接池那么容易,StackExchange的目标在于管道处理和多路复用。关于连接数量的多少,可能是一个也可能是一个连接池,在于我们如何使用,可以参考老外这篇文章https://frankdecaire.blogspot.jp/2016/03/stack-exchange-redis-connection-pooling.html ,相反的如果没有管道和多路复用的思想,由于socket面向有连接的协议时,send和recv都是阻塞的,他们在System.Net.Socket实现的内部调用了win32 API WSARecv和WSASend。连续的200个send recv对,最后一个命令就要在前面199个send&recv执行结束后发送到redis。所以有了StackExchange.Redis 我们不必把所有的时间都浪费在客户端到服务端以及服务端到客户端的等待上。当不同调用方同一时刻访问时,StackExchange客户端会自动使用管道分离访问请求,因此无论使用阻塞还是异步的访问方式,这些工作都是被管道处理的。从本质上讲,它填补了 waiting 时间与其他调用方的工作。
三. 代码结构,仅供参考
结构大概就是这样,RedisAsyncHelper下的所有类都是部分类,他们的类名称是RedisHelper。他们共同实现了IRedisHelper的接口,并且留下了详细的注释。
同步版本和异步版本的目录结构是一样的。所以我仅分享下Async版本,如果对Async有疑问可移步 http://www.cnblogs.com/tdws/p/6172207.html。
四. 预备阶段
CommonHelper中的两个帮助类:
RedisInnerTypeHelper.cs
1 using StackExchange.Redis; 2 using System.Collections.Generic; 3 using System.Linq; 4 5 namespace Fantasy.RedisRepository.CommonHelper 6 { 7 internal class RedisInnerTypeHelper 8 { 9 public static List<T> RedisValuesToGenericList<T>(RedisValue[] redisValues) 10 { 11 var result = new List<T>(); 12 redisValues.ToList().ForEach(r => result.Add(SerializeHelper.Deserialize<T>(r))); 13 return result; 14 } 15 16 public static RedisValue[] GenericListToRedisValues<T>(List<T> values) 17 { 18 var redisValues = new List<RedisValue>(); 19 values.ForEach(v => redisValues.Add(SerializeHelper.Serialize(values))); 20 return redisValues.ToArray(); 21 } 22 23 public static RedisKey[] GenericListToRedisKeys(List<string> keys) 24 { 25 var redisKeys = new List<RedisKey>(); 26 keys.ForEach(k => redisKeys.Add(k)); 27 return redisKeys.ToArray(); 28 } 29 } 30 }
SerializeHelper.cs
1 using System.IO; 2 using System.Runtime.Serialization.Formatters.Binary; 3 4 namespace Fantasy.RedisRepository.CommonHelper 5 { 6 internal static class SerializeHelper 7 { 8 /// <summary> 9 /// 字节数组序列化 10 /// </summary> 11 /// <param name="o"></param> 12 /// <returns></returns> 13 internal static byte[] Serialize(object o) 14 { 15 if (o == null) 16 { 17 return null; 18 } 19 20 BinaryFormatter binaryFormatter = new BinaryFormatter(); 21 using (MemoryStream memoryStream = new MemoryStream()) 22 { 23 binaryFormatter.Serialize(memoryStream, o); 24 byte[] objectDataAsStream = memoryStream.ToArray(); 25 return objectDataAsStream; 26 } 27 } 28 29 /// <summary> 30 /// 字节数组反序列化 31 /// </summary> 32 /// <typeparam name="T"></typeparam> 33 /// <param name="stream"></param> 34 /// <returns></returns> 35 internal static T Deserialize<T>(byte[] stream) 36 { 37 if (stream == null) 38 { 39 return default(T); 40 } 41 42 BinaryFormatter binaryFormatter = new BinaryFormatter(); 43 using (MemoryStream memoryStream = new MemoryStream(stream)) 44 { 45 T result = (T)binaryFormatter.Deserialize(memoryStream); 46 return result; 47 } 48 } 49 } 50 }
Config中的配置类:
ConfigHelper.cs
1 using System; 2 using System.Configuration; 3 4 namespace Fantasy.RedisRepository.Config 5 { 6 internal class ConfigHelper 7 { 8 internal static T Get<T>(string appSettingsKey, T defaultValue) 9 { 10 string text = ConfigurationManager.AppSettings[appSettingsKey]; 11 if (string.IsNullOrWhiteSpace(text)) 12 return defaultValue; 13 try 14 { 15 var value = Convert.ChangeType(text, typeof(T)); 16 return (T)value; 17 } 18 catch 19 { 20 return defaultValue; 21 } 22 } 23 } 24 }
RedisClientConfig.cs
1 namespace Fantasy.RedisRepository.Config 2 { 3 internal class RedisClientConfig 4 { 5 6 private static string _server = ConfigHelper.Get("RedisServer", "115.xx.xx.31"); 7 /// <summary> 8 /// 节点IP 9 /// </summary> 10 public static string Server 11 { 12 get { return _server; } 13 set { _server = value; } 14 } 15 16 private static int _port = ConfigHelper.Get("RedisPort", 6380); 17 /// <summary> 18 /// 节点端口 19 /// </summary> 20 public static int Port 21 { 22 get { return _port; } 23 set { _port = value; } 24 } 25 26 private static string _slaveServer = ConfigHelper.Get("SlaveServer", "115.xx.xx.31"); 27 /// <summary> 28 /// 节点IP 29 /// </summary> 30 public static string SlaveServer 31 { 32 get { return _slaveServer; } 33 set { _slaveServer = value; } 34 } 35 36 private static int _slavePort = ConfigHelper.Get("SlavePort", 6381); 37 /// <summary> 38 /// 节点端口 39 /// </summary> 40 public static int SlavePort 41 { 42 get { return _slavePort; } 43 set { _slavePort = value; } 44 } 45 46 private static string _auth = ConfigHelper.Get("RedisAuth", "fantasy.."); 47 /// <summary> 48 /// 节点密码 49 /// </summary> 50 public static string RedisAuth 51 { 52 get { return _auth; } 53 set { _auth = value; } 54 } 55 56 private static int _defaultDatabase = ConfigHelper.Get("RedisDataBase", 0); 57 /// <summary> 58 /// redis默认0号库 59 /// </summary> 60 public static int DefaultDatabase 61 { 62 get { return _defaultDatabase; } 63 set { _defaultDatabase = value; } 64 } 65 66 private static int _connectTimeout = 10000; 67 public static int ConnectTimeout 68 { 69 get { return _connectTimeout; } 70 set { _connectTimeout = value; } 71 } 72 73 private static int _connectRetry = 3; 74 public static int ConnectRetry 75 { 76 get { return _connectRetry; } 77 set { _connectRetry = value; } 78 } 79 80 private static bool _preserveAsyncOrder = false; 81 public static bool PreserveAsyncOrder 82 { 83 get { return _preserveAsyncOrder; } 84 set { _preserveAsyncOrder = value; } 85 } 86 } 87 }
RedisConnection.cs
1 using Fantasy.RedisRepository.Config; 2 using StackExchange.Redis; 3 4 namespace Fantasy.RedisRepository 5 { 6 /// <summary> 7 /// Redis连接类 8 /// </summary> 9 public static class RedisConnection 10 { 11 private static ConnectionMultiplexer _connection; 12 private static readonly object SyncObject = new object(); 13 /// <summary> 14 /// redis连接对象,单例加锁不影响性能 15 /// </summary> 16 public static ConnectionMultiplexer GenerateConnection 17 { 18 get 19 { 20 if (_connection == null || !_connection.IsConnected) 21 { 22 lock (SyncObject) 23 { 24 var configurationOptions = new ConfigurationOptions() 25 { 26 Password = RedisClientConfig.RedisAuth, 27 EndPoints = 28 { 29 {RedisClientConfig.Server, RedisClientConfig.Port}, 30 {RedisClientConfig.SlaveServer, RedisClientConfig.SlavePort} 31 } 32 }; 33 _connection = ConnectionMultiplexer.Connect(configurationOptions); 34 } 35 } 36 return _connection; 37 } 38 } 39 } 40 }
五. RedisHelper
实际上就是做了层序列化包装而已。
IRedisHelper:
using System; using StackExchange.Redis; using System.Collections.Generic; using System.Threading.Tasks; namespace Fantasy.RedisRepository.RedisHelpers { /// <summary> /// 异步方法接口 --Author 吴双 www.cnblogs.com/tdws /// 存入数据均为方法内部序列化后的byte,所以取数据的时候需要反序列化时,请指定正确的数据类型 /// </summary> public partial interface IRedisHelper { #region Redis数据类型—String /// <summary> /// 将任何数据以redis string存储 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <param name="timeout"></param> /// <returns></returns> Task<bool> StringSetAsync<T>(string key, T value, TimeSpan? timeout = null); /// <summary> /// 对数值进行减法操作,默认-1 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns>操作后的结果</returns> Task<long> StringDecrementAsync(string key, long value = 1L); /// <summary> /// 对数值进行加法操作,默认+1 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns>操作后的结果</returns> Task<long> StringIncrementAsync(string key, long value = 1L); /// <summary> /// 从redis string中以指定类型取出 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> Task<T> StringGetAsync<T>(string key); #endregion #region Redis数据类型—Hash /// <summary> /// 向Hash key中存储任意类型任意值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="field"></param> /// <param name="value"></param> /// <returns>是否成功</returns> Task<bool> HashSetAsync<T>(string key, string field, T value); /// <summary> /// 批量 向Hash key中存储任意类型任意值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="hashFields"></param> /// <returns>无返回值</returns> Task HashMultiSetAsync<T>(string key, Dictionary<string, T> hashFields); /// <summary> /// 对指定hash key中制定field做数量增加操作 默认自增1 /// 如果此操作前key不存在 则创建。 如果此操作前该field不存在或者非数字 则先被置0,再被继续操作 /// </summary> /// <param name="key"></param> /// <param name="field"></param> /// <param name="incrCount"></param> /// <returns>操作后的结果</returns> Task<long> HashIncrementAsync(string key, string field, long incrCount = 1); /// <summary> /// 对指定hash key中制定field做数量增加操作 默认自减1 /// 如果此操作前key不存在 则创建。 如果此操作前该field不存在或者非数字 则先被置0,再被继续操作 /// </summary> /// <param name="key"></param> /// <param name="field"></param> /// <param name="decrCount"></param> /// <returns>操作后的结果</returns> Task<long> HashDecrementAsync(string key, string field, long decrCount = 1); /// <summary> /// 从指定Hash中 删除指定field /// 如果key或者field不存在,则false /// </summary> /// <param name="key"></param> /// <param name="field"></param> /// <returns>是否成功</returns> Task<bool> HashDeleteFieldAsync(string key, string field); /// <summary> /// 从指定Hash key中 批量删除指定field /// 如果key或者field不存在,则false /// </summary> /// <param name="key"></param> /// <param name="fields"></param> /// <returns>移除数量</returns> Task<long> HashMultiDeleteFieldAsync(string key, List<string> fields); /// <summary> /// 从指定Hash key中获取指定field值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="field"></param> /// <returns></returns> Task<T> HashGetAsync<T>(string key, string field); /// <summary> /// 从指定Hash key中判断field是否存在 /// </summary> /// <param name="key"></param> /// <param name="field"></param> /// <returns></returns> Task<bool> HashFieldExistAsync(string key, string field); /// <summary> /// 获取指定Hash key中的所有field的值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> Task<List<T>> HashValuesAsync<T>(string key); /// <summary> /// 获取指定Hash key中所有 field名称及其Value /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> Task<Dictionary<string, T>> HashGetAllAsync<T>(string key); /// <summary> /// 获取指定Hash key中所有field /// </summary> /// <param name="key"></param> /// <returns></returns> Task<List<string>> HashFieldsAsync(string key); #endregion #region Redis数据类型—List /// <summary> /// 在指定pivot后插入value, 如果pivot不存在,则返回-1, 如果key不存在,则返回0 /// 如果存在多个相同指定的的pivot,则插入第一个指定pivot后面. /// 即链表从左向右查找,遇到指定pivot,则确定位置 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="pivot">list中的一个值</param> /// <param name="value"></param> /// <returns></returns> Task<long> ListInsertAfterAsync<T>(string key, string pivot, T value); /// <summary> /// 在指定pivot前插入value, 如果pivot不存在,则返回-1, 如果key不存在,则返回0 /// 如果存在多个相同指定的的pivot,则插入第一个指定pivot前面. /// 即链表从左向右查找,遇到指定pivot,则确定位置 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="pivot"></param> /// <param name="value"></param> /// <returns></returns> Task<long> ListInsertBeforeAsync<T>(string key, string pivot, T value); /// <summary> /// 从链表左侧弹出第一个元素(弹出能获取到该元素并且被删除) /// 如果key不存在 或者链表为空 则为null /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> Task<T> ListLeftPopAsync<T>(string key); /// <summary> /// 从链表左侧增加一个元素,key不存在则被创建 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <returns>返回操作后的链表长度</returns> Task<long> ListLeftPushAsync<T>(string key, T value); /// <summary> /// 从链表左侧批量增加元素,如果 a b c 则c会在链表左侧第一位 b第二位 a第三位 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="values"></param> /// <returns>返回操作后的链表长度</returns> Task<long> ListLeftMultiPushAsync<T>(string key, List<T> values); /// <summary> /// 获取链表长度,不存在key则为0 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> Task<long> ListLengthAsync<T>(string key); /// <summary> /// 获取链表中所有数据,从左侧start开始到stop结束,从0—-1则认为获取全部,默认获取全部 /// start为负数则代表从链表右侧开始,-1为右侧第一位,-2为右侧第二位 /// start要小于stop,否则返回null /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="start"></param> /// <param name="stop"></param> /// <returns></returns> Task<List<T>> ListRangeAsync<T>(string key, long start = 0L, long stop = -1L); /// <summary> /// 从链表中一处count数量的value. count大于0则从左至右,count小于0则从右至左,count=0则移除全部 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <param name="count"></param> /// <returns></returns> Task<long> ListRemoveAsync<T>(string key, T value, long count = 0L); /// <summary> /// 从右侧弹出第一个元素(弹出能获取到该元素并且被删除) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> Task<T> ListRightPopAsync<T>(string key); /// <summary> /// 从链表右侧加入元素,如果 rpush a b c 则c为右侧第一位 b第二位 c第三位 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> Task<long> ListRightPushAsync<T>(string key, T value); /// <summary> /// 从右侧批量插入,和左侧相反 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="values"></param> /// <returns></returns> Task<long> ListRightMultiPushAsync<T>(string key, List<T> values); /// <summary> /// 在链表指定索引处,插入元素 /// 正数索引从0开始,代表左侧。负数从-1开始 代表从右侧。-1为右侧第一位 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="index"></param> /// <param name="value"></param> /// <returns></returns> Task ListSetByIndexAsync<T>(string key, int index, T value); /// <summary> /// 留下start到stop之间的数据。负数代表从右侧寻找 -1为右侧第一位 /// </summary> /// <param name="key"></param> /// <param name="start"></param> /// <param name="stop"></param> /// <returns></returns> Task ListTrimAsync(string key, long start, long stop); /// <summary> /// 获取指定index的值,负数代表从右侧寻找 -1为右侧第一位 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="index"></param> /// <returns></returns> Task<T> ListGetByIndexAsync<T>(string key, long index); #endregion #region Redis数据类型—Set /// <summary> /// 向指定集合中增加一个元素 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> Task<bool> SetAddAsync<T>(string key, T value); /// <summary> /// 指定集合计算操作operation枚举,指定计算结果将存的目标destKey,指定需要参与计算的多个key /// </summary> /// <param name="operation"></param> /// <param name="destKey"></param> /// <param name="combineKeys"></param> /// <returns></returns> Task<long> SetCombineAndStoreAsync(SetOperation operation, string destKey, List<string> combineKeys); /// <summary> /// 指定集合计算操作operation枚举,指定需要参与计算的多个key /// </summary> /// <typeparam name="T"></typeparam> /// <param name="operation"></param> /// <param name="combineKeys"></param> /// <returns></returns> Task<List<T>> SetCombineAsync<T>(SetOperation operation, List<string> combineKeys); /// <summary> /// 指定值是否存在于指定集合中 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> Task<bool> SetContainsAsync<T>(string key, T value); /// <summary> /// 获取指定集合中元素个数 /// </summary> /// <param name="key"></param> /// <returns></returns> Task<long> SetLengthAsync(string key); /// <summary> /// 获取指定集合中的所有元素 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> Task<List<T>> SetMembersAsync<T>(string key, T value); /// <summary> /// 从sourceKey移除指定value到目标distKey集合当中 /// 如果sourceKey存在指定value则返回true,否则不做任何操作返回false /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sourcekey"></param> /// <param name="distKey"></param> /// <param name="value"></param> /// <returns></returns> Task<bool> SetMoveAsync<T>(string sourcekey, string distKey, T value); /// <summary> /// 从指定集合当中随机取出一个元素 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> Task<T> SetRandomMemberAsync<T>(string key); /// <summary> /// 从指定集合随机弹出(删除并获取)一个元素 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> Task<T> SetPopAsync<T>(string key); /// <summary> /// 从集合中随机弹出(删除并获取)多个元素 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> Task<List<T>> SetRandomMembersAsync<T>(string key); /// <summary> /// 从集合中移除指定元素 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> Task<bool> SetRemoveAsync<T>(string key, T value); /// <summary> /// 从集合中批量移除元素 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="values"></param> /// <returns></returns> Task<long> SetMultiRemoveAsync<T>(string key, List<T> values); #endregion #region Redis数据类型—SortSet #endregion #region Redis Key操作 /// <summary> /// 删除指定key /// </summary> /// <param name="key"></param> /// <returns></returns> Task<bool> KeyDeleteAsync(string key); /// <summary> /// 设置key过期时间具体DateTime /// </summary> /// <param name="key"></param> /// <param name="expireAt"></param> /// <returns></returns> Task<bool> KeyExpireAtAsync(string key, DateTime expireAt); /// <summary> /// 设置key在将来的timeout后过期(TimeSpan) /// </summary> /// <param name="key"></param> /// <param name="timeout"></param> /// <returns></returns> Task<bool> KeyExpireInAsync(string key, TimeSpan timeout); /// <summary> /// key重命名 /// </summary> /// <param name="key"></param> /// <param name="newKey"></param> /// <returns></returns> Task<bool> KeyRenameAsync(string key, string newKey); /// <summary> /// 判断key是否已存在 /// </summary> /// <param name="key"></param> /// <returns></returns> Task<bool> KeyExistsAsync(string key); #endregion #region Redis Transcation /// <summary> /// 在事务中执行一系列redis命令。注意:在委托中的一系列命令的所有 值 都需要进行字节数组序列化 /// </summary> /// <param name="ranOperations"></param> /// <returns></returns> Task<bool> DoInTranscationAsync(Action<ITransaction> ranOperations); #endregion Task<RedisResult> Test(); } }
RedisHelper部分类RedisStringHelperAsync.cs
1 using System; 2 using Fantasy.RedisRepository.CommonHelper; 3 using StackExchange.Redis; 4 using System.Threading.Tasks; 5 6 namespace Fantasy.RedisRepository.RedisHelpers 7 { 8 /// <summary> 9 /// Redis异步操作类 String部分类 10 /// </summary> 11 internal partial class RedisHelper// : IRedisHelper 12 { 13 14 private static IDatabase _client; 15 16 internal RedisHelper() 17 { 18 _client = RedisConnection.GenerateConnection.GetDatabase(); 19 } 20 21 #region String 写操作 22 23 /// <summary> 24 /// 将任何数据添加到redis中 25 /// </summary> 26 /// <typeparam name="T"></typeparam> 27 /// <param name="key"></param> 28 /// <param name="value"></param> 29 /// <param name="timeout"></param> 30 /// <returns></returns> 31 public async Task<bool> StringSetAsync<T>(string key, T value, TimeSpan? timeout = null) 32 { 33 return await _client.StringSetAsync(key, SerializeHelper.Serialize(value), timeout); 34 } 35 36 public async Task<long> StringDecrementAsync(string key, long value = 1L) 37 { 38 return await _client.StringDecrementAsync(key, value); 39 } 40 41 public async Task<long> StringIncrementAsync(string key, long value = 1L) 42 { 43 return await _client.StringIncrementAsync(key, value); 44 } 45 #endregion 46 47 #region String 读操作 48 49 /// <summary> 50 /// 根据key获取指定类型数据 51 /// </summary> 52 /// <typeparam name="T"></typeparam> 53 /// <param name="key"></param> 54 /// <returns></returns> 55 public async Task<T> StringGetAsync<T>(string key) 56 { 57 return SerializeHelper.Deserialize<T>(await _client.StringGetAsync(key, CommandFlags.PreferSlave)); 58 } 59 60 #endregion 61 62 } 63 }
RedisHelper部分类RedisHashHelperAsync.cs
1 using Fantasy.RedisRepository.CommonHelper; 2 using StackExchange.Redis; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Threading.Tasks; 6 7 namespace Fantasy.RedisRepository.RedisHelpers 8 { 9 /// <summary> 10 /// Redis异步操作类 Hash部分类 11 /// </summary> 12 internal partial class RedisHelper 13 { 14 #region Hash 写操作 15 16 public async Task<bool> HashSetAsync<T>(string key, string field, T value) 17 { 18 return await _client.HashSetAsync(key, field, SerializeHelper.Serialize(value)); 19 } 20 21 public async Task HashMultiSetAsync<T>(string key, Dictionary<string, T> hashFields) 22 { 23 List<HashEntry> entries = new List<HashEntry>(); 24 hashFields.ToList().ForEach(d => entries.Add(new HashEntry(d.Key, SerializeHelper.Serialize(d.Value)))); 25 await _client.HashSetAsync(key, entries.ToArray()); 26 } 27 28 public async Task<long> HashIncrementAsync(string key, string field, long incrCount = 1) 29 { 30 return await _client.HashIncrementAsync(key, field, incrCount); 31 } 32 33 public async Task<long> HashDecrementAsync(string key, string field, long decrCount = 1) 34 { 35 return await _client.HashDecrementAsync(key, field, decrCount); 36 } 37 38 public async Task<bool> HashDeleteFieldAsync(string key, string field) 39 { 40 return await _client.HashDeleteAsync(key, field); 41 } 42 43 public async Task<long> HashMultiDeleteFieldAsync(string key, List<string> fields) 44 { 45 List<RedisValue> values = new List<RedisValue>(); 46 fields.ForEach(f => values.Add(f)); 47 return await _client.HashDeleteAsync(key, values.ToArray()); 48 } 49 50 #endregion 51 52 #region Hash 读操作 53 54 /// <summary> 55 /// Redis 指定hash类型key中field是否存在 56 /// </summary> 57 /// <param name="key"></param> 58 /// <param name="field"></param> 59 /// <returns></returns> 60 public async Task<bool> HashFieldExistAsync(string key, string field) 61 { 62 return await _client.HashExistsAsync(key, field, CommandFlags.PreferSlave); 63 } 64 public async Task<List<string>> HashFieldsAsync(string key) 65 { 66 RedisValue[] values = await _client.HashKeysAsync(key, CommandFlags.PreferSlave); 67 return RedisInnerTypeHelper.RedisValuesToGenericList<string>(values); 68 } 69 public async Task<List<T>> HashValuesAsync<T>(string key) 70 { 71 var values = await _client.HashValuesAsync(key, CommandFlags.PreferSlave); 72 return RedisInnerTypeHelper.RedisValuesToGenericList<T>(values); 73 } 74 75 public async Task<T> HashGetAsync<T>(string key, string field) 76 { 77 return SerializeHelper.Deserialize<T>(await _client.HashGetAsync(key, field, CommandFlags.PreferSlave)); 78 } 79 80 public async Task<Dictionary<string, T>> HashGetAllAsync<T>(string key) 81 { 82 HashEntry[] entries = await _client.HashGetAllAsync(key, CommandFlags.PreferSlave); 83 Dictionary<string, T> dic = new Dictionary<string, T>(); 84 entries.ToList().ForEach(e => dic.Add(e.Name, SerializeHelper.Deserialize<T>(e.Value))); 85 return dic; 86 } 87 88 #endregion 89 90 } 91 }
RedisHelper部分类RedisListHelperAsync.cs
1 using Fantasy.RedisRepository.CommonHelper; 2 using StackExchange.Redis; 3 using System.Collections.Generic; 4 using System.Threading.Tasks; 5 6 namespace Fantasy.RedisRepository.RedisHelpers 7 { 8 /// <summary> 9 /// Redis异步操作类 List部分类 10 /// </summary> 11 internal partial class RedisHelper 12 { 13 // _client.ListRightPopLeftPushAsync(); 14 // _client.ListTrimAsync(); 15 16 #region List 写操作 17 18 public async Task<long> ListInsertAfterAsync<T>(string key, string pivot, T value) 19 { 20 return await _client.ListInsertAfterAsync(key, pivot, SerializeHelper.Serialize(value)); 21 } 22 23 public async Task<long> ListInsertBeforeAsync<T>(string key, string pivot, T value) 24 { 25 return await _client.ListInsertBeforeAsync(key, pivot, SerializeHelper.Serialize(value)); 26 } 27 28 public async Task<T> ListLeftPopAsync<T>(string key) 29 { 30 return SerializeHelper.Deserialize<T>(await _client.ListLeftPopAsync(key)); 31 } 32 33 public async Task<long> ListLeftPushAsync<T>(string key, T value) 34 { 35 return await _client.ListLeftPushAsync(key, SerializeHelper.Serialize(value)); 36 } 37 38 public async Task<long> ListLeftMultiPushAsync<T>(string key, List<T> values) 39 { 40 return await _client.ListLeftPushAsync(key, SerializeHelper.Serialize(RedisInnerTypeHelper.GenericListToRedisValues(values))); 41 } 42 43 public async Task<long> ListRemoveAsync<T>(string key, T value, long count = 0L) 44 { 45 return await _client.ListRemoveAsync(key, SerializeHelper.Serialize(value), count); 46 } 47 48 public async Task<T> ListRightPopAsync<T>(string key) 49 { 50 return SerializeHelper.Deserialize<T>(await _client.ListRightPopAsync(key)); 51 } 52 53 public async Task<long> ListRightPushAsync<T>(string key, T value) 54 { 55 return await _client.ListRightPushAsync(key, SerializeHelper.Serialize(value)); 56 } 57 58 public async Task<long> ListRightMultiPushAsync<T>(string key, List<T> values) 59 { 60 return 61 await 62 _client.ListRightPushAsync(key, 63 SerializeHelper.Serialize(RedisInnerTypeHelper.GenericListToRedisValues(values))); 64 } 65 66 public async Task ListSetByIndexAsync<T>(string key, int index, T value) 67 { 68 await _client.ListSetByIndexAsync(key, index, SerializeHelper.Serialize(value)); 69 } 70 71 public async Task ListTrimAsync(string key, long start, long stop) 72 { 73 await _client.ListTrimAsync(key, start, stop); 74 } 75 #endregion 76 77 #region List 读操作 78 79 public async Task<T> ListGetByIndexAsync<T>(string key, long index) 80 { 81 return SerializeHelper.Deserialize<T>(await _client.ListGetByIndexAsync(key, index, CommandFlags.PreferSlave)); 82 } 83 84 public async Task<long> ListLengthAsync<T>(string key) 85 { 86 return await _client.ListLengthAsync(key, CommandFlags.PreferSlave); 87 } 88 89 public async Task<List<T>> ListRangeAsync<T>(string key, long start = 0L, long stop = -1L) 90 { 91 return RedisInnerTypeHelper.RedisValuesToGenericList<T>(await _client.ListRangeAsync(key, start, stop, CommandFlags.PreferSlave)); 92 } 93 94 #endregion 95 } 96 }
RedisLuaHelper.cs 这里打算装一些功能行lua脚本, 外部依然是传key一类的参数,这个不完整,只是个实例。
1 using StackExchange.Redis; 2 using System.Threading.Tasks; 3 4 namespace Fantasy.RedisRepository.RedisHelpers 5 { 6 internal partial class RedisHelper 7 { 8 public async Task<RedisResult> LuaMutilGetHash() 9 { 10 string lua = @"local result={} 11 for i, v in ipairs(KEYS) do 12 result[i] = redis.call('hgetall',v) 13 end 14 return result"; 15 var res = await _client.ScriptEvaluateAsync(lua, new RedisKey[] { "people:1", "people:2", "people:3" }); 16 var res1= LuaScript.GetCachedScriptCount(); 17 return res; 18 } 19 } 20 }
关于Transcation的封装,我个人没有什么好的方法,提供了这样一个方法
1 public async Task<bool> DoInTranscationAsync(Action<ITransaction> runOperations) 2 { 3 var tran = RedisConnection.GenerateConnection.GetDatabase().CreateTransaction(); 4 5 runOperations(tran); 6 7 return await tran.ExecuteAsync(); 8 }
RedisFactory.cs
1 using Fantasy.RedisRepository.RedisHelpers; 2 3 namespace Fantasy.RedisRepository 4 { 5 public class RedisFactory 6 { 7 /// <summary> 8 /// 外部访问redis入口,暂时只暴露异步方法 9 /// </summary> 10 /// <returns></returns> 11 public static IRedisHelper CreateRedisRepository() 12 { 13 return new RedisHelper(); 14 } 15 } 16 }
六. 写在最后
自用RedisRepository分享,仅供参考。有交流才有进步,希望能得到更好的建议,做更好的改进。
如果我的点滴分享对你有点地帮助,欢迎点击下方红色按钮关注,我将持续输出分享。也欢迎为我也为你自己点赞支持。
参考:https://github.com/StackExchange/StackExchange.Redis/tree/master/Docs
——保持学习,谨记谦虚。不端不装,有趣有梦。
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- MVC异步分页代码分享 2020-02-21
- 分享基于EF6、Unitwork、Autofac的Repository模式设计 2019-07-23
- Swagger实例分享(VS+WebApi+Swashbuckle) 2019-07-23
- 面试经验分享(面试要主动) 2018-10-08
- [ASP.NET]大文件无法上传排查经验分享 2018-06-28
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash