Skip to content

Commit 0313198

Browse files
Add constructor overloads to RedisClient that take a non-lazy loaded ConnectionMultiplexer
1 parent 59680d8 commit 0313198

File tree

5 files changed

+33
-14
lines changed

5 files changed

+33
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ In your startup.cs file add the following in the ConfigureServices method replac
4040
// Configure Redis connection
4141
var redisConfigOptions = ConfigurationOptions.Parse("127.0.0.1:6379");
4242
redisConfigOptions.AbortOnConnectFail = false;
43-
services.AddSingleton(new Lazy<IConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(redisConfigOptions)));
43+
services.AddSingleton<IConnectionMultiplexer>(ConnectionMultiplexer.Connect(redisConfigOptions));
4444
```
4545
\* **For production environments it is recommended that you turn on [persistence features](https://redis.io/topics/persistence) in Redis to preserve your message queues in the event that Redis crashes or is restarted.**
4646

sample/Learning.EventStore.Sample.Web/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void ConfigureServices(IServiceCollection services)
5959
// Configure Redis Connection
6060
var redisConfigOptions = ConfigurationOptions.Parse("localhost:6379");
6161
redisConfigOptions.AbortOnConnectFail = false;
62-
services.AddSingleton(new Lazy<IConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(redisConfigOptions)));
62+
services.AddSingleton<IConnectionMultiplexer>(ConnectionMultiplexer.Connect(redisConfigOptions));
6363

6464
// Set up EventStore
6565
var keyPrefix = "Learning.EventStore.Sample.Web";
@@ -69,7 +69,7 @@ public void ConfigureServices(IServiceCollection services)
6969
ApplicationName = keyPrefix,
7070
EnableCompression = false
7171
};
72-
services.AddSingleton<IRedisClient>(y => new RedisClient(y.GetService<Lazy<IConnectionMultiplexer>>()));
72+
services.AddSingleton<IRedisClient>(y => new RedisClient(y.GetService<IConnectionMultiplexer>()));
7373
services.AddSingleton<IEventSubscriber>(y => new RedisEventSubscriber(y.GetService<IRedisClient>(), keyPrefix, y.GetService<IHostingEnvironment>().EnvironmentName));
7474
services.AddScoped<ISession, Session>();
7575
services.AddSingleton<IMessageQueue>(y => new RedisMessageQueue(y.GetService<IRedisClient>(), keyPrefix, y.GetService<IHostingEnvironment>().EnvironmentName));

src/Learning.EventStore.Common/Redis/RedisClient.cs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ namespace Learning.EventStore.Common.Redis
77
{
88
public class RedisClient : IRedisClient, IDisposable
99
{
10-
private readonly Lazy<IConnectionMultiplexer> _redis;
10+
private readonly IConnectionMultiplexer _redisConnection;
1111

12-
public IDatabase Database => _redis.Value.GetDatabase();
12+
public IDatabase Database => _redisConnection.GetDatabase();
1313

1414
private readonly int _retryCount;
1515

@@ -29,14 +29,33 @@ public class RedisClient : IRedisClient, IDisposable
2929
retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)) // exponential backoff
3030
);
3131

32-
public RedisClient(Lazy<IConnectionMultiplexer> redis)
33-
: this(redis, 3)
32+
/// <summary>
33+
/// DEPRECATED: Use the constructor that takes a non-lazy loaded IConnectionMultiplexer
34+
/// </summary>
35+
/// <param name="redisConnection"></param>
36+
public RedisClient(Lazy<IConnectionMultiplexer> redisConnection)
37+
: this(redisConnection, 3)
3438
{
3539
}
3640

37-
public RedisClient(Lazy<IConnectionMultiplexer> redis, int retryCount)
41+
/// <summary>
42+
/// DEPRECATED: Use the constructor that takes a non-lazy loaded IConnectionMultiplexer
43+
/// </summary>
44+
/// <param name="redisConnection"></param>
45+
/// <param name="retryCount"></param>
46+
public RedisClient(Lazy<IConnectionMultiplexer> redisConnection, int retryCount)
47+
: this(redisConnection.Value, retryCount)
3848
{
39-
_redis = redis;
49+
}
50+
51+
public RedisClient(IConnectionMultiplexer redisConnectionConnection)
52+
: this(redisConnectionConnection, 3)
53+
{
54+
}
55+
56+
public RedisClient(IConnectionMultiplexer redisConnectionConnection, int retryCount)
57+
{
58+
_redisConnection = redisConnectionConnection;
4059
_retryCount = retryCount;
4160
}
4261

@@ -108,12 +127,12 @@ public long ListRemove(RedisKey key, RedisValue value)
108127

109128
public async Task PublishAsync(RedisChannel channel, RedisValue value)
110129
{
111-
await RetryPolicyAsync.ExecuteAsync(() => _redis.Value.GetSubscriber().PublishAsync(channel, value)).ConfigureAwait(false);
130+
await RetryPolicyAsync.ExecuteAsync(() => _redisConnection.GetSubscriber().PublishAsync(channel, value)).ConfigureAwait(false);
112131
}
113132

114133
public async Task SubscribeAsync(RedisChannel channel, Action<RedisChannel, RedisValue> handler)
115134
{
116-
await RetryPolicyAsync.ExecuteAsync(() => _redis.Value.GetSubscriber().SubscribeAsync(channel, handler)).ConfigureAwait(false);
135+
await RetryPolicyAsync.ExecuteAsync(() => _redisConnection.GetSubscriber().SubscribeAsync(channel, handler)).ConfigureAwait(false);
117136
}
118137

119138
public async Task<string> StringGetAsync(string key)
@@ -218,7 +237,7 @@ public async Task<bool> ExecuteTransactionAsync(ITransaction trans)
218237

219238
public void Dispose()
220239
{
221-
_redis.Value.Dispose();
240+
_redisConnection.Dispose();
222241
}
223242
}
224243
}

src/Learning.EventStore/Learning.EventStore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<AssemblyTitle>Learning.EventStore</AssemblyTitle>
5-
<VersionPrefix>11.0.1</VersionPrefix>
5+
<VersionPrefix>11.0.2</VersionPrefix>
66
<TargetFrameworks>netstandard2.0</TargetFrameworks>
77
<AssemblyName>Learning.EventStore</AssemblyName>
88
<PackageId>Learning.EventStore</PackageId>

src/Learning.MessageQueue/Learning.MessageQueue.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<AssemblyTitle>Learning.MessageQueue</AssemblyTitle>
5-
<VersionPrefix>4.0.1</VersionPrefix>
5+
<VersionPrefix>4.0.2</VersionPrefix>
66
<TargetFrameworks>netstandard2.0</TargetFrameworks>
77
<AssemblyName>Learning.MessageQueue</AssemblyName>
88
<PackageId>Learning.MessageQueue</PackageId>

0 commit comments

Comments
 (0)