Scenario:
Stackexchange.Redis.ConnectionMultiplexer hides the details of multiple servers and shared and reused between callers. It is thread-safe and ready for this usage.
Solution:
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
| private TResult SendData<TResult>(Func<ConnectionMultiplexer, TResult> call)
{
_lock.EnterReadLock();
try
{
if (_redis != null)
{
return call(_redis);
}
}
finally
{
_lock.ExitReadLock();
}
_lock.EnterWriteLock();
try
{
if (_redis != null)
{
return call(_redis);
}
_redis =
ConnectionMultiplexer.Connect(
$"{string.Join(",", Servers.Select(c => $"{c.Server}:{c.Port}"))}{(string.IsNullOrWhiteSpace(ServerOptions) ? "" : $",{.ServerOptions}")}");
_redis.PreserveAsyncOrder = false;
return call(_redis);
}
finally
{
_lockSlim.ExitWriteLock();
}
}
|
| |
| |
No comments:
Post a Comment