一、引言

今天开始有关Redis学习的第九篇文章了,以后肯定会大量系统使用Redis作为缓存介质,为了更好的更好的Redis,自己写了两个工具类,但是这两个工具类,没有提供一致的接口,是为了使用的独立性。测试已经完毕,可以正常访问Windows和Linux版本上的Redis服务,各种操作也没问题。今天就把主要代码贴出来,因为有一部分是需要配置文件的,我自己独立写了一套配置系统,这套配置系统就不贴出来,大家可以根据自己的理解来写这个部分的内容,好了,开始我们今天的写作吧。

二、简介

我写了两个工具类,第一个是以【StackExchange.Redis】为实现技术的Redis的客户端工具类,另一个是以【ServiceStack.Redis】为实现技术的Redis客户端工具类。【ServiceStack.Redis】的官网主页:https://servicestack.net/redis,【ServiceStack.Redis】在github上的主页:https://github.com/ServiceStack/ServiceStack.Redis,【ServiceStack.Redis】和Visual Studio 2015整个也很方便,微软在这块做的的很不过,我们可以通过命令直接安装:Install-Package ServiceStack.Redis,当然我们也可以通过NuGet来安装,安装完成,在您项目的【引用】里面会生成四个dll,分别是:ServiceStack.Common,ServiceStack.Interfaces,ServiceStack.Redis,ServiceStack.Client 四个程序集,然后在项目中使用Using引入就可以正常使用了。【ServiceStack.Redis】是Redis官方推荐的C#客户端,性能非常优越,使用也很方便,但是从v4版本已经逐渐商业化了,目的可以想而知,嗨,都是钱惹的祸。

上面我说道了,【ServiceStack.Redis】后来就商业化了,我想了想光靠他也是不行了,还要在写一个版本的工具类,所以就选择了【StackExchange.Redis】,【StackExchange.Redis】也是开源的,到目前,一直都是开源的,它的地址是:https://github.com/StackExchange/StackExchange.Redis,我使用的是.net 4.5,工具采用 vs2015, StackExchange.Redis版本是1.0.488。工具类还会在持续的使用过程中,我还会更新和修改的。

