一. 写在前面
毕业工作后,今天终于能回家了,回想了一些这半年来所做的内容,总是觉得还停留在那么基础的水平 ,在解决各种问题的过程中,自己的创新思路比较少,靠搜索来的比较多 。不想做16年的总结了 ,希望17年能学更多的我爱的技术,看更多的开源代码,能学到更多的设计思想和代码思路,能再更新这两年来对代码的理解。
这篇分享,主要是弥补我之前RedisRepository的不足。
半年前由于我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却是好用的,老司机如果有好的例子,希望老司机给些指导或者分享。
二. 代码结构,仅供参考
结构大概就是这样,RedisAsyncHelper下的所有类都是部分类,他们的类名称是RedisHelper。他们共同实现了IRedisHelper的接口,并且留下了详细的注释。
同步版本和异步版本的目录结构是一样的。
三. 预备阶段
CommonHelper中的两个帮助类:
RedisInnerTypeHelper.cs
using StackExchange.Redis; using System.Collections.Generic; using System.Linq; namespace Fantasy.RedisRepository.CommonHelper { internal class RedisInnerTypeHelper { public static List<T> RedisValuesToGenericList<T>(RedisValue[] redisValues) { var result = new List<T>(); redisValues.ToList().ForEach(r => result.Add(SerializeHelper.Deserialize<T>(r))); return result; } public static RedisValue[] GenericListToRedisValues<T>(List<T> values) { var redisValues = new List<RedisValue>(); values.ForEach(v => redisValues.Add(SerializeHelper.Serialize(values))); return redisValues.ToArray(); } public static RedisKey[] GenericListToRedisKeys(List<string> keys) { var redisKeys = new List<RedisKey>(); keys.ForEach(k => redisKeys.Add(k)); return redisKeys.ToArray(); } } }</div>
SerializeHelper.cs
using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace Fantasy.RedisRepository.CommonHelper { internal static class SerializeHelper { /// <summary> /// 字节数组序列化 /// </summary> /// <param name="o"></param> /// <returns></returns> internal static byte[] Serialize(object o) { if (o == null) { return null; } BinaryFormatter binaryFormatter = new BinaryFormatter(); using (MemoryStream memoryStream = new MemoryStream()) { binaryFormatter.Serialize(memoryStream, o); byte[] objectDataAsStream = memoryStream.ToArray(); return objectDataAsStream; } } /// <summary> /// 字节数组反序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="stream"></param> /// <returns></returns> internal static T Deserialize<T>(byte[] stream) { if (stream == null) { return default(T); } BinaryFormatter binaryFormatter = new BinaryFormatter(); using (MemoryStream memoryStream = new MemoryStream(stream)) { T result = (T)binaryFormatter.Deserialize(memoryStream); return result; } } } }</div>
Config中的配置类:
ConfigHelper.cs
using System; using System.Configuration; namespace Fantasy.RedisRepository.Config { internal class ConfigHelper { internal static T Get<T>(string appSettingsKey, T defaultValue) { string text = ConfigurationManager.AppSettings[appSettingsKey]; if (string.IsNullOrWhiteSpace(text)) return defaultValue; try { var value = Convert.ChangeType(text, typeof(T)); return (T)value; } catch { return defaultValue; } } } }</div>
RedisClientConfig.cs
namespace Fantasy.RedisRepository.Config { internal class RedisClientConfig { private static string _server = ConfigHelper.Get("RedisServer", "115.xx.xx.31"); /// <summary> /// 节点IP /// </summary> public static string Server { get { return _server; } set { _server = value; } } private static int _port = ConfigHelper.Get("RedisPort", 6380); /// <summary> /// 节点端口 /// </summary> public static int Port { get { return _port; } set { _port = value; } } private static string _slaveServer = ConfigHelper.Get("SlaveServer", "115.xx.xx.31"); /// <summary> /// 节点IP /// </summary> public static string SlaveServer { get { return _slaveServer; } set { _slaveServer = value; } } private static int _slavePort = ConfigHelper.Get("SlavePort", 6381); /// <summary> /// 节点端口 /// </summary> public static int SlavePort { get { return _slavePort; } set { _slavePort = value; } } private static string _auth = ConfigHelper.Get("RedisAuth", "fantasy.."); /// <summary> /// 节点密码 /// </summary> public static string RedisAuth { get { return _auth; } set { _auth = value; } } private static int _defaultDatabase = ConfigHelper.Get("RedisDataBase", 0); /// <summary> /// redis默认0号库 /// </summary> public static int DefaultDatabase { get { return _defaultDatabase; } set { _defaultDatabase = value; } } private static int _connectTimeout = 10000; public static int ConnectTimeout { get { return _connectTimeout; } set { _connectTimeout = value; } } private static int _connectRetry = 3; public static int ConnectRetry { get { return _connectRetry; } set { _connectRetry = value; } } private static bool _preserveAsyncOrder = false; public static bool PreserveAsyncOrder { get { return _preserveAsyncOrder; } set { _preserveAsyncOrder = value; } } } }</div>
RedisConnection.cs