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

Commit 898d940

Browse files
committed
Add BasicRedisResolver w/o master validation for sentinels
1 parent cd42a9d commit 898d940

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using ServiceStack.Logging;
5+
6+
namespace ServiceStack.Redis
7+
{
8+
public class BasicRedisResolver : IRedisResolver
9+
{
10+
static ILog log = LogManager.GetLogger(typeof(BasicRedisResolver));
11+
12+
public int ReadWriteHostsCount { get; private set; }
13+
public int ReadOnlyHostsCount { get; private set; }
14+
15+
private RedisEndpoint[] masters;
16+
private RedisEndpoint[] slaves;
17+
18+
public RedisEndpoint[] Masters
19+
{
20+
get { return masters; }
21+
}
22+
public RedisEndpoint[] Slaves
23+
{
24+
get { return slaves; }
25+
}
26+
27+
public BasicRedisResolver(IEnumerable<RedisEndpoint> masters, IEnumerable<RedisEndpoint> slaves)
28+
{
29+
ResetMasters(masters.ToList());
30+
ResetSlaves(slaves.ToList());
31+
}
32+
33+
public virtual void ResetMasters(IEnumerable<string> hosts)
34+
{
35+
ResetMasters(hosts.ToRedisEndPoints());
36+
}
37+
38+
public virtual void ResetMasters(List<RedisEndpoint> newMasters)
39+
{
40+
if (newMasters == null || newMasters.Count == 0)
41+
throw new Exception("Must provide at least 1 master");
42+
43+
masters = newMasters.ToArray();
44+
ReadWriteHostsCount = masters.Length;
45+
46+
if (log.IsDebugEnabled)
47+
log.Debug("New Redis Masters: " + string.Join(", ", masters.Map(x => x.GetHostString())));
48+
}
49+
50+
public virtual void ResetSlaves(IEnumerable<string> hosts)
51+
{
52+
ResetSlaves(hosts.ToRedisEndPoints());
53+
}
54+
55+
public RedisClient CreateRedisClient(RedisEndpoint config, bool readWrite)
56+
{
57+
return RedisConfig.ClientFactory(config);
58+
}
59+
60+
public RedisEndpoint GetReadWriteHost(int desiredIndex)
61+
{
62+
return masters[desiredIndex % masters.Length];
63+
}
64+
65+
public RedisEndpoint GetReadOnlyHost(int desiredIndex)
66+
{
67+
return ReadOnlyHostsCount > 0
68+
? slaves[desiredIndex % slaves.Length]
69+
: GetReadWriteHost(desiredIndex);
70+
}
71+
72+
public virtual void ResetSlaves(List<RedisEndpoint> newSlaves)
73+
{
74+
slaves = (newSlaves ?? new List<RedisEndpoint>()).ToArray();
75+
ReadOnlyHostsCount = slaves.Length;
76+
77+
if (log.IsDebugEnabled)
78+
log.Debug("New Redis Slaves: " + string.Join(", ", slaves.Map(x => x.GetHostString())));
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)