Skip to content

Commit

Permalink
Updated to latest Redis Dep, Adds ILoggerFactory support
Browse files Browse the repository at this point in the history
  • Loading branch information
niemyjski committed Jan 4, 2024
1 parent 793d51e commit eb30a07
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 38 deletions.
2 changes: 1 addition & 1 deletion samples/Foundatio.SampleJob/SampleServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static IServiceProvider Create(ILoggerFactory loggerFactory) {
container.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
}

var muxer = ConnectionMultiplexer.Connect("localhost");
var muxer = ConnectionMultiplexer.Connect("localhost", o => o.LoggerFactory = loggerFactory);
container.AddSingleton(muxer);
container.AddSingleton<IQueue<PingRequest>>(s => new RedisQueue<PingRequest>(o => o.ConnectionMultiplexer(muxer).RetryDelay(TimeSpan.FromSeconds(1)).WorkItemTimeout(TimeSpan.FromSeconds(5)).LoggerFactory(loggerFactory)));
container.AddSingleton<ICacheClient>(s => new RedisCacheClient(o => o.ConnectionMultiplexer(muxer).LoggerFactory(loggerFactory)));
Expand Down
2 changes: 1 addition & 1 deletion samples/Foundatio.SampleJobClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void Main(string[] args)
_loggerFactory.MaxLogEntriesToStore = Console.WindowHeight - (OPTIONS_MENU_LINE_COUNT + SEPERATOR_LINE_COUNT) - 1;
_logger = _loggerFactory.CreateLogger<Program>();

var muxer = ConnectionMultiplexer.Connect("localhost");
var muxer = ConnectionMultiplexer.Connect("localhost", o => o.LoggerFactory = _loggerFactory);
_queue = new RedisQueue<PingRequest>(new RedisQueueOptions<PingRequest> { ConnectionMultiplexer = muxer });
_messageBus = new RedisMessageBus(o => o.Subscriber(muxer.GetSubscriber()).LoggerFactory(_loggerFactory).MapMessageTypeToClassName<EchoMessage>());

Expand Down
4 changes: 1 addition & 3 deletions src/Foundatio.Redis/Cache/RedisCacheClientOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using Foundatio.Serializer;
using StackExchange.Redis;
using StackExchange.Redis;