三、实例代码
 
         这是Redis客户端的配置文件的格式,格式很简单,可以分开配置,也可以合在一起配置。代码中标红的是和我的配置系统有关的代码,大家请注意。

  1. <configuration>
  2. <configSections>
  3. <section name="Framework" type="Enterprise.Framework.Configuration.ConfigurationFrameworkSectionHandler,Enterprise.Framework.Configuration" />
  4. </configSections>
  5.  
  6. <Framework type="Enterprise.Framework.Configuration.ConfigurationFrameworkManager,Enterprise.Framework.Configuration">
  7. <Enterprise.Framework.NoSQL>
  8. <RedisClients>
  9. <RedisClient name="ServiceStack" technology="ServiceStack.Redis">
  10. <readWriteHosts>192.168.3.11:6379,192.168.3.11:6380</readWriteHosts>
  11. <!--<readOnlyHosts>可以省略该项</readOnlyHosts>-->
  12. <!--<maxWritePoolSize>可以省略该项</maxWritePoolSize>-->
  13. <!--<maxReadPoolSize>可以省略该项</maxReadPoolSize>-->
  14. <!--<password>可以省略该项</password>-->
  15. <!--<autoStart>可以省略该项</autoStart>-->
  16. </RedisClient>
  17. <RedisClient name="StackExchange" technology="StackExchange.Redis">
  18. <host>192.168.131.1:6379</host>
  19. <!--<password>可以省略该项</password>-->
  20. </RedisClient>
  21. </RedisClients>
  22. </Enterprise.Framework.NoSQL>
  23. </Framework>
  24. </configuration>

        1、这是以【ServiceStack.Redis】为实现技术的工具类,对外界的访问接口提供了2个,第一个是以配置文件中自定义的名称参数的,红色代码是和我独立的配置系统相关联的,另一个访问接口是以配置实体类参数的,代码很简单,不多说了。

      工具类:ServiceStackRedisClientProvider.cs

  1. 1 /// <summary>
  2. 2 /// 通过ServiceStack.Redis实现的Redis的客户端操作类型
  3. 3 /// </summary>
  4. 4 public sealed class ServiceStackRedisClientProvider
  5. 5 {
  6. 6 #region 私有变量
  7. 7
  8. 8 //线程同步变量
  9. 9 private static readonly object lockObject = new object();
  10. 10
  11. 11 //redis链接池管理对象
  12. 12 private static volatile PooledRedisClientManager _instance = null;
  13. 13
  14. 14 //配置文件里面的ServiceStack详细配置设置
  15. 15 private static ServiceStackDetails _serviceStackDetails;
  16. 16
  17. 17 //可以自行配置ServiceStack的配置对象
  18. 18 private static ServiceStackConfigEntry _serviceStackConfigEntry;
  19. 19
  20. 20 #endregion
  21. 21
  22. 22 #region 私有构造函数
  23. 23
  24. 24 /// <summary>
  25. 25 /// 私有构造函数,禁止外部通过new关键字来创建该对象实例
  26. 26 /// </summary>
  27. 27 private ServiceStackRedisClientProvider() { }
  28. 28
  29. 29 #endregion
  30. 30
  31. 31 #region 获取PooledRedisClientManager实例的方法
  32. 32
  33. 33 /// <summary>
  34. 34 /// 获取redis链接池管理对象实例
  35. 35 /// 实例发生变化的集中情况:
  36. 36 /// 1.实例为空
  37. 37 /// 2.配置文件发生变化
  38. 38 /// </summary>
  39. 39 /// <param name="startByConfigFile">这是一个布尔值,true表示根据配置文件的配置启动,false表示是根据配置对象启动</param>
  40. 40 /// <returns>返回PooledRedisClientManager类型的对象实例</returns>
  41. 41 private static PooledRedisClientManager GetInstance(bool startByConfigFile)
  42. 42 {
  43. 43 if (_instance == null)
  44. 44 {
  45. 45 lock (lockObject)
  46. 46 {
  47. 47 if (_instance == null)
  48. 48 {
  49. 49 string[] readWriteServerList=null;
  50. 50 string[] readOnlyServerList=null;
  51. 51 RedisClientManagerConfig managerConfig=null;
  52. 52
  53. 53 //根据我们配置文件中数据来设置启动信息(app.config或者web.config)
  54. 54 if (startByConfigFile && (_serviceStackDetails != null))
  55. 55 {
  56. 56 managerConfig = new RedisClientManagerConfig()
  57. 57 {
  58. 58 AutoStart = _serviceStackDetails.AutoStart,
  59. 59 MaxReadPoolSize = _serviceStackDetails.MaxReadPoolSize,
  60. 60 MaxWritePoolSize = _serviceStackDetails.MaxWritePoolSize,
  61. 61 };
  62. 62
  63. 63 readWriteServerList = GetRedisHosts(_serviceStackDetails.ReadWriteHosts);
  64. 64 readOnlyServerList = GetRedisHosts(_serviceStackDetails.ReadOnlyHosts);
  65. 65 }
  66. 66 else if (!startByConfigFile && (_serviceStackConfigEntry != null))//根据配置对象来设置启动信息(ServiceStackConfigEntry)
  67. 67 {
  68. 68 managerConfig = new RedisClientManagerConfig()
  69. 69 {
  70. 70 AutoStart = _serviceStackConfigEntry.AutoStart,
  71. 71 MaxReadPoolSize = _serviceStackConfigEntry.MaxReadPoolSize,
  72. 72 MaxWritePoolSize = _serviceStackConfigEntry.MaxWritePoolSize,
  73. 73 };
  74. 74
  75. 75 readWriteServerList = GetRedisHosts(_serviceStackConfigEntry.ReadWriteHosts);
  76. 76 readOnlyServerList = GetRedisHosts(_serviceStackConfigEntry.ReadOnlyHosts);
  77. 77 }
  78. 78 else
  79. 79 {
  80. 80 throw new InvalidOperationException("Redis客户端初始化配置失败!");
  81. 81 }
  82. 82
  83. 83 if ((readWriteServerList != null && readWriteServerList.Length > 0)&&(readOnlyServerList != null && readOnlyServerList.Length <= 0))
  84. 84 {
  85. 85 _instance = new PooledRedisClientManager(readWriteServerList);
  86. 86 }
  87. 87
  88. 88 if ((readWriteServerList != null && readWriteServerList.Length > 0) && (readOnlyServerList != null && readOnlyServerList.Length > 0))
  89. 89 {
  90. 90 _instance = new PooledRedisClientManager(readWriteServerList, readOnlyServerList, managerConfig);
  91. 91 }
  92. 92 }
  93. 93 }
  94. 94 }
  95. 95 return _instance;
  96. 96 }
  97. 97
  98. 98 /// <summary>
  99. 99 /// 解析Redis服务器列表,该列表格式IP[:Port]
  100. 100 /// </summary>
  101. 101 /// <param name="redisHosts">包含一个或者多个Redis服务器地址的字符串列表,以逗号做为分隔符</param>
  102. 102 /// <returns>返回Redis服务器地址列表</returns>
  103. 103 private static string[] GetRedisHosts(string redisHosts)
  104. 104 {
  105. 105 if (string.IsNullOrWhiteSpace(redisHosts) || string.IsNullOrEmpty(redisHosts))
  106. 106 {
  107. 107 return new string[] { };
  108. 108 }
  109. 109 var hosts=redisHosts.Split(',');
  110. 110 foreach (var host in hosts)
  111. 111 {
  112. 112 if (!Regex.IsMatch(host, @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]):\d{3,4}$"))
  113. 113 {
  114. 114 throw new InvalidOperationException("Redis服务器地址格式不正确!");
  115. 115 }
  116. 116 }
  117. 117 return hosts;
  118. 118 }
  119. 119
  120. 120 #endregion
  121. 121
  122. 122 #region 提供对外访问接口
  123. 123
  124. 124 /// <summary>
  125. 125 /// 获取Redis客户端对象实例
  126. 126 /// </summary>
  127. 127 /// <param name="redisClientName">在配置文件中,Redis客户端的名称</param>
  128. 128 /// <param name="databaseIndex">redis逻辑分为16个数据库,排序为:0-15,我们默认使用的是0号数据库,数据库当前的索引值</param>
  129. 129 /// <returns>返回IRedisClient对象实例</returns>
  130. 130 public static IRedisClient GetRedisClient(string redisClientName,int databaseIndex = 0)
  131. 131 {
  132. 132 //获取配置数据
  133. 133 ParameterValidityChecker.RequiredParameterStringNotNullOrWhiteSpace(redisClientName, "Redis客户端的名称不能为空!");
  134. 134 var _configurationManager = (ConfigurationFrameworkManager)ConfigurationManager.GetSection("Framework");
  135. 135 if (_configurationManager != null)
  136. 136 {
  137. 137 _serviceStackDetails = _configurationManager.RedisClientConfiguration.GetServiceStackDetails(redisClientName);
  138. 138 if (_serviceStackDetails == null)
  139. 139 {
  140. 140 throw new InvalidOperationException("以ServiceStack.Redis为实现技术的Redis客户端的配置有误!");
  141. 141 }
  142. 142 }
  143. 143 else
  144. 144 {
  145. 145 throw new InvalidOperationException("以ServiceStack.Redis为实现技术的Redis客户端的配置有误!");
  146. 146 }
  147. 147
  148. 148 //实例化Redis客户端实例对象
  149. 149 var pooledRedisClientManager = GetInstance(true);
  150. 150 var redisClient = pooledRedisClientManager.GetClient();
  151. 151 if (!string.IsNullOrEmpty(_serviceStackDetails.Password))
  152. 152 {
  153. 153 redisClient.Password = _serviceStackDetails.Password;
  154. 154 }
  155. 155 redisClient.Db = databaseIndex;
  156. 156 return redisClient;
  157. 157 }
  158. 158
  159. 159 /// <summary>
  160. 160 /// 获取Redis客户端对象实例
  161. 161 /// </summary>
  162. 162 /// <param name="serviceStackConfigEntry">在配置文件中,Redis客户端的名称</param>
  163. 163 /// <param name="databaseIndex">redis逻辑分为16个数据库,排序为:0-15,我们默认使用的是0号数据库,数据库当前的索引值</param>
  164. 164 /// <returns>返回IRedisClient对象实例</returns>
  165. 165 public static IRedisClient GetRedisClient(ServiceStackConfigEntry serviceStackConfigEntry, int databaseIndex = 0)
  166. 166 {
  167. 167 //获取配置数据
  168. 168 if (serviceStackConfigEntry == null)
  169. 169 {
  170. 170 throw new ArgumentNullException("以ServiceStack.Redis为实现技术的Redis客户端的配置对象不能为空!");
  171. 171 }
  172. 172 else
  173. 173 {
  174. 174 _serviceStackConfigEntry = serviceStackConfigEntry;
  175. 175 }
  176. 176
  177. 177 if (string.IsNullOrEmpty(_serviceStackConfigEntry.ReadWriteHosts) || string.IsNullOrWhiteSpace(_serviceStackConfigEntry.ReadWriteHosts))
  178. 178 {
  179. 179 throw new InvalidOperationException("【ReadWriteHosts】必须设置其值!");
  180. 180 }
  181. 181
  182. 182 //实例化Redis客户端实例对象
  183. 183 var pooledRedisClientManager = GetInstance(false);
  184. 184 var redisClient = pooledRedisClientManager.GetClient();
  185. 185 if (!string.IsNullOrEmpty(_serviceStackConfigEntry.Password)&&!string.IsNullOrWhiteSpace(_serviceStackConfigEntry.Password))
  186. 186 {
  187. 187 redisClient.Password = _serviceStackConfigEntry.Password;
  188. 188 }
  189. 189 redisClient.Db = databaseIndex;
  190. 190 return redisClient;
  191. 191 }
  192. 192
  193. 193 #endregion
  194. 194 }

     配置实体类 ServiceStackConfigEntry.cs的代码:

  1. 1 /// <summary>
  2. 2 /// 配置文件中,以ServiceStack.Redis为实现技术的配置Redis的详情
  3. 3 /// </summary>
  4. 4 public sealed class ServiceStackConfigEntry
  5. 5 {
  6. 6 #region 构造函数
  7. 7
  8. 8 /// <summary>
  9. 9 /// 给配置参数初始化默认值
  10. 10 /// </summary>
  11. 11 public ServiceStackConfigEntry()
  12. 12 {
  13. 13 ReadWriteHosts = "127.0.0.1:6379";
  14. 14 ReadOnlyHosts = string.Empty;
  15. 15 MaxWritePoolSize = 200;
  16. 16 MaxReadPoolSize = 200;
  17. 17 Password = string.Empty;
  18. 18 AutoStart = true;
  19. 19 }
  20. 20
  21. 21 #endregion
  22. 22
  23. 23 #region 配置属性
  24. 24
  25. 25 /// <summary>
  26. 26 /// 可读可写的Redis服务器地址,多个地址以逗号分隔,例如:192.168.127.11:6379,192.168.127.128:6380
  27. 27 /// </summary>
  28. 28 public string ReadWriteHosts { get; set; }
  29. 29
  30. 30 /// <summary>
  31. 31 /// 只能读的Redis服务器地址,多个地址以逗号分隔,例如:192.168.127.11:6379,192.168.127.128:6380
  32. 32 /// </summary>
  33. 33 public string ReadOnlyHosts { get; set; }
  34. 34
  35. 35 /// <summary>
  36. 36 /// 最大写链接数
  37. 37 /// </summary>
  38. 38 public int MaxWritePoolSize { get; set; }
  39. 39
  40. 40 /// <summary>
  41. 41 /// 最大读链接数
  42. 42 /// </summary>
  43. 43 public int MaxReadPoolSize { get; set; }
  44. 44
  45. 45 /// <summary>
  46. 46 /// 登陆Redis服务器的密码
  47. 47 /// </summary>
  48. 48 public string Password { get; set; }
  49. 49
  50. 50 /// <summary>
  51. 51 /// 是否自动启动
  52. 52 /// </summary>
  53. 53 public bool AutoStart { get; set; }
  54. 54
  55. 55 #endregion
  56. 56 }

      获取配置文件详情的类型:ServiceStackDetails.cs

  1. /// <summary>
  2. /// 配置文件中,以ServiceStack.Redis为实现技术的配置Redis的详情
  3. /// </summary>
  4. public sealed class ServiceStackDetails
  5. {
  6. #region 构造函数
  7.  
  8. /// <summary>
  9. /// 给配置参数初始化默认值
  10. /// </summary>
  11. public ServiceStackDetails()
  12. {
  13. ReadWriteHosts = "127.0.0.1:6379";
  14. ReadOnlyHosts = string.Empty;
  15. MaxWritePoolSize = 200;
  16. MaxReadPoolSize = 200;
  17. Password = string.Empty;
  18. AutoStart = true;
  19. }
  20.  
  21. #endregion
  22.  
  23. #region 配置属性
  24.  
  25. /// <summary>
  26. /// 可读可写的Redis服务器地址,多个地址以逗号分隔,例如:192.168.127.11:6379,192.168.127.128:6380
  27. /// </summary>
  28. public string ReadWriteHosts { get; internal set; }
  29.  
  30. /// <summary>
  31. /// 只能读的Redis服务器地址,多个地址以逗号分隔,例如:192.168.127.11:6379,192.168.127.128:6380
  32. /// </summary>
  33. public string ReadOnlyHosts { get; internal set; }
  34.  
  35. /// <summary>
  36. /// 最大写链接数
  37. /// </summary>
  38. public int MaxWritePoolSize { get; internal set; }
  39.  
  40. /// <summary>
  41. /// 最大读链接数
  42. /// </summary>
  43. public int MaxReadPoolSize { get; internal set; }
  44.  
  45. /// <summary>
  46. /// 登陆Redis服务器的密码
  47. /// </summary>
  48. public string Password { get; internal set; }
  49.  
  50. /// <summary>
  51. /// 是否自动启动
  52. /// </summary>
  53. public bool AutoStart { get; internal set; }
  54.  
  55. #endregion
  56. }

   2、这是以【StackExchange.Redis】为实现技术的工具类,对外界的访问接口提供了2个,第一个是以配置文件中自定义的名称参数的,红色代码是和我独立的配置系统相关联的,另一个访问接口是以配置实体类参数的,代码很简单,不多说了。

       工具类: StackExchangeRedisClientProvider.cs

  1. 1 /// <summary>
  2. 2 /// 通过StackExchange.Redis实现的Redis的客户端操作类型
  3. 3 /// </summary>
  4. 4 public sealed class StackExchangeRedisClientProvider
  5. 5 {
  6. 6 #region 私有字段
  7. 7
  8. 8 /// <summary>
  9. 9 /// 线程同步变量
  10. 10 /// </summary>
  11. 11 private static readonly object lockObject = new object();
  12. 12
  13. 13 /// <summary>
  14. 14 /// redis链接池管理对象
  15. 15 /// </summary>
  16. 16 private static volatile ConnectionMultiplexer _instance;
  17. 17
  18. 18 /// <summary>
  19. 19 /// 日志记录器
  20. 20 /// </summary>
  21. 21 private static readonly ILog _log = LogManager.GetLogger(typeof(StackExchangeRedisClientProvider));
  22. 22
  23. 23 private static StackExchangeDetails _stackExchangeDetails;
  24. 24
  25. 25 private static StackExchangeConfigEntry _stackExchangeConfigEntry;
  26. 26
  27. 27 #endregion
  28. 28
  29. 29 #region 私有构造函数
  30. 30
  31. 31 /// <summary>
  32. 32 /// 私有构造函数,禁止不允许通过new 来实例化该对象
  33. 33 /// </summary>
  34. 34 private StackExchangeRedisClientProvider() { }
  35. 35
  36. 36 #endregion
  37. 37
  38. 38 #region 获取Redis客户端实例
  39. 39
  40. 40 /// <summary>
  41. 41 /// 使用一个静态属性来返回已连接的实例
  42. 42 /// 实例发生变化的几种情况:
  43. 43 /// 1.实例为空
  44. 44 /// 2.连接关闭
  45. 45 /// 3.文件发生变化时
  46. 46 /// </summary>
  47. 47 /// <param name="startByConfigFile">这是一个布尔值,true表示根据配置文件的配置启动,false表示是根据配置对象启动</param>
  48. 48 /// <returns>返回ConnectionMultiplexer类型的对象实例</returns>
  49. 49 private static ConnectionMultiplexer GetInstance(bool startByConfigFile)
  50. 50 {
  51. 51 if (startByConfigFile)
  52. 52 {
  53. 53 GetRedisHosts(_stackExchangeDetails.Hosts);
  54. 54 }
  55. 55 else
  56. 56 {
  57. 57 GetRedisHosts(_stackExchangeConfigEntry.Hosts);
  58. 58 }
  59. 59
  60. 60 if (_instance == null || !_instance.IsConnected)
  61. 61 {
  62. 62 lock (lockObject)
  63. 63 {
  64. 64 if (_instance == null || !_instance.IsConnected)
  65. 65 {
  66. 66 if (startByConfigFile)
  67. 67 {
  68. 68 _instance = ConnectionMultiplexer.Connect(_stackExchangeDetails.Hosts);
  69. 69 }
  70. 70 else
  71. 71 {
  72. 72 _instance = ConnectionMultiplexer.Connect(_stackExchangeConfigEntry.Hosts);
  73. 73 }
  74. 74 }
  75. 75 }
  76. 76 }
  77. 77 _instance.ErrorMessage += MuxerErrorMessage;
  78. 78 _instance.HashSlotMoved += MuxerHashSlotMoved;
  79. 79 _instance.InternalError += MuxerInternalError;
  80. 80 _instance.ConnectionFailed += MuxerConnectionFailed;
  81. 81 _instance.ConnectionRestored += MuxerConnectionRestored;
  82. 82 _instance.ConfigurationChanged += MuxerConfigurationChanged;
  83. 83 return _instance;
  84. 84 }
  85. 85
  86. 86 /// <summary>
  87. 87 /// 解析Redis服务器列表,该列表格式IP:Port
  88. 88 /// </summary>
  89. 89 /// <param name="redisHosts">包含一个或者多个Redis服务器地址的字符串列表,以逗号做为分隔符</param>
  90. 90 private static void GetRedisHosts(string redisHosts)
  91. 91 {
  92. 92 if (string.IsNullOrWhiteSpace(redisHosts) || string.IsNullOrEmpty(redisHosts))
  93. 93 {
  94. 94 return;
  95. 95 }
  96. 96 var hosts = redisHosts.Split(',');
  97. 97 foreach (var host in hosts)
  98. 98 {
  99. 99 if (!Regex.IsMatch(host, @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]):\d{3,4}$"))
  100. 100 {
  101. 101 throw new InvalidOperationException("Redis服务器地址格式不正确!");
  102. 102 }
  103. 103 }
  104. 104 }
  105. 105
  106. 106 /// <summary>
  107. 107 /// 获取Redis客户端对象实例
  108. 108 /// </summary>
  109. 109 /// <param name="redisClientName">在配置文件中,Redis客户端的名称</param>
  110. 110 /// <param name="databaseIndex">redis逻辑分为16个数据库,排序为:0-15,我们默认使用的是0号数据库,数据库当前的索引值</param>
  111. 111 /// <returns></returns>
  112. 112 public static IDatabase GetRedisClient(string redisClientName, int databaseIndex =0)
  113. 113 {
  114. 114 //获取配置数据
  115. 115 ParameterValidityChecker.RequiredParameterStringNotNullOrWhiteSpace(redisClientName, "Redis客户端的名称不能为空!");
  116. 116 var _configurationManager = (ConfigurationFrameworkManager)ConfigurationManager.GetSection("Framework");
  117. 117 if (_configurationManager != null)
  118. 118 {
  119. 119 _stackExchangeDetails = _configurationManager.RedisClientConfiguration.GetStackExchangeDetails(redisClientName);
  120. 120 if (_stackExchangeDetails == null)
  121. 121 {
  122. 122 throw new InvalidOperationException("以StackExchange.Redis为实现技术的Redis客户端的配置有误!");
  123. 123 }
  124. 124 }
  125. 125 else
  126. 126 {
  127. 127 throw new InvalidOperationException("以StackExchange.Redis为实现技术的Redis客户端的配置有误!");
  128. 128 }
  129. 129
  130. 130 //实例化Redis客户端实例对象
  131. 131 var instance = GetInstance(true);
  132. 132 return instance.GetDatabase(databaseIndex);
  133. 133 }
  134. 134
  135. 135 /// <summary>
  136. 136 /// 获取Redis客户端对象实例
  137. 137 /// </summary>
  138. 138 /// <param name="stackExchangeConfigEntry">StackExchange配置对象</param>
  139. 139 /// <param name="databaseIndex">redis逻辑分为16个数据库,排序为:0-15,我们默认使用的是0号数据库,数据库当前的索引值</param>
  140. 140 /// <returns></returns>
  141. 141 public static IDatabase GetRedisClient(StackExchangeConfigEntry stackExchangeConfigEntry, int databaseIndex =0)
  142. 142 {
  143. 143 //获取配置数据
  144. 144 if (stackExchangeConfigEntry == null)
  145. 145 {
  146. 146 throw new ArgumentNullException("以StackExchange.Redis为实现技术的Redis客户端的配置对象不能为空!");
  147. 147 }
  148. 148 else
  149. 149 {
  150. 150 _stackExchangeConfigEntry = stackExchangeConfigEntry;
  151. 151 }
  152. 152
  153. 153 if (string.IsNullOrEmpty(_stackExchangeConfigEntry.Hosts) || string.IsNullOrWhiteSpace(_stackExchangeConfigEntry.Hosts))
  154. 154 {
  155. 155 throw new InvalidOperationException("【Hosts】必须设置其值!");
  156. 156 }
  157. 157
  158. 158 //实例化Redis客户端实例对象
  159. 159 var instance = GetInstance(false);
  160. 160 return instance.GetDatabase(databaseIndex);
  161. 161 }
  162. 162
  163. 163 #endregion
  164. 164 }


     配置实体类:StackExchangeConfigEntry.cs

  1. 1 /// <summary>
  2. 2 /// 配置文件中,以StackExchange.Redis为实现技术的配置Redis的详情
  3. 3 /// </summary>
  4. 4 public sealed class StackExchangeConfigEntry
  5. 5 {
  6. 6 #region 构造函数
  7. 7
  8. 8 /// <summary>
  9. 9 /// 给配置参数初始化默认值
  10. 10 /// </summary>
  11. 11 public StackExchangeConfigEntry()
  12. 12 {
  13. 13 Hosts = "127.0.0.1:6379";
  14. 14 Password = string.Empty;
  15. 15 }
  16. 16
  17. 17 #endregion
  18. 18
  19. 19 #region 配置属性
  20. 20
  21. 21 /// <summary>
  22. 22 /// Redis服务器地址,多个地址以逗号分隔,例如:192.168.127.11:6379,192.168.127.128:6380
  23. 23 /// </summary>
  24. 24 public string Hosts { get; set; }
  25. 25
  26. 26 /// <summary>
  27. 27 /// 登陆Redis服务器的密码
  28. 28 /// </summary>
  29. 29 public string Password { get; set; }
  30. 30
  31. 31 #endregion
  32. 32 }

 
   根据配置信息获取数据的类型:StackExchangeDetails.cs

  1. 1 /// <summary>
  2. 2 /// 配置文件中,以StackExchange.Redis为实现技术的配置Redis的详情
  3. 3 /// </summary>
  4. 4 public sealed class StackExchangeDetails
  5. 5 {
  6. 6 #region 构造函数
  7. 7
  8. 8 /// <summary>
  9. 9 /// 给配置参数初始化默认值
  10. 10 /// </summary>
  11. 11 public StackExchangeDetails()
  12. 12 {
  13. 13 Hosts = "127.0.0.1:6379";
  14. 14 Password = string.Empty;
  15. 15 }
  16. 16
  17. 17 #endregion
  18. 18
  19. 19 #region 配置属性
  20. 20
  21. 21 /// <summary>
  22. 22 /// Redis服务器的主机地址,如果多个地址则以逗号分隔,格式:127.0.0.1:6379,127.0.0.1:6380
  23. 23 /// </summary>
  24. 24 public string Hosts{ get; internal set; }
  25. 25
  26. 26 /// <summary>
  27. 27 /// 登陆Redis服务器的密码
  28. 28 /// </summary>
  29. 29 public string Password { get; internal set; }
  30. 30
  31. 31 #endregion
  32. 32 }

 

