16
16
17
17
namespace ServiceStack . Redis
18
18
{
19
- /// <summary>
20
- /// Provides thread-safe retrievel of redis clients since each client is a new one.
21
- /// Allows the configuration of different ReadWrite and ReadOnly hosts
22
- /// </summary>
23
- public partial class BasicRedisClientManager
19
+ /// <summary>
20
+ /// Provides thread-safe retrievel of redis clients since each client is a new one.
21
+ /// Allows the configuration of different ReadWrite and ReadOnly hosts
22
+ /// </summary>
23
+ public partial class BasicRedisClientManager
24
24
: IRedisClientsManager , IRedisFailover
25
- {
26
- private List < RedisEndPoint > ReadWriteHosts { get ; set ; }
27
- private List < RedisEndPoint > ReadOnlyHosts { get ; set ; }
25
+ {
26
+ private List < RedisEndPoint > ReadWriteHosts { get ; set ; }
27
+ private List < RedisEndPoint > ReadOnlyHosts { get ; set ; }
28
28
public int ? ConnectTimeout { get ; set ; }
29
29
30
30
/// <summary>
31
31
/// Gets or sets object key prefix.
32
32
/// </summary>
33
33
public string NamespacePrefix { get ; set ; }
34
- private int readWriteHostsIndex ;
35
- private int readOnlyHostsIndex ;
34
+ private int readWriteHostsIndex ;
35
+ private int readOnlyHostsIndex ;
36
36
37
- public IRedisClientFactory RedisClientFactory { get ; set ; }
37
+ public IRedisClientFactory RedisClientFactory { get ; set ; }
38
38
39
- public long Db { get ; private set ; }
39
+ public long Db { get ; private set ; }
40
40
41
41
public Action < IRedisNativeClient > ConnectionFilter { get ; set ; }
42
42
43
43
public List < Action < IRedisClientsManager > > OnFailover { get ; private set ; }
44
44
45
- public BasicRedisClientManager ( ) : this ( RedisNativeClient . DefaultHost ) { }
46
-
47
- public BasicRedisClientManager ( params string [ ] readWriteHosts )
48
- : this ( readWriteHosts , readWriteHosts ) { }
49
-
50
- public BasicRedisClientManager ( int initialDb , params string [ ] readWriteHosts )
51
- : this ( readWriteHosts , readWriteHosts , initialDb ) { }
52
-
53
- /// <summary>
54
- /// Hosts can be an IP Address or Hostname in the format: host[:port]
55
- /// e.g. 127.0.0.1:6379
56
- /// default is: localhost:6379
57
- /// </summary>
58
- /// <param name="readWriteHosts">The write hosts.</param>
59
- /// <param name="readOnlyHosts">The read hosts.</param>
60
- public BasicRedisClientManager (
61
- IEnumerable < string > readWriteHosts ,
62
- IEnumerable < string > readOnlyHosts )
63
- : this ( readWriteHosts , readOnlyHosts , RedisNativeClient . DefaultDb )
64
- {
65
- }
66
-
67
- public BasicRedisClientManager (
68
- IEnumerable < string > readWriteHosts ,
69
- IEnumerable < string > readOnlyHosts ,
70
- long initalDb )
71
- {
72
- this . Db = initalDb ;
73
-
74
- ReadWriteHosts = readWriteHosts . ToRedisEndPoints ( ) ;
75
- ReadOnlyHosts = readOnlyHosts . ToRedisEndPoints ( ) ;
76
-
77
- this . RedisClientFactory = Redis . RedisClientFactory . Instance ;
78
-
79
- this . OnStart ( ) ;
80
- }
81
-
82
- protected virtual void OnStart ( )
83
- {
84
- this . Start ( ) ;
85
- }
86
-
87
- /// <summary>
88
- /// Returns a Read/Write client (The default) using the hosts defined in ReadWriteHosts
89
- /// </summary>
90
- /// <returns></returns>
91
- public IRedisClient GetClient ( )
92
- {
93
- var nextHost = ReadWriteHosts [ readWriteHostsIndex ++ % ReadWriteHosts . Count ] ;
94
- var client = RedisClientFactory . CreateRedisClient (
95
- nextHost . Host , nextHost . Port ) ;
45
+ public BasicRedisClientManager ( ) : this ( RedisNativeClient . DefaultHost ) { }
46
+
47
+ public BasicRedisClientManager ( params string [ ] readWriteHosts )
48
+ : this ( readWriteHosts , readWriteHosts ) { }
49
+
50
+ public BasicRedisClientManager ( int initialDb , params string [ ] readWriteHosts )
51
+ : this ( readWriteHosts , readWriteHosts , initialDb ) { }
52
+
53
+ /// <summary>
54
+ /// Hosts can be an IP Address or Hostname in the format: host[:port]
55
+ /// e.g. 127.0.0.1:6379
56
+ /// default is: localhost:6379
57
+ /// </summary>
58
+ /// <param name="readWriteHosts">The write hosts.</param>
59
+ /// <param name="readOnlyHosts">The read hosts.</param>
60
+ public BasicRedisClientManager (
61
+ IEnumerable < string > readWriteHosts ,
62
+ IEnumerable < string > readOnlyHosts )
63
+ : this ( readWriteHosts , readOnlyHosts , RedisNativeClient . DefaultDb )
64
+ {
65
+ }
66
+
67
+ public BasicRedisClientManager (
68
+ IEnumerable < string > readWriteHosts ,
69
+ IEnumerable < string > readOnlyHosts ,
70
+ long initalDb )
71
+ {
72
+ this . Db = initalDb ;
73
+
74
+ ReadWriteHosts = readWriteHosts . ToRedisEndPoints ( ) ;
75
+ ReadOnlyHosts = readOnlyHosts . ToRedisEndPoints ( ) ;
76
+
77
+ this . OnFailover = new List < Action < IRedisClientsManager > > ( ) ;
78
+ this . RedisClientFactory = Redis . RedisClientFactory . Instance ;
79
+
80
+ this . OnStart ( ) ;
81
+ }
82
+
83
+ protected virtual void OnStart ( )
84
+ {
85
+ this . Start ( ) ;
86
+ }
87
+
88
+ /// <summary>
89
+ /// Returns a Read/Write client (The default) using the hosts defined in ReadWriteHosts
90
+ /// </summary>
91
+ /// <returns></returns>
92
+ public IRedisClient GetClient ( )
93
+ {
94
+ var nextHost = ReadWriteHosts [ readWriteHostsIndex ++ % ReadWriteHosts . Count ] ;
95
+ var client = RedisClientFactory . CreateRedisClient (
96
+ nextHost . Host , nextHost . Port ) ;
96
97
97
98
if ( this . ConnectTimeout != null )
98
99
{
99
100
client . ConnectTimeout = this . ConnectTimeout . Value ;
100
101
}
101
102
102
- //Set database to userSpecified if different
103
- if ( Db != RedisNativeClient . DefaultDb )
104
- {
105
- client . ChangeDb ( Db ) ;
106
- }
103
+ //Set database to userSpecified if different
104
+ if ( Db != RedisNativeClient . DefaultDb )
105
+ {
106
+ client . ChangeDb ( Db ) ;
107
+ }
107
108
108
109
if ( nextHost . RequiresAuth )
109
110
client . Password = nextHost . Password ;
110
111
111
112
client . NamespacePrefix = NamespacePrefix ;
112
113
client . ConnectionFilter = ConnectionFilter ;
113
114
114
- return client ;
115
- }
115
+ return client ;
116
+ }
116
117
117
- /// <summary>
118
- /// Returns a ReadOnly client using the hosts defined in ReadOnlyHosts.
119
- /// </summary>
120
- /// <returns></returns>
121
- public virtual IRedisClient GetReadOnlyClient ( )
122
- {
123
- var nextHost = ReadOnlyHosts [ readOnlyHostsIndex ++ % ReadOnlyHosts . Count ] ;
124
- var client = RedisClientFactory . CreateRedisClient (
125
- nextHost . Host , nextHost . Port ) ;
118
+ /// <summary>
119
+ /// Returns a ReadOnly client using the hosts defined in ReadOnlyHosts.
120
+ /// </summary>
121
+ /// <returns></returns>
122
+ public virtual IRedisClient GetReadOnlyClient ( )
123
+ {
124
+ var nextHost = ReadOnlyHosts [ readOnlyHostsIndex ++ % ReadOnlyHosts . Count ] ;
125
+ var client = RedisClientFactory . CreateRedisClient (
126
+ nextHost . Host , nextHost . Port ) ;
126
127
127
128
if ( this . ConnectTimeout != null )
128
129
{
129
130
client . ConnectTimeout = this . ConnectTimeout . Value ;
130
131
}
131
132
132
- //Set database to userSpecified if different
133
- if ( Db != RedisNativeClient . DefaultDb )
134
- {
135
- client . ChangeDb ( Db ) ;
136
- }
133
+ //Set database to userSpecified if different
134
+ if ( Db != RedisNativeClient . DefaultDb )
135
+ {
136
+ client . ChangeDb ( Db ) ;
137
+ }
137
138
138
139
if ( nextHost . RequiresAuth )
139
140
client . Password = nextHost . Password ;
@@ -142,21 +143,21 @@ public virtual IRedisClient GetReadOnlyClient()
142
143
client . ConnectionFilter = ConnectionFilter ;
143
144
144
145
return client ;
145
- }
146
-
147
- public void SetAll < T > ( IDictionary < string , T > values )
148
- {
149
- foreach ( var entry in values )
150
- {
151
- Set ( entry . Key , entry . Value ) ;
152
- }
153
- }
154
-
155
- public void Start ( )
156
- {
157
- readWriteHostsIndex = 0 ;
158
- readOnlyHostsIndex = 0 ;
159
- }
146
+ }
147
+
148
+ public void SetAll < T > ( IDictionary < string , T > values )
149
+ {
150
+ foreach ( var entry in values )
151
+ {
152
+ Set ( entry . Key , entry . Value ) ;
153
+ }
154
+ }
155
+
156
+ public void Start ( )
157
+ {
158
+ readWriteHostsIndex = 0 ;
159
+ readOnlyHostsIndex = 0 ;
160
+ }
160
161
161
162
public void FailoverTo ( params string [ ] readWriteHosts )
162
163
{
@@ -171,8 +172,8 @@ public void FailoverTo(IEnumerable<string> readWriteHosts, IEnumerable<string> r
171
172
Start ( ) ;
172
173
}
173
174
174
- public void Dispose ( )
175
- {
176
- }
177
- }
175
+ public void Dispose ( )
176
+ {
177
+ }
178
+ }
178
179
}
0 commit comments