namespace Foundatio.Caching {
public class RedisCacheClientOptions : SharedOptions {
Expand Down
2 changes: 1 addition & 1 deletion src/Foundatio.Redis/Foundatio.Redis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<ItemGroup>
<PackageReference Include="Foundatio" Version="10.6.1" Condition="'$(ReferenceFoundatioSource)' == '' OR '$(ReferenceFoundatioSource)' == 'false'" />
<ProjectReference Include="..\..\..\Foundatio\src\Foundatio\Foundatio.csproj" Condition="'$(ReferenceFoundatioSource)' == 'true'" />
<PackageReference Include="StackExchange.Redis" Version="2.6.116" />
<PackageReference Include="StackExchange.Redis" Version="2.7.10" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Scripts\*.lua" />
Expand Down
6 changes: 3 additions & 3 deletions tests/Foundatio.Redis.Tests/Caching/RedisCacheClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
namespace Foundatio.Redis.Tests.Caching {
public class RedisCacheClientTests : CacheClientTestsBase {
public RedisCacheClientTests(ITestOutputHelper output) : base(output) {
var muxer = SharedConnection.GetMuxer();
var muxer = SharedConnection.GetMuxer(Log);
muxer.FlushAllAsync().GetAwaiter().GetResult();
}

protected override ICacheClient GetCacheClient(bool shouldThrowOnSerializationError = true) {
return new RedisCacheClient(o => o.ConnectionMultiplexer(SharedConnection.GetMuxer()).LoggerFactory(Log).ShouldThrowOnSerializationError(shouldThrowOnSerializationError));
return new RedisCacheClient(o => o.ConnectionMultiplexer(SharedConnection.GetMuxer(Log)).LoggerFactory(Log).ShouldThrowOnSerializationError(shouldThrowOnSerializationError));
}

[Fact]
Expand Down Expand Up @@ -131,7 +131,7 @@ public override Task CanManageListsAsync() {

[Fact]
public async Task CanUpgradeListType() {
var db = SharedConnection.GetMuxer().GetDatabase();
var db = SharedConnection.GetMuxer(Log).GetDatabase();
var cache = GetCacheClient();
if (cache == null)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
namespace Foundatio.Redis.Tests.Caching {
public class RedisHybridCacheClientTests : HybridCacheClientTests {
public RedisHybridCacheClientTests(ITestOutputHelper output) : base(output) {
var muxer = SharedConnection.GetMuxer();
var muxer = SharedConnection.GetMuxer(Log);
muxer.FlushAllAsync().GetAwaiter().GetResult();
}

protected override ICacheClient GetCacheClient(bool shouldThrowOnSerializationError = true) {
return new RedisHybridCacheClient(o => o
.ConnectionMultiplexer(SharedConnection.GetMuxer())
.ConnectionMultiplexer(SharedConnection.GetMuxer(Log))
.LoggerFactory(Log).ShouldThrowOnSerializationError(shouldThrowOnSerializationError),
localConfig => localConfig
.CloneValues(true)
Expand Down
4 changes: 2 additions & 2 deletions tests/Foundatio.Redis.Tests/Jobs/RedisJobQueueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
namespace Foundatio.Redis.Tests.Jobs {
public class RedisJobQueueTests : JobQueueTestsBase {
public RedisJobQueueTests(ITestOutputHelper output) : base(output) {
var muxer = SharedConnection.GetMuxer();
var muxer = SharedConnection.GetMuxer(Log);
muxer.FlushAllAsync().GetAwaiter().GetResult();
}

protected override IQueue<SampleQueueWorkItem> GetSampleWorkItemQueue(int retries, TimeSpan retryDelay) {
return new RedisQueue<SampleQueueWorkItem>(o => o
.ConnectionMultiplexer(SharedConnection.GetMuxer())
.ConnectionMultiplexer(SharedConnection.GetMuxer(Log))
.Retries(retries)
.RetryDelay(retryDelay)
.LoggerFactory(Log)
Expand Down
4 changes: 2 additions & 2 deletions tests/Foundatio.Redis.Tests/Locks/RedisLockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class RedisLockTests : LockTestBase, IDisposable {
private readonly IMessageBus _messageBus;

public RedisLockTests(ITestOutputHelper output) : base(output) {
var muxer = SharedConnection.GetMuxer();
var muxer = SharedConnection.GetMuxer(Log);
muxer.FlushAllAsync().GetAwaiter().GetResult();
_cache = new RedisCacheClient(o => o.ConnectionMultiplexer(muxer).LoggerFactory(Log));
_messageBus = new RedisMessageBus(o => o.Subscriber(muxer.GetSubscriber()).Topic("test-lock").LoggerFactory(Log));
Expand Down Expand Up @@ -95,7 +95,7 @@ public override Task LockOneAtATimeAsync() {
public void Dispose() {
_cache.Dispose();
_messageBus.Dispose();
var muxer = SharedConnection.GetMuxer();
var muxer = SharedConnection.GetMuxer(Log);
muxer.FlushAllAsync().GetAwaiter().GetResult();
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Foundatio.Redis.Tests/Messaging/RedisMessageBusTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
namespace Foundatio.Redis.Tests.Messaging {
public class RedisMessageBusTests : MessageBusTestBase {
public RedisMessageBusTests(ITestOutputHelper output) : base(output) {
var muxer = SharedConnection.GetMuxer();
var muxer = SharedConnection.GetMuxer(Log);
muxer.FlushAllAsync().GetAwaiter().GetResult();
}

protected override IMessageBus GetMessageBus(Func<SharedMessageBusOptions, SharedMessageBusOptions> config = null) {
return new RedisMessageBus(o => {
o.Subscriber(SharedConnection.GetMuxer().GetSubscriber());
o.Subscriber(SharedConnection.GetMuxer(Log).GetSubscriber());
o.Topic("test-messages");
o.LoggerFactory(Log);
if (config != null)
Expand Down Expand Up @@ -123,7 +123,7 @@ public override void CanDisposeWithNoSubscribersOrPublishers() {

[Fact]
public async Task CanDisposeCacheAndQueueAndReceiveSubscribedMessages() {
var muxer = SharedConnection.GetMuxer();
var muxer = SharedConnection.GetMuxer(Log);
var messageBus1 = new RedisMessageBus(new RedisMessageBusOptions { Subscriber = muxer.GetSubscriber(), Topic = "test-messages", LoggerFactory = Log });

var cache = new RedisCacheClient(new RedisCacheClientOptions { ConnectionMultiplexer = muxer });
Expand Down
8 changes: 4 additions & 4 deletions tests/Foundatio.Redis.Tests/Metrics/RedisMetricsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
namespace Foundatio.Redis.Tests.Metrics {
public class RedisMetricsTests : MetricsClientTestBase, IDisposable {
public RedisMetricsTests(ITestOutputHelper output) : base(output) {
var muxer = SharedConnection.GetMuxer();
var muxer = SharedConnection.GetMuxer(Log);
muxer.FlushAllAsync().GetAwaiter().GetResult();
}

#pragma warning disable CS0618 // Type or member is obsolete
public override IMetricsClient GetMetricsClient(bool buffered = false) {
return new RedisMetricsClient(o => o.ConnectionMultiplexer(SharedConnection.GetMuxer()).Buffered(buffered).LoggerFactory(Log));
return new RedisMetricsClient(o => o.ConnectionMultiplexer(SharedConnection.GetMuxer(Log)).Buffered(buffered).LoggerFactory(Log));
}
#pragma warning restore CS0618 // Type or member is obsolete

Expand Down Expand Up @@ -86,7 +86,7 @@ public async Task SendGaugesBufferedAsync() {

[Fact]
public async Task SendRedisAsync() {
var db = SharedConnection.GetMuxer().GetDatabase();
var db = SharedConnection.GetMuxer(Log).GetDatabase();

int max = 1000;
for (int index = 0; index <= max; index++) {
Expand All @@ -95,7 +95,7 @@ public async Task SendRedisAsync() {
}

public void Dispose() {
var muxer = SharedConnection.GetMuxer();
var muxer = SharedConnection.GetMuxer(Log);
muxer.FlushAllAsync().GetAwaiter().GetResult();
}
}
Expand Down
24 changes: 12 additions & 12 deletions tests/Foundatio.Redis.Tests/Queues/RedisQueueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
namespace Foundatio.Redis.Tests.Queues {
public class RedisQueueTests : QueueTestBase {
public RedisQueueTests(ITestOutputHelper output) : base(output) {
var muxer = SharedConnection.GetMuxer();
var muxer = SharedConnection.GetMuxer(Log);
while (muxer.CountAllKeysAsync().GetAwaiter().GetResult() != 0)
muxer.FlushAllAsync().GetAwaiter().GetResult();
}

protected override IQueue<SimpleWorkItem> GetQueue(int retries = 1, TimeSpan? workItemTimeout = null, TimeSpan? retryDelay = null, int[] retryMultipliers = null, int deadLetterMaxItems = 100, bool runQueueMaintenance = true) {
var queue = new RedisQueue<SimpleWorkItem>(o => o
.ConnectionMultiplexer(SharedConnection.GetMuxer())
.ConnectionMultiplexer(SharedConnection.GetMuxer(Log))
.Retries(retries)
.RetryDelay(retryDelay.GetValueOrDefault(TimeSpan.FromMinutes(1)))
.RetryMultipliers(retryMultipliers ?? new[] { 1, 3, 5, 10 })
Expand Down Expand Up @@ -155,7 +155,7 @@ public override Task CanUseQueueOptionsAsync() {

[RetryFact]
public override async Task CanDequeueWithLockingAsync() {
var muxer = SharedConnection.GetMuxer();
var muxer = SharedConnection.GetMuxer(Log);
using var cache = new RedisCacheClient(new RedisCacheClientOptions { ConnectionMultiplexer = muxer, LoggerFactory = Log });
using var messageBus = new RedisMessageBus(new RedisMessageBusOptions { Subscriber = muxer.GetSubscriber(), Topic = "test-queue", LoggerFactory = Log });
var distributedLock = new CacheLockProvider(cache, messageBus, Log);
Expand All @@ -164,7 +164,7 @@ public override async Task CanDequeueWithLockingAsync() {

[Fact]
public override async Task CanHaveMultipleQueueInstancesWithLockingAsync() {
var muxer = SharedConnection.GetMuxer();
var muxer = SharedConnection.GetMuxer(Log);
using var cache = new RedisCacheClient(new RedisCacheClientOptions { ConnectionMultiplexer = muxer, LoggerFactory = Log });
using var messageBus = new RedisMessageBus(new RedisMessageBusOptions { Subscriber = muxer.GetSubscriber(), Topic = "test-queue", LoggerFactory = Log });
var distributedLock = new CacheLockProvider(cache, messageBus, Log);
Expand All @@ -178,7 +178,7 @@ public async Task VerifyCacheKeysAreCorrect() {
return;

using (queue) {
var muxer = SharedConnection.GetMuxer();
var muxer = SharedConnection.GetMuxer(Log);
var db = muxer.GetDatabase();
string listPrefix = muxer.IsCluster() ? "{q:SimpleWorkItem}" : "q:SimpleWorkItem";

Expand Down Expand Up @@ -231,7 +231,7 @@ public async Task VerifyCacheKeysAreCorrectAfterAbandon() {

using (TestSystemClock.Install()) {
using (queue) {
var muxer = SharedConnection.GetMuxer();
var muxer = SharedConnection.GetMuxer(Log);
var db = muxer.GetDatabase();
string listPrefix = muxer.IsCluster() ? "{q:SimpleWorkItem}" : "q:SimpleWorkItem";

Expand Down Expand Up @@ -299,7 +299,7 @@ public async Task VerifyCacheKeysAreCorrectAfterAbandonWithRetryDelay() {

using (TestSystemClock.Install()) {
using (queue) {
var muxer = SharedConnection.GetMuxer();
var muxer = SharedConnection.GetMuxer(Log);
var db = muxer.GetDatabase();
string listPrefix = muxer.IsCluster() ? "{q:SimpleWorkItem}" : "q:SimpleWorkItem";

Expand Down Expand Up @@ -361,7 +361,7 @@ public async Task CanTrimDeadletterItems() {
return;

using (queue) {
var muxer = SharedConnection.GetMuxer();
var muxer = SharedConnection.GetMuxer(Log);
var db = muxer.GetDatabase();
string listPrefix = muxer.IsCluster() ? "{q:SimpleWorkItem}" : "q:SimpleWorkItem";

Expand Down Expand Up @@ -552,7 +552,7 @@ await queue.EnqueueAsync(new SimpleWorkItem {
Assert.Equal(workItemCount, stats.Completed + stats.Deadletter);
Assert.Equal(0, stats.Queued);

var muxer = SharedConnection.GetMuxer();
var muxer = SharedConnection.GetMuxer(Log);
_logger.LogTrace("# Keys: {0}", muxer.CountAllKeysAsync());
}
}
Expand Down Expand Up @@ -590,7 +590,7 @@ await queue.EnqueueAsync(new SimpleWorkItem {
Assert.Equal(workItemCount, stats.Completed);
Assert.Equal(0, stats.Queued);

var muxer = SharedConnection.GetMuxer();
var muxer = SharedConnection.GetMuxer(Log);
_logger.LogTrace("# Keys: {0}", muxer.CountAllKeysAsync());
}
}
Expand Down Expand Up @@ -630,7 +630,7 @@ await queue.StartWorkingAsync(async workItem => {
Assert.Equal(workItemCount, stats.Completed);
Assert.Equal(0, stats.Queued);

var muxer = SharedConnection.GetMuxer();
var muxer = SharedConnection.GetMuxer(Log);
_logger.LogTrace("# Keys: {0}", muxer.CountAllKeysAsync());
}
}
Expand Down Expand Up @@ -663,7 +663,7 @@ private IQueue<T> CreateQueue<T>(bool allQueuesTheSameName = true) where T : cla
name = "cmd";

var queue = new RedisQueue<T>(o => o
.ConnectionMultiplexer(SharedConnection.GetMuxer())
.ConnectionMultiplexer(SharedConnection.GetMuxer(Log))
.Name(name)
.LoggerFactory(Log)
);
Expand Down
5 changes: 3 additions & 2 deletions tests/Foundatio.Redis.Tests/SharedConnection.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using System;
using Foundatio.Tests.Utility;
using Microsoft.Extensions.Logging;
using StackExchange.Redis;

namespace Foundatio.Redis.Tests {
public static class SharedConnection {
private static ConnectionMultiplexer _muxer;

public static ConnectionMultiplexer GetMuxer() {
public static ConnectionMultiplexer GetMuxer(ILoggerFactory loggerFactory) {
string connectionString = Configuration.GetConnectionString("RedisConnectionString");
if (String.IsNullOrEmpty(connectionString))
return null;

if (_muxer == null)
_muxer = ConnectionMultiplexer.Connect(connectionString);
_muxer = ConnectionMultiplexer.Connect(connectionString, o => o.LoggerFactory = loggerFactory);

return _muxer;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Foundatio.Redis.Tests/Storage/RedisFileStorageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
namespace Foundatio.Redis.Tests.Storage {
public class RedisFileStorageTests : FileStorageTestsBase {
public RedisFileStorageTests(ITestOutputHelper output) : base(output) {
var muxer = SharedConnection.GetMuxer();
var muxer = SharedConnection.GetMuxer(Log);
muxer.FlushAllAsync().GetAwaiter().GetResult();
}

protected override IFileStorage GetStorage() {
return new RedisFileStorage(o => o.ConnectionMultiplexer(SharedConnection.GetMuxer()).LoggerFactory(Log));
return new RedisFileStorage(o => o.ConnectionMultiplexer(SharedConnection.GetMuxer(Log)).LoggerFactory(Log));
}

[Fact]
Expand Down

0 comments on commit eb30a07

Please sign in to comment.