Redis進階實踐之九 獨立封裝的RedisClient客戶端工具類

Redis進階實踐之九 獨立封裝的RedisClient客戶端工具類html

1、引言git

        今天開始有關Redis學習的第九篇文章了,之後確定會大量系統使用Redis做爲緩存介質,爲了更好的更好的Redis,本身寫了兩個工具類,可是這兩個工具類,沒有提供一致的接口,是爲了使用的獨立性。測試已經完畢,能夠正常訪問Windows和Linux版本上的Redis服務,各類操做也沒問題。今天就把主要代碼貼出來,由於有一部分是須要配置文件的,我本身獨立寫了一套配置系統,這套配置系統就不貼出來,你們能夠根據本身的理解來寫這個部分的內容,好了,開始咱們今天的寫做吧。github

2、簡介web

        我寫了兩個工具類,第一個是以【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版本已經逐漸商業化了,目的能夠想而知,嗨,都是錢惹的禍。redis

       上面我說道了,【ServiceStack.Redis】後來就商業化了,我想了想光靠他也是不行了,還要在寫一個版本的工具類,因此就選擇了【StackExchange.Redis】,【StackExchange.Redis】也是開源的,到目前,一直都是開源的,它的地址是:https://github.com/StackExchange/StackExchange.Redis,我使用的是.net 4.5,工具採用 vs2015, StackExchange.Redis版本是1.0.488。工具類還會在持續的使用過程當中,我還會更新和修改的。數據庫

3、實例代碼
 
         這是Redis客戶端的配置文件的格式,格式很簡單,能夠分開配置,也能夠合在一塊兒配置。代碼中標紅的是和個人配置系統有關的代碼,你們請注意。緩存

複製代碼

<configuration>
  <configSections>
    <section name="Framework" type="Enterprise.Framework.Configuration.ConfigurationFrameworkSectionHandler,Enterprise.Framework.Configuration" />
  </configSections>

  <Framework type="Enterprise.Framework.Configuration.ConfigurationFrameworkManager,Enterprise.Framework.Configuration">
    <Enterprise.Framework.NoSQL>
      <RedisClients>
          <RedisClient name="ServiceStack" technology="ServiceStack.Redis">
            <readWriteHosts>192.168.3.11:6379,192.168.3.11:6380</readWriteHosts>
            <!--<readOnlyHosts>能夠省略該項</readOnlyHosts>-->
            <!--<maxWritePoolSize>能夠省略該項</maxWritePoolSize>-->
            <!--<maxReadPoolSize>能夠省略該項</maxReadPoolSize>-->
            <!--<password>能夠省略該項</password>-->
            <!--<autoStart>能夠省略該項</autoStart>-->
          </RedisClient>
          <RedisClient name="StackExchange" technology="StackExchange.Redis">
            <host>192.168.131.1:6379</host>
            <!--<password>能夠省略該項</password>-->
          </RedisClient>
      </RedisClients>
    </Enterprise.Framework.NoSQL>
  </Framework>
</configuration>

複製代碼

 

        一、這是以【ServiceStack.Redis】爲實現技術的工具類,對外界的訪問接口提供了2個,第一個是以配置文件中自定義的名稱參數的,紅色代碼是和我獨立的配置系統相關聯的,另外一個訪問接口是以配置實體類參數的,代碼很簡單,很少說了。
 服務器

      工具類:ServiceStackRedisClientProvider.csapp

複製代碼