四、结束

好了,今天就写到这里了,先说明一下,这两个类暂时没有提供统一的接口,看以后的需要吧,如果有需要,我在重构。StackExchangeDetails 和 ServiceStackDetails 这两个类在这个 Enterprise.Framework.Configuration 命名空间,配置的系统暂时就不贴代码了,代码很多,其他的类型都在 Enterprise.Framework.NoSQL.RedisClient 这个命名空间下边。

天下国家,可均也;爵禄,可辞也;白刃,可蹈也;中庸不可能也

Redis进阶实践之九 独立封装的RedisClient客户端工具类(转载9)的更多相关文章

  1. Redis进阶实践之九 独立封装的RedisClient客户端工具类

    一.引言 今天开始有关Redis学习的第九篇文章了,以后肯定会大量系统使用Redis作为缓存介质,为了更好的更好的Redis,自己写了两个工具类,但是这两个工具类,没有提供一致的接口,是为了使用的独立 ...

  2. Redis进阶实践之十三 Redis的Redis-trib.rb文件详解

    一.简介     事先说明一下,本篇文章不涉及对redis-trib.rb源代码的分析,只是从使用的角度来阐述一下,对第一次使用的人来说很重要.redis-trib.rb是redis官方推出的管理re ...

  3. Redis进阶实践之十三 Redis的Redis-trib.rb脚本文件使用详解

    转载来源:http://www.cnblogs.com/PatrickLiu/p/8484784.html 一.简介 事先说明一下,本篇文章不涉及对redis-trib.rb源代码的分析,只是从使用的 ...

  4. Redis进阶实践之七Redis和Lua初步整合使用(转载 7)

    Redis进阶实践之七Redis和Lua初步整合使用 一.引言 Redis学了一段时间了,基本的东西都没问题了.从今天开始讲写一些redis和lua脚本的相关的东西,lua这个脚本是一个好东西,可以运 ...

  5. Redis进阶实践之五Redis的高级特性(转载 5)

    Redis进阶实践之五Redis的高级特性 一.引言 上一篇文章写了Redis的特征,使用场景,同时也介绍了Redis的基本数据类型,redis的数据类型是操作redis的基础,这个必须好好的掌握.今 ...

  6. Redis进阶实践之三如何在Windows系统上安装安装Redis(转载)

    Redis进阶实践之三如何在Windows系统上安装安装Redis 一.Redis的简介 Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括 ...

  7. Redis进阶实践之二如何在Linux系统上安装安装Redis(转载)(2)

    Redis进阶实践之二如何在Linux系统上安装安装Redis 一.引言 上一篇文章写了“如何安装VMware Pro虚拟机”和在虚拟机上安装Linux操作系统.那是第一步,有了Linux操作系统,我 ...

  8. Redis进阶实践之十六 Redis大批量增加数据

    一.介绍      有时,Redis实例需要在很短的时间内加载大量先前存在或用户生成的数据,以便尽可能快地创建数百万个键.这就是所谓的批量插入,本文档的目标是提供有关如何以尽可能快的速度向Redis提 ...

  9. Redis进阶实践之十八 使用管道模式加速Redis查询

    一.引言             学习redis 也有一段时间了,该接触的也差不多了.后来有一天,以为同事问我,如何向redis中批量的增加数据,肯定是大批量的,为了这主题,我从新找起了解决方案.目前 ...

