Skip to content

Commit

Permalink
Upgrade StackExchange.Redis to v2.6.122 (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
Turnerj authored Aug 10, 2023
1 parent 8f5956b commit 0ee6795
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 29 deletions.
2 changes: 1 addition & 1 deletion CacheTower.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="StackExchange.Redis" Version="2.6.116" />
<PackageReference Include="StackExchange.Redis" Version="2.6.122" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 4 additions & 2 deletions src/CacheTower.Extensions.Redis/RedisLockExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public RedisLockExtension(IConnectionMultiplexer connection, RedisLockOptions op

LockedOnKeyRefresh = new ConcurrentDictionary<string, TaskCompletionSource<bool>>(StringComparer.Ordinal);

Subscriber.Subscribe(options.RedisChannel, (channel, value) =>
Subscriber.Subscribe(GetRedisChannel(), (channel, value) =>
{
if (!value.IsNull)
{
Expand All @@ -54,6 +54,8 @@ public RedisLockExtension(IConnectionMultiplexer connection, RedisLockOptions op
});
}

private RedisChannel GetRedisChannel() => new(Options.RedisChannel, RedisChannel.PatternMode.Auto);

/// <inheritdoc/>
public void Register(ICacheStack cacheStack)
{
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand All @@ -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<string>(StringComparer.Ordinal);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="StackExchange.Redis" Version="2.6.116" />
<PackageReference Include="StackExchange.Redis" Version="2.6.122" />
</ItemGroup>

<ItemGroup>
Expand Down
18 changes: 7 additions & 11 deletions src/CacheTower.Providers.Redis/RedisCacheLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CacheEntry<T>>(stream);
}
using var stream = new MemoryStream(redisValue);
return Options.Serializer.Deserialize<CacheEntry<T>>(stream);
}

return default;
Expand All @@ -99,13 +97,11 @@ public async ValueTask SetAsync<T>(string cacheKey, CacheEntry<T> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public async Task RefreshValueNotifiesChannelSubscribers()

var completionSource = new TaskCompletionSource<bool>();

await connection.GetSubscriber().SubscribeAsync("CacheTower.CacheLock", (channel, value) =>
await connection.GetSubscriber().SubscribeAsync(new("CacheTower.CacheLock", RedisChannel.PatternMode.Literal), (channel, value) =>
{
if (value == "TestKey")
{
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public async Task RemoteEvictionOccursOnRefresh()
extensionTwo.Register(cacheStackMockTwo);

var completionSource = new TaskCompletionSource<bool>();
connection.GetSubscriber().Subscribe("CacheTower.RemoteEviction").OnMessage(channelMessage =>
connection.GetSubscriber().Subscribe(new("CacheTower.RemoteEviction", RedisChannel.PatternMode.Literal)).OnMessage(channelMessage =>
{
if (channelMessage.Message == "TestKey")
{
Expand Down Expand Up @@ -111,7 +111,7 @@ public async Task RemoteEvictionOccursOnLocalEviction()
extensionTwo.Register(cacheStackMockTwo);

var completionSource = new TaskCompletionSource<bool>();
connection.GetSubscriber().Subscribe("CacheTower.RemoteEviction").OnMessage(channelMessage =>
connection.GetSubscriber().Subscribe(new("CacheTower.RemoteEviction", RedisChannel.PatternMode.Literal)).OnMessage(channelMessage =>
{
if (channelMessage.Message == "TestKey")
{
Expand Down Expand Up @@ -166,7 +166,7 @@ public async Task RemoteFlush()
extensionTwo.Register(cacheStackMockTwo);

var completionSource = new TaskCompletionSource<bool>();
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)
{
Expand Down Expand Up @@ -210,9 +210,9 @@ public async Task NoEvictionOnNewEntries()
var subscriberMock = Substitute.For<ISubscriber>();

subscriberMock.Subscribe(Arg.Any<RedisChannel>(), Arg.Any<CommandFlags>())
.Returns(x => realConnection.GetSubscriber().Subscribe("DummyMessageQueue"));
.Returns(x => realConnection.GetSubscriber().Subscribe(new("DummyMessageQueue", RedisChannel.PatternMode.Literal)));
subscriberMock.Subscribe(Arg.Any<RedisChannel>(), Arg.Any<CommandFlags>())
.Returns(x => realConnection.GetSubscriber().Subscribe("DummyMessageQueue"));
.Returns(x => realConnection.GetSubscriber().Subscribe(new("DummyMessageQueue", RedisChannel.PatternMode.Literal)));
connectionMock.GetSubscriber(Arg.Any<object>())
.Returns(subscriberMock);

Expand All @@ -222,7 +222,7 @@ public async Task NoEvictionOnNewEntries()

await cacheStackOne.GetOrSetAsync<int>("NoEvictionOnNewEntries", _ => Task.FromResult(1), new CacheSettings(TimeSpan.FromMinutes(5)));

await subscriberMock.DidNotReceive().PublishAsync("CacheTower.RemoteEviction", Arg.Any<RedisValue>(), Arg.Any<CommandFlags>());
await subscriberMock.DidNotReceive().PublishAsync(new("CacheTower.RemoteEviction", RedisChannel.PatternMode.Literal), Arg.Any<RedisValue>(), Arg.Any<CommandFlags>());
}
}
}

0 comments on commit 0ee6795

Please sign in to comment.