1 /// <summary>
  2     /// 經過ServiceStack.Redis實現的Redis的客戶端操做類型
  3     /// </summary>
  4     public sealed class ServiceStackRedisClientProvider
  5     {
  6         #region 私有變量
  7 
  8         //線程同步變量
  9         private static readonly object lockObject = new object();
 10 
 11         //redis連接池管理對象
 12         private static volatile PooledRedisClientManager _instance = null;
 13 
 14         //配置文件裏面的ServiceStack詳細配置設置
 15         private static ServiceStackDetails _serviceStackDetails;
 16 
 17         //能夠自行配置ServiceStack的配置對象
 18         private static ServiceStackConfigEntry _serviceStackConfigEntry;
 19 
 20         #endregion
 21 
 22         #region 私有構造函數
 23 
 24         /// <summary>
 25         /// 私有構造函數,禁止外部經過new關鍵字來建立該對象實例
 26         /// </summary>
 27         private ServiceStackRedisClientProvider() { }
 28 
 29         #endregion
 30 
 31         #region 獲取PooledRedisClientManager實例的方法
 32 
 33         /// <summary>
 34         /// 獲取redis連接池管理對象實例
 35         /// 實例發生變化的集中狀況:
 36         /// 1.實例爲空
 37         /// 2.配置文件發生變化
 38         /// </summary>
 39         /// <param name="startByConfigFile">這是一個布爾值,true表示根據配置文件的配置啓動,false表示是根據配置對象啓動</param>
 40         /// <returns>返回PooledRedisClientManager類型的對象實例</returns>
 41         private static PooledRedisClientManager GetInstance(bool startByConfigFile)
 42         {
 43             if (_instance == null)
 44             {
 45                 lock (lockObject)
 46                 {
 47                     if (_instance == null)
 48                     {
 49                         string[] readWriteServerList=null;
 50                         string[] readOnlyServerList=null;
 51                         RedisClientManagerConfig managerConfig=null;
 52 
 53                         //根據咱們配置文件中數據來設置啓動信息(app.config或者web.config)
 54                         if (startByConfigFile && (_serviceStackDetails != null))
 55                         {
 56                             managerConfig = new RedisClientManagerConfig()
 57                             {
 58                                 AutoStart = _serviceStackDetails.AutoStart,
 59                                 MaxReadPoolSize = _serviceStackDetails.MaxReadPoolSize,
 60                                 MaxWritePoolSize = _serviceStackDetails.MaxWritePoolSize,
 61                             };
 62 
 63                             readWriteServerList = GetRedisHosts(_serviceStackDetails.ReadWriteHosts);
 64                             readOnlyServerList = GetRedisHosts(_serviceStackDetails.ReadOnlyHosts);
 65                         }
 66                         else if (!startByConfigFile && (_serviceStackConfigEntry != null))//根據配置對象來設置啓動信息(ServiceStackConfigEntry)
 67                         {
 68                             managerConfig = new RedisClientManagerConfig()
 69                             {
 70                                 AutoStart = _serviceStackConfigEntry.AutoStart,
 71                                 MaxReadPoolSize = _serviceStackConfigEntry.MaxReadPoolSize,
 72                                 MaxWritePoolSize = _serviceStackConfigEntry.MaxWritePoolSize,
 73                             };
 74 
 75                             readWriteServerList = GetRedisHosts(_serviceStackConfigEntry.ReadWriteHosts);
 76                             readOnlyServerList = GetRedisHosts(_serviceStackConfigEntry.ReadOnlyHosts);
 77                         }
 78                         else
 79                         {
 80                             throw new InvalidOperationException("Redis客戶端初始化配置失敗!");
 81                         }
 82 
 83                         if ((readWriteServerList != null && readWriteServerList.Length > 0)&&(readOnlyServerList != null && readOnlyServerList.Length <= 0))
 84                         {
 85                             _instance = new PooledRedisClientManager(readWriteServerList);
 86                         }
 87 
 88                         if ((readWriteServerList != null && readWriteServerList.Length > 0) && (readOnlyServerList != null && readOnlyServerList.Length > 0))
 89                         {
 90                             _instance = new PooledRedisClientManager(readWriteServerList, readOnlyServerList, managerConfig);
 91                         }
 92                     }
 93                 }
 94             }
 95             return _instance;
 96         }
 97 
 98         /// <summary>
 99         /// 解析Redis服務器列表,該列表格式IP[:Port]
100         /// </summary>
101         /// <param name="redisHosts">包含一個或者多個Redis服務器地址的字符串列表,以逗號作爲分隔符</param>
102         /// <returns>返回Redis服務器地址列表</returns>
103         private static string[] GetRedisHosts(string redisHosts)
104         {
105             if (string.IsNullOrWhiteSpace(redisHosts) || string.IsNullOrEmpty(redisHosts))
106             {
107                 return new string[] { };
108             }
109             var hosts=redisHosts.Split(',');
110             foreach (var host in hosts)
111             {
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                 {
114                     throw new InvalidOperationException("Redis服務器地址格式不正確!");
115                 }
116             }
117             return hosts;
118         }
119 
120         #endregion
121 
122         #region 提供對外訪問接口
123 
124         /// <summary>
125         /// 獲取Redis客戶端對象實例
126         /// </summary>
127         /// <param name="redisClientName">在配置文件中,Redis客戶端的名稱</param>
128         /// <param name="databaseIndex">redis邏輯分爲16個數據庫,排序爲:0-15,咱們默認使用的是0號數據庫,數據庫當前的索引值</param>
129         /// <returns>返回IRedisClient對象實例</returns>
130         public static IRedisClient GetRedisClient(string redisClientName,int databaseIndex = 0)
131         {
132             //獲取配置數據
133             ParameterValidityChecker.RequiredParameterStringNotNullOrWhiteSpace(redisClientName, "Redis客戶端的名稱不能爲空!");
134             var _configurationManager = (ConfigurationFrameworkManager)ConfigurationManager.GetSection("Framework");
135             if (_configurationManager != null)
136             {
137                 _serviceStackDetails = _configurationManager.RedisClientConfiguration.GetServiceStackDetails(redisClientName);
138                 if (_serviceStackDetails == null)
139                 {
140                     throw new InvalidOperationException("以ServiceStack.Redis爲實現技術的Redis客戶端的配置有誤!");
141                 }
142             }
143             else
144             {
145                 throw new InvalidOperationException("以ServiceStack.Redis爲實現技術的Redis客戶端的配置有誤!");
146             }
147 
148             //實例化Redis客戶端實例對象
149             var pooledRedisClientManager = GetInstance(true);
150             var redisClient = pooledRedisClientManager.GetClient();
151             if (!string.IsNullOrEmpty(_serviceStackDetails.Password))
152             {
153                 redisClient.Password = _serviceStackDetails.Password;
154             }
155             redisClient.Db = databaseIndex;
156             return redisClient;
157         }
158 
159         /// <summary>
160         /// 獲取Redis客戶端對象實例
161         /// </summary>
162         /// <param name="serviceStackConfigEntry">在配置文件中,Redis客戶端的名稱</param>
163         /// <param name="databaseIndex">redis邏輯分爲16個數據庫,排序爲:0-15,咱們默認使用的是0號數據庫,數據庫當前的索引值</param>
164         /// <returns>返回IRedisClient對象實例</returns>
165         public static IRedisClient GetRedisClient(ServiceStackConfigEntry serviceStackConfigEntry, int databaseIndex = 0)
166         {
167             //獲取配置數據
168             if (serviceStackConfigEntry == null)
169             {
170                 throw new ArgumentNullException("以ServiceStack.Redis爲實現技術的Redis客戶端的配置對象不能爲空!");
171             }
172             else
173             {
174                 _serviceStackConfigEntry = serviceStackConfigEntry;
175             }
176 
177             if (string.IsNullOrEmpty(_serviceStackConfigEntry.ReadWriteHosts) || string.IsNullOrWhiteSpace(_serviceStackConfigEntry.ReadWriteHosts))
178             {
179                 throw new InvalidOperationException("【ReadWriteHosts】必須設置其值!");
180             }
181 
182             //實例化Redis客戶端實例對象
183             var pooledRedisClientManager = GetInstance(false);
184             var redisClient = pooledRedisClientManager.GetClient();
185             if (!string.IsNullOrEmpty(_serviceStackConfigEntry.Password)&&!string.IsNullOrWhiteSpace(_serviceStackConfigEntry.Password))
186             {
187                 redisClient.Password = _serviceStackConfigEntry.Password;
188             }
189             redisClient.Db = databaseIndex;
190             return redisClient;
191         }
192 
193         #endregion
194     }