随机推荐

  1. UE4 Sockets多线程TCP通信

    转自:https://blog.csdn.net/zilisen/article/details/75007447 一.简介 UE4引擎是提供了Sockets模块和Networking模块的,博主在研 ...

  2. 第7章 网络层协议(2)_ICMP协议

    2. ICMP协议 2.1 ICMP报文(Internet Control Message Protocol)的类型 报文类型 类型值 代码 描述 请求报文 8 0 请求回显报文 响应报文 0 0 回 ...

  3. 电商系统架构总结1(EF)

    最近主导了一个电商系统的设计开发过程,包括前期分析设计,框架搭建,功能模块的具体开发(主要负责在线支付部分),成功上线后的部署维护,运维策略等等全过程. 虽然这个系统不是什么超大型的电商系统 数亿计的 ...

  4. JSP:注册&登录

    数据库:Mysql 除了_id自动增长,其余全是varchar 注册:register.jsp <%@ page language="java" import="j ...

  5. TableStore:多行数据操作

    1.批量写 public static void batchWriteRow(SyncClient client) { BatchWriteRowRequest request = new Batch ...

  6. js语法规则 ---console.log ---- prompt ----基本类型 ---parseInt

    在页面中可以在body里面加入type=”text/javascript” 例如: <script type="text/javascript"> </scrip ...

  7. day6--递归函数

    一递归函数 我们老师经常喜欢讲的一句话就是:人理解函数,神理解递归,那么什么是递归函数? 递归函数:在一个函数里面调用函数本身,也就是说这个函数里面出现了和函数一样的名字 例如: def func(n ...

  8. git代理设置

    git config --global http.proxy http://127.0.0.1:1080git config --global https.proxy https://127.0.0. ...

  9. Java Base64 加密/解密

    Base64常用来表示字串加密过后的内容,使用Java 程式语言来实作Base64的编码与解码功能 1.在Java上做Base64的编码与解码,会使用到JDK里sun.misc套件下的BASE64En ...

  10. vmware使用vsphere的镜像

    vsphere镜像导出后可以使用vmware station打开, vsphere镜像导出时需要关机,否则会提示失败,有文件不能导出.