From 0ee67956f53e9e06f260f1bbd45b186d7487ca43 Mon Sep 17 00:00:00 2001 From: James Turner Date: Thu, 10 Aug 2023 22:57:59 +0930 Subject: [PATCH] Upgrade StackExchange.Redis to v2.6.122 (#258) --- CacheTower.sln | 2 +- .../CacheTower.Extensions.Redis.csproj | 2 +- .../RedisLockExtension.cs | 6 ++++-- .../RedisRemoteEvictionExtension.cs | 8 ++++---- .../CacheTower.Providers.Redis.csproj | 2 +- .../RedisCacheLayer.cs | 18 +++++++----------- .../Redis/RedisLockExtensionTests.cs | 6 +++--- .../Redis/RedisRemoteEvictionExtensionTests.cs | 12 ++++++------ 8 files changed, 27 insertions(+), 29 deletions(-) diff --git a/CacheTower.sln b/CacheTower.sln index 1e73a7ef..726fe881 100644 --- a/CacheTower.sln +++ b/CacheTower.sln @@ -45,7 +45,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CacheTower.Serializers.Newt EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CacheTower.Serializers.Protobuf", "src\CacheTower.Serializers.Protobuf\CacheTower.Serializers.Protobuf.csproj", "{436A6DF0-3DD8-4342-ACC5-EF0B8D5DF9DD}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheTower.Serializers.SystemTextJson", "src\CacheTower.Serializers.SystemTextJson\CacheTower.Serializers.SystemTextJson.csproj", "{4E94E3E8-3CC0-4F23-BFD7-157C8C386AFB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CacheTower.Serializers.SystemTextJson", "src\CacheTower.Serializers.SystemTextJson\CacheTower.Serializers.SystemTextJson.csproj", "{4E94E3E8-3CC0-4F23-BFD7-157C8C386AFB}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/CacheTower.Extensions.Redis/CacheTower.Extensions.Redis.csproj b/src/CacheTower.Extensions.Redis/CacheTower.Extensions.Redis.csproj index 9107c912..4dcb0844 100644 --- a/src/CacheTower.Extensions.Redis/CacheTower.Extensions.Redis.csproj +++ b/src/CacheTower.Extensions.Redis/CacheTower.Extensions.Redis.csproj @@ -9,7 +9,7 @@ - + diff --git a/src/CacheTower.Extensions.Redis/RedisLockExtension.cs b/src/CacheTower.Extensions.Redis/RedisLockExtension.cs index 69ecb991..27ca21d4 100644 --- a/src/CacheTower.Extensions.Redis/RedisLockExtension.cs +++ b/src/CacheTower.Extensions.Redis/RedisLockExtension.cs @@ -45,7 +45,7 @@ public RedisLockExtension(IConnectionMultiplexer connection, RedisLockOptions op LockedOnKeyRefresh = new ConcurrentDictionary>(StringComparer.Ordinal); - Subscriber.Subscribe(options.RedisChannel, (channel, value) => + Subscriber.Subscribe(GetRedisChannel(), (channel, value) => { if (!value.IsNull) { @@ -54,6 +54,8 @@ public RedisLockExtension(IConnectionMultiplexer connection, RedisLockOptions op }); } + private RedisChannel GetRedisChannel() => new(Options.RedisChannel, RedisChannel.PatternMode.Auto); + /// public void Register(ICacheStack cacheStack) { @@ -68,7 +70,7 @@ public void Register(ICacheStack cacheStack) private async ValueTask ReleaseLockAsync(string cacheKey) { var lockKey = string.Format(Options.KeyFormat, cacheKey); - await Subscriber.PublishAsync(Options.RedisChannel, cacheKey, CommandFlags.FireAndForget); + await Subscriber.PublishAsync(GetRedisChannel(), cacheKey, CommandFlags.FireAndForget); await Database.KeyDeleteAsync(lockKey, CommandFlags.FireAndForget); UnlockWaitingTasks(cacheKey); } diff --git a/src/CacheTower.Extensions.Redis/RedisRemoteEvictionExtension.cs b/src/CacheTower.Extensions.Redis/RedisRemoteEvictionExtension.cs index 7f1c5fa4..7c9d87b6 100644 --- a/src/CacheTower.Extensions.Redis/RedisRemoteEvictionExtension.cs +++ b/src/CacheTower.Extensions.Redis/RedisRemoteEvictionExtension.cs @@ -12,8 +12,8 @@ namespace CacheTower.Extensions.Redis public class RedisRemoteEvictionExtension : ICacheChangeExtension { private ISubscriber Subscriber { get; } - private string FlushChannel { get; } - private string EvictionChannel { get; } + private RedisChannel FlushChannel { get; } + private RedisChannel EvictionChannel { get; } private bool IsRegistered { get; set; } @@ -39,8 +39,8 @@ public RedisRemoteEvictionExtension(IConnectionMultiplexer connection, string ch } Subscriber = connection.GetSubscriber(); - FlushChannel = $"{channelPrefix}.RemoteFlush"; - EvictionChannel = $"{channelPrefix}.RemoteEviction"; + FlushChannel = new($"{channelPrefix}.RemoteFlush", RedisChannel.PatternMode.Auto); + EvictionChannel = new($"{channelPrefix}.RemoteEviction", RedisChannel.PatternMode.Auto); FlaggedEvictions = new HashSet(StringComparer.Ordinal); } diff --git a/src/CacheTower.Providers.Redis/CacheTower.Providers.Redis.csproj b/src/CacheTower.Providers.Redis/CacheTower.Providers.Redis.csproj index f6099f0a..6b401982 100644 --- a/src/CacheTower.Providers.Redis/CacheTower.Providers.Redis.csproj +++ b/src/CacheTower.Providers.Redis/CacheTower.Providers.Redis.csproj @@ -9,7 +9,7 @@ - + diff --git a/src/CacheTower.Providers.Redis/RedisCacheLayer.cs b/src/CacheTower.Providers.Redis/RedisCacheLayer.cs index cb8e425b..7fb03e5d 100644 --- a/src/CacheTower.Providers.Redis/RedisCacheLayer.cs +++ b/src/CacheTower.Providers.Redis/RedisCacheLayer.cs @@ -75,10 +75,8 @@ public async ValueTask FlushAsync() var redisValue = await Database.StringGetAsync(cacheKey); if (redisValue != RedisValue.Null) { - using (var stream = new MemoryStream(redisValue)) - { - return Options.Serializer.Deserialize>(stream); - } + using var stream = new MemoryStream(redisValue); + return Options.Serializer.Deserialize>(stream); } return default; @@ -99,13 +97,11 @@ public async ValueTask SetAsync(string cacheKey, CacheEntry cacheEntry) return; } - using (var stream = new MemoryStream()) - { - Options.Serializer.Serialize(stream, cacheEntry); - stream.Seek(0, SeekOrigin.Begin); - var redisValue = RedisValue.CreateFrom(stream); - await Database.StringSetAsync(cacheKey, redisValue, expiryOffset); - } + using var stream = new MemoryStream(); + Options.Serializer.Serialize(stream, cacheEntry); + stream.Seek(0, SeekOrigin.Begin); + var redisValue = RedisValue.CreateFrom(stream); + await Database.StringSetAsync(cacheKey, redisValue, expiryOffset); } } } diff --git a/tests/CacheTower.Tests/Extensions/Redis/RedisLockExtensionTests.cs b/tests/CacheTower.Tests/Extensions/Redis/RedisLockExtensionTests.cs index 819b47e6..1c73b6e8 100644 --- a/tests/CacheTower.Tests/Extensions/Redis/RedisLockExtensionTests.cs +++ b/tests/CacheTower.Tests/Extensions/Redis/RedisLockExtensionTests.cs @@ -74,7 +74,7 @@ public async Task RefreshValueNotifiesChannelSubscribers() var completionSource = new TaskCompletionSource(); - await connection.GetSubscriber().SubscribeAsync("CacheTower.CacheLock", (channel, value) => + await connection.GetSubscriber().SubscribeAsync(new("CacheTower.CacheLock", RedisChannel.PatternMode.Literal), (channel, value) => { if (value == "TestKey") { @@ -126,7 +126,7 @@ public async Task ObservedLockSingle() Assert.IsTrue(extension.LockedOnKeyRefresh.ContainsKey("TestKey"), "Lock was not established"); //Trigger the end of the lock - await connection.GetSubscriber().PublishAsync("CacheTower.CacheLock", "TestKey"); + await connection.GetSubscriber().PublishAsync(new("CacheTower.CacheLock", RedisChannel.PatternMode.Literal), "TestKey"); var succeedingTask = await Task.WhenAny(lockTask, Task.Delay(TimeSpan.FromSeconds(10))); if (!succeedingTask.Equals(lockTask)) @@ -160,7 +160,7 @@ public async Task ObservedLockMultiple() Assert.IsTrue(extension.LockedOnKeyRefresh.ContainsKey("TestKey"), "Lock was not established"); //Trigger the end of the lock - await connection.GetSubscriber().PublishAsync("CacheTower.CacheLock", "TestKey"); + await connection.GetSubscriber().PublishAsync(new("CacheTower.CacheLock", RedisChannel.PatternMode.Literal), "TestKey"); var whenAllRefreshesTask = Task.WhenAll(lockTask1, lockTask2); var succeedingTask = await Task.WhenAny(whenAllRefreshesTask, Task.Delay(TimeSpan.FromSeconds(10))); diff --git a/tests/CacheTower.Tests/Extensions/Redis/RedisRemoteEvictionExtensionTests.cs b/tests/CacheTower.Tests/Extensions/Redis/RedisRemoteEvictionExtensionTests.cs index ef15f8cf..68ab6899 100644 --- a/tests/CacheTower.Tests/Extensions/Redis/RedisRemoteEvictionExtensionTests.cs +++ b/tests/CacheTower.Tests/Extensions/Redis/RedisRemoteEvictionExtensionTests.cs @@ -60,7 +60,7 @@ public async Task RemoteEvictionOccursOnRefresh() extensionTwo.Register(cacheStackMockTwo); var completionSource = new TaskCompletionSource(); - connection.GetSubscriber().Subscribe("CacheTower.RemoteEviction").OnMessage(channelMessage => + connection.GetSubscriber().Subscribe(new("CacheTower.RemoteEviction", RedisChannel.PatternMode.Literal)).OnMessage(channelMessage => { if (channelMessage.Message == "TestKey") { @@ -111,7 +111,7 @@ public async Task RemoteEvictionOccursOnLocalEviction() extensionTwo.Register(cacheStackMockTwo); var completionSource = new TaskCompletionSource(); - connection.GetSubscriber().Subscribe("CacheTower.RemoteEviction").OnMessage(channelMessage => + connection.GetSubscriber().Subscribe(new("CacheTower.RemoteEviction", RedisChannel.PatternMode.Literal)).OnMessage(channelMessage => { if (channelMessage.Message == "TestKey") { @@ -166,7 +166,7 @@ public async Task RemoteFlush() extensionTwo.Register(cacheStackMockTwo); var completionSource = new TaskCompletionSource(); - connection.GetSubscriber().Subscribe("CacheTower.RemoteFlush").OnMessage(channelMessage => + connection.GetSubscriber().Subscribe(new("CacheTower.RemoteFlush", RedisChannel.PatternMode.Literal)).OnMessage(channelMessage => { if (channelMessage.Message == StackExchange.Redis.RedisValue.EmptyString) { @@ -210,9 +210,9 @@ public async Task NoEvictionOnNewEntries() var subscriberMock = Substitute.For(); subscriberMock.Subscribe(Arg.Any(), Arg.Any()) - .Returns(x => realConnection.GetSubscriber().Subscribe("DummyMessageQueue")); + .Returns(x => realConnection.GetSubscriber().Subscribe(new("DummyMessageQueue", RedisChannel.PatternMode.Literal))); subscriberMock.Subscribe(Arg.Any(), Arg.Any()) - .Returns(x => realConnection.GetSubscriber().Subscribe("DummyMessageQueue")); + .Returns(x => realConnection.GetSubscriber().Subscribe(new("DummyMessageQueue", RedisChannel.PatternMode.Literal))); connectionMock.GetSubscriber(Arg.Any()) .Returns(subscriberMock); @@ -222,7 +222,7 @@ public async Task NoEvictionOnNewEntries() await cacheStackOne.GetOrSetAsync("NoEvictionOnNewEntries", _ => Task.FromResult(1), new CacheSettings(TimeSpan.FromMinutes(5))); - await subscriberMock.DidNotReceive().PublishAsync("CacheTower.RemoteEviction", Arg.Any(), Arg.Any()); + await subscriberMock.DidNotReceive().PublishAsync(new("CacheTower.RemoteEviction", RedisChannel.PatternMode.Literal), Arg.Any(), Arg.Any()); } } } \ No newline at end of file