複製代碼

 

     配置實體類 ServiceStackConfigEntry.cs的代碼:
 ide

複製代碼

1 /// <summary>
 2     ///  配置文件中,以ServiceStack.Redis爲實現技術的配置Redis的詳情
 3     /// </summary>
 4     public sealed class ServiceStackConfigEntry
 5     {
 6         #region 構造函數
 7 
 8         /// <summary>
 9         /// 給配置參數初始化默認值
10         /// </summary>
11         public ServiceStackConfigEntry()
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; set; }
29 
30         /// <summary>
31         /// 只能讀的Redis服務器地址,多個地址以逗號分隔,例如:192.168.127.11:6379,192.168.127.128:6380
32         /// </summary>
33         public string ReadOnlyHosts { get; set; }
34 
35         /// <summary>
36         /// 最大寫連接數
37         /// </summary>
38         public int MaxWritePoolSize { get; set; }
39 
40         /// <summary>
41         /// 最大讀連接數
42         /// </summary>
43         public int MaxReadPoolSize { get; set; }
44 
45         /// <summary>
46         /// 登錄Redis服務器的密碼
47         /// </summary>
48         public string Password { get; set; }
49 
50         /// <summary>
51         /// 是否自動啓動
52         /// </summary>
53         public bool AutoStart { get; set; }
54 
55         #endregion
56     }

