forked from ServiceStack/ServiceStack.Redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicRedisResolver.cs
More file actions
95 lines (77 loc) · 2.96 KB
/
BasicRedisResolver.cs
File metadata and controls
95 lines (77 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections.Generic;
using System.Linq;
using ServiceStack.Logging;
using ServiceStack.Text;
namespace ServiceStack.Redis
{
public class BasicRedisResolver : IRedisResolver, IRedisResolverExtended
{
static ILog log = LogManager.GetLogger(typeof(BasicRedisResolver));
public Func<RedisEndpoint, RedisClient> ClientFactory { get; set; }
public int ReadWriteHostsCount { get; private set; }
public int ReadOnlyHostsCount { get; private set; }
private RedisEndpoint[] masters;
private RedisEndpoint[] slaves;
public RedisEndpoint[] Masters
{
get { return masters; }
}
public RedisEndpoint[] Slaves
{
get { return slaves; }
}
public BasicRedisResolver(IEnumerable<RedisEndpoint> masters, IEnumerable<RedisEndpoint> slaves)
{
ResetMasters(masters.ToList());
ResetSlaves(slaves.ToList());
ClientFactory = RedisConfig.ClientFactory;
}
public virtual void ResetMasters(IEnumerable<string> hosts)
{
ResetMasters(hosts.ToRedisEndPoints());
}
public virtual void ResetMasters(List<RedisEndpoint> newMasters)
{
if (newMasters == null || newMasters.Count == 0)
throw new Exception("Must provide at least 1 master");
masters = newMasters.ToArray();
ReadWriteHostsCount = masters.Length;
if (log.IsDebugEnabled)
log.Debug("New Redis Masters: " + string.Join(", ", masters.Map(x => x.GetHostString())));
}
public virtual void ResetSlaves(IEnumerable<string> hosts)
{
ResetSlaves(hosts.ToRedisEndPoints());
}
public virtual void ResetSlaves(List<RedisEndpoint> newSlaves)
{
slaves = (newSlaves ?? TypeConstants<RedisEndpoint>.EmptyList).ToArray();
ReadOnlyHostsCount = slaves.Length;
if (log.IsDebugEnabled)
log.Debug("New Redis Slaves: " + string.Join(", ", slaves.Map(x => x.GetHostString())));
}
public RedisClient CreateRedisClient(RedisEndpoint config, bool master)
{
return ClientFactory(config);
}
public RedisEndpoint GetReadWriteHost(int desiredIndex)
{
return masters[desiredIndex % masters.Length];
}
public RedisEndpoint GetReadOnlyHost(int desiredIndex)
{
return ReadOnlyHostsCount > 0
? slaves[desiredIndex % slaves.Length]
: GetReadWriteHost(desiredIndex);
}
public RedisClient CreateMasterClient(int desiredIndex)
{
return CreateRedisClient(GetReadWriteHost(desiredIndex), master: true);
}
public RedisClient CreateSlaveClient(int desiredIndex)
{
return CreateRedisClient(GetReadOnlyHost(desiredIndex), master: false);
}
}
}