Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 9a94fcc

Browse files
committed
Init OnFailover collection, clean tabs.
1 parent b45479b commit 9a94fcc

File tree

1 file changed

+104
-103
lines changed

1 file changed

+104
-103
lines changed

src/ServiceStack.Redis/BasicRedisClientManager.cs

Lines changed: 104 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -16,124 +16,125 @@
1616

1717
namespace ServiceStack.Redis
1818
{
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
2424
: 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; }
2828
public int? ConnectTimeout { get; set; }
2929

3030
/// <summary>
3131
/// Gets or sets object key prefix.
3232
/// </summary>
3333
public string NamespacePrefix { get; set; }
34-
private int readWriteHostsIndex;
35-
private int readOnlyHostsIndex;
34+
private int readWriteHostsIndex;
35+
private int readOnlyHostsIndex;
3636

37-
public IRedisClientFactory RedisClientFactory { get; set; }
37+
public IRedisClientFactory RedisClientFactory { get; set; }
3838

39-
public long Db { get; private set; }
39+
public long Db { get; private set; }
4040

4141
public Action<IRedisNativeClient> ConnectionFilter { get; set; }
4242

4343
public List<Action<IRedisClientsManager>> OnFailover { get; private set; }
4444

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);
9697

9798
if (this.ConnectTimeout != null)
9899
{
99100
client.ConnectTimeout = this.ConnectTimeout.Value;
100101
}
101102

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+
}
107108

108109
if (nextHost.RequiresAuth)
109110
client.Password = nextHost.Password;
110111

111112
client.NamespacePrefix = NamespacePrefix;
112113
client.ConnectionFilter = ConnectionFilter;
113114

114-
return client;
115-
}
115+
return client;
116+
}
116117

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);
126127

127128
if (this.ConnectTimeout != null)
128129
{
129130
client.ConnectTimeout = this.ConnectTimeout.Value;
130131
}
131132

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+
}
137138

138139
if (nextHost.RequiresAuth)
139140
client.Password = nextHost.Password;
@@ -142,21 +143,21 @@ public virtual IRedisClient GetReadOnlyClient()
142143
client.ConnectionFilter = ConnectionFilter;
143144

144145
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+
}
160161

161162
public void FailoverTo(params string[] readWriteHosts)
162163
{
@@ -171,8 +172,8 @@ public void FailoverTo(IEnumerable<string> readWriteHosts, IEnumerable<string> r
171172
Start();
172173
}
173174

174-
public void Dispose()
175-
{
176-
}
177-
}
175+
public void Dispose()
176+
{
177+
}
178+
}
178179
}

0 commit comments

Comments
 (0)