複製代碼

 

      獲取配置文件詳情的類型:ServiceStackDetails.cs

    

複製代碼

/// <summary>
    ///  配置文件中,以ServiceStack.Redis爲實現技術的配置Redis的詳情
    /// </summary>
    public sealed class ServiceStackDetails
    {
        #region 構造函數

        /// <summary>
        /// 給配置參數初始化默認值
        /// </summary>
        public ServiceStackDetails()
        {
            ReadWriteHosts = "127.0.0.1:6379";
            ReadOnlyHosts = string.Empty;
            MaxWritePoolSize = 200;
            MaxReadPoolSize = 200;
            Password = string.Empty;
            AutoStart = true;
        }

        #endregion

        #region 配置屬性

        /// <summary>
        /// 可讀可寫的Redis服務器地址,多個地址以逗號分隔,例如:192.168.127.11:6379,192.168.127.128:6380
        /// </summary>
        public string ReadWriteHosts { get; internal set; }

        /// <summary>
        /// 只能讀的Redis服務器地址,多個地址以逗號分隔,例如:192.168.127.11:6379,192.168.127.128:6380
        /// </summary>
        public string ReadOnlyHosts { get; internal set; }

        /// <summary>
        /// 最大寫連接數
        /// </summary>
        public int MaxWritePoolSize { get; internal set; }

        /// <summary>
        /// 最大讀連接數
        /// </summary>
        public int MaxReadPoolSize { get; internal set; }

        /// <summary>
        /// 登錄Redis服務器的密碼
        /// </summary>
        public string Password { get; internal set; }

        /// <summary>
        /// 是否自動啓動
        /// </summary>
        public bool AutoStart { get; internal set; }

        #endregion
    }

複製代碼

 

          二、這是以【StackExchange.Redis】爲實現技術的工具類,對外界的訪問接口提供了2個,第一個是以配置文件中自定義的名稱參數的,紅色代碼是和我獨立的配置系統相關聯的,另外一個訪問接口是以配置實體類參數的,代碼很簡單,很少說了。

 

       工具類: StackExchangeRedisClientProvider.cs

 

複製代碼

1 /// <summary>
  2     /// 經過StackExchange.Redis實現的Redis的客戶端操做類型
  3     /// </summary>
  4     public sealed class StackExchangeRedisClientProvider
  5     {
  6         #region 私有字段
  7 
  8         /// <summary>
  9         /// 線程同步變量
 10         /// </summary>
 11         private static readonly object lockObject = new object();
 12 
 13         /// <summary>
 14         /// redis連接池管理對象
 15         /// </summary>
 16         private static volatile ConnectionMultiplexer _instance;
 17 
 18         /// <summary>
 19         /// 日誌記錄器
 20         /// </summary>
 21         private static readonly ILog _log = LogManager.GetLogger(typeof(StackExchangeRedisClientProvider));
 22 
 23         private static StackExchangeDetails _stackExchangeDetails;
 24 
 25         private static StackExchangeConfigEntry _stackExchangeConfigEntry;
 26 
 27         #endregion
 28 
 29         #region 私有構造函數
 30 
 31         /// <summary>
 32         /// 私有構造函數,禁止不容許經過new 來實例化該對象
 33         /// </summary>
 34         private StackExchangeRedisClientProvider() { }
 35 
 36         #endregion
 37 
 38         #region 獲取Redis客戶端實例
 39 
 40         /// <summary>
 41         /// 使用一個靜態屬性來返回已鏈接的實例
 42         /// 實例發生變化的幾種狀況:
 43         /// 1.實例爲空
 44         /// 2.鏈接關閉
 45         /// 3.文件發生變化時
 46         /// </summary>
 47         /// <param name="startByConfigFile">這是一個布爾值,true表示根據配置文件的配置啓動,false表示是根據配置對象啓動</param>
 48         /// <returns>返回ConnectionMultiplexer類型的對象實例</returns>
 49         private static ConnectionMultiplexer GetInstance(bool startByConfigFile)
 50         {
 51             if (startByConfigFile)
 52             {
 53                 GetRedisHosts(_stackExchangeDetails.Hosts);
 54             }
 55             else
 56             {
 57                 GetRedisHosts(_stackExchangeConfigEntry.Hosts);
 58             }
 59             
 60             if (_instance == null || !_instance.IsConnected)
 61             {
 62                 lock (lockObject)
 63                 {
 64                     if (_instance == null || !_instance.IsConnected)
 65                     {
 66                         if (startByConfigFile)
 67                         {
 68                             _instance = ConnectionMultiplexer.Connect(_stackExchangeDetails.Hosts);
 69                         }
 70                         else
 71                         {
 72                             _instance = ConnectionMultiplexer.Connect(_stackExchangeConfigEntry.Hosts);
 73                         }
 74                     }
 75                 }
 76             }
 77             _instance.ErrorMessage += MuxerErrorMessage;
 78             _instance.HashSlotMoved += MuxerHashSlotMoved;
 79             _instance.InternalError += MuxerInternalError;
 80             _instance.ConnectionFailed += MuxerConnectionFailed;
 81             _instance.ConnectionRestored += MuxerConnectionRestored;
 82             _instance.ConfigurationChanged += MuxerConfigurationChanged;
 83             return _instance;
 84         }
 85 
 86         /// <summary>
 87         /// 解析Redis服務器列表,該列表格式IP:Port
 88         /// </summary>
 89         /// <param name="redisHosts">包含一個或者多個Redis服務器地址的字符串列表,以逗號作爲分隔符</param>
 90         private static void GetRedisHosts(string redisHosts)
 91         {
 92             if (string.IsNullOrWhiteSpace(redisHosts) || string.IsNullOrEmpty(redisHosts))
 93             {
 94                 return;
 95             }
 96             var hosts = redisHosts.Split(',');
 97             foreach (var host in hosts)
 98             {
 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                 {
101                     throw new InvalidOperationException("Redis服務器地址格式不正確!");
102                 }
103             }
104         }
105 
106         /// <summary>
107         /// 獲取Redis客戶端對象實例
108         /// </summary>
109         /// <param name="redisClientName">在配置文件中,Redis客戶端的名稱</param>
110         /// <param name="databaseIndex">redis邏輯分爲16個數據庫,排序爲:0-15,咱們默認使用的是0號數據庫,數據庫當前的索引值</param>
111         /// <returns></returns>
112         public static IDatabase GetRedisClient(string redisClientName, int databaseIndex =0)
113         {
114             //獲取配置數據
115             ParameterValidityChecker.RequiredParameterStringNotNullOrWhiteSpace(redisClientName, "Redis客戶端的名稱不能爲空!");
116             var _configurationManager = (ConfigurationFrameworkManager)ConfigurationManager.GetSection("Framework");
117             if (_configurationManager != null)
118             {
119                 _stackExchangeDetails = _configurationManager.RedisClientConfiguration.GetStackExchangeDetails(redisClientName);
120                 if (_stackExchangeDetails == null)
121                 {
122                     throw new InvalidOperationException("以StackExchange.Redis爲實現技術的Redis客戶端的配置有誤!");
123                 }
124             }
125             else
126             {
127                 throw new InvalidOperationException("以StackExchange.Redis爲實現技術的Redis客戶端的配置有誤!");
128             }
129 
130             //實例化Redis客戶端實例對象
131             var instance = GetInstance(true);
132             return instance.GetDatabase(databaseIndex);
133         }
134 
135         /// <summary>
136         /// 獲取Redis客戶端對象實例
137         /// </summary>
138         /// <param name="stackExchangeConfigEntry">StackExchange配置對象</param>
139         /// <param name="databaseIndex">redis邏輯分爲16個數據庫,排序爲:0-15,咱們默認使用的是0號數據庫,數據庫當前的索引值</param>
140         /// <returns></returns>
141         public static IDatabase GetRedisClient(StackExchangeConfigEntry stackExchangeConfigEntry, int databaseIndex =0)
142         {
143             //獲取配置數據
144             if (stackExchangeConfigEntry == null)
145             {
146                 throw new ArgumentNullException("以StackExchange.Redis爲實現技術的Redis客戶端的配置對象不能爲空!");
147             }
148             else
149             {
150                 _stackExchangeConfigEntry = stackExchangeConfigEntry;
151             }
152 
153             if (string.IsNullOrEmpty(_stackExchangeConfigEntry.Hosts) || string.IsNullOrWhiteSpace(_stackExchangeConfigEntry.Hosts))
154             {
155                 throw new InvalidOperationException("【Hosts】必須設置其值!");
156             }
157 
158             //實例化Redis客戶端實例對象
159             var instance = GetInstance(false);
160             return instance.GetDatabase(databaseIndex);
161         }
162 
163         #endregion
164     }

複製代碼


     配置實體類:StackExchangeConfigEntry.cs

複製代碼

1 /// <summary>
 2     ///  配置文件中,以StackExchange.Redis爲實現技術的配置Redis的詳情
 3     /// </summary>
 4     public sealed class StackExchangeConfigEntry
 5     {
 6         #region 構造函數
 7 
 8         /// <summary>
 9         /// 給配置參數初始化默認值
10         /// </summary>
11         public StackExchangeConfigEntry()
12         {
13             Hosts = "127.0.0.1:6379";
14             Password = string.Empty;
15         }
16 
17         #endregion
18 
19         #region 配置屬性
20 
21         /// <summary>
22         /// Redis服務器地址,多個地址以逗號分隔,例如:192.168.127.11:6379,192.168.127.128:6380
23         /// </summary>
24         public string Hosts { get; set; }
25 
26         /// <summary>
27         /// 登錄Redis服務器的密碼
28         /// </summary>
29         public string Password { get; set; }
30 
31         #endregion
32     }

複製代碼

 
   根據配置信息獲取數據的類型:StackExchangeDetails.cs

 

複製代碼

1 /// <summary>
 2     ///  配置文件中,以StackExchange.Redis爲實現技術的配置Redis的詳情
 3     /// </summary>
 4     public sealed class StackExchangeDetails
 5     {
 6         #region 構造函數
 7 
 8         /// <summary>
 9         /// 給配置參數初始化默認值
10         /// </summary>
11         public StackExchangeDetails()
12         {
13             Hosts = "127.0.0.1:6379";
14             Password = string.Empty;
15         }
16 
17         #endregion
18 
19         #region 配置屬性
20 
21         /// <summary>
22         /// Redis服務器的主機地址,若是多個地址則以逗號分隔,格式:127.0.0.1:6379,127.0.0.1:6380
23         /// </summary>
24         public string Hosts{ get; internal set; }
25 
26         /// <summary>
27         /// 登錄Redis服務器的密碼
28         /// </summary>
29         public string Password { get; internal set; }
30 
31         #endregion
32     }

複製代碼

 

4、結束

       好了,今天就寫到這裏了,先說明一下,這兩個類暫時沒有提供統一的接口,看之後的須要吧,若是有須要,我在重構。StackExchangeDetails 和 ServiceStackDetails 這兩個類在這個 Enterprise.Framework.Configuration 命名空間,配置的系統暫時就不貼代碼了,代碼不少,其餘的類型都在 Enterprise.Framework.NoSQL.RedisClient 這個命名空間下邊。

   

天下國家,可均也;爵祿,可辭也;白刃,可蹈也;中庸不可能也

相關文章
相關標籤/搜索