Skip to content

Commit

Permalink
Fixes #285: InMemoryCacheClient ReplaceIfEqualAsync was not setting c…
Browse files Browse the repository at this point in the history
…ache expiration causing locks to fail (#287)
  • Loading branch information
niemyjski authored Jul 26, 2023
1 parent b32bd48 commit 0eea68f
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 38 deletions.
13 changes: 8 additions & 5 deletions src/Foundatio.TestHarness/Caching/CacheClientTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,16 +483,19 @@ public virtual async Task CanReplaceIfEqual() {
using (cache) {
await cache.RemoveAllAsync();

Assert.True(await cache.AddAsync("replace-if-equal", "123"));
var result = await cache.GetAsync<string>("replace-if-equal");
const string cacheKey = "replace-if-equal";
Assert.True(await cache.AddAsync(cacheKey, "123"));
var result = await cache.GetAsync<string>(cacheKey);
Assert.NotNull(result);
Assert.Equal("123", result.Value);
Assert.Null(await cache.GetExpirationAsync(cacheKey));

Assert.False(await cache.ReplaceIfEqualAsync("replace-if-equal", "456", "789"));
Assert.True(await cache.ReplaceIfEqualAsync("replace-if-equal", "456", "123"));
result = await cache.GetAsync<string>("replace-if-equal");
Assert.False(await cache.ReplaceIfEqualAsync(cacheKey, "456", "789", TimeSpan.FromHours(1)));
Assert.True(await cache.ReplaceIfEqualAsync(cacheKey, "456", "123", TimeSpan.FromHours(1)));
result = await cache.GetAsync<string>(cacheKey);
Assert.NotNull(result);
Assert.Equal("456", result.Value);
Assert.NotNull(await cache.GetExpirationAsync(cacheKey));
}
}

Expand Down
32 changes: 22 additions & 10 deletions src/Foundatio/Caching/InMemoryCacheClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -401,7 +401,10 @@ public async Task<long> ListAddAsync<T>(string key, IEnumerable<T> values, TimeS
collection.Add(stringValue);
cacheEntry.Value = collection;
cacheEntry.ExpiresAt = expiresAt;
if (expiresIn.HasValue)
cacheEntry.ExpiresAt = expiresAt;
return cacheEntry;
});

Expand All @@ -417,7 +420,10 @@ public async Task<long> ListAddAsync<T>(string key, IEnumerable<T> values, TimeS
collection.AddRange(items);
cacheEntry.Value = collection;
cacheEntry.ExpiresAt = expiresAt;
if (expiresIn.HasValue)
cacheEntry.ExpiresAt = expiresAt;
return cacheEntry;
});

Expand Down Expand Up @@ -452,7 +458,9 @@ public Task<long> ListRemoveAsync<T>(string key, IEnumerable<T> values, TimeSpan
cacheEntry.Value = collection;
}
cacheEntry.ExpiresAt = expiresAt;
if (expiresIn.HasValue)
cacheEntry.ExpiresAt = expiresAt;
if (_logger.IsEnabled(LogLevel.Trace)) _logger.LogTrace("Removed value from set with cache key: {Key}", key);
return cacheEntry;
});
Expand All @@ -468,7 +476,9 @@ public Task<long> ListRemoveAsync<T>(string key, IEnumerable<T> values, TimeSpan
cacheEntry.Value = collection;
}
cacheEntry.ExpiresAt = expiresAt;
if (expiresIn.HasValue)
cacheEntry.ExpiresAt = expiresAt;
if (_logger.IsEnabled(LogLevel.Trace)) _logger.LogTrace("Removed value from set with cache key: {Key}", key);
return cacheEntry;
});
Expand Down Expand Up @@ -563,18 +573,20 @@ public async Task<bool> ReplaceIfEqualAsync<T>(string key, T value, T expected,

var expiresAt = expiresIn.HasValue ? SystemClock.UtcNow.SafeAdd(expiresIn.Value) : DateTime.MaxValue;
bool wasExpectedValue = false;
bool success = _memory.TryUpdate(key, (k, e) => {
var currentValue = e.GetValue<T>();
bool success = _memory.TryUpdate(key, (k, cacheEntry) => {
var currentValue = cacheEntry.GetValue<T>();
if (currentValue.Equals(expected)) {
e.Value = value;
cacheEntry.Value = value;
wasExpectedValue = true;
if (expiresIn.HasValue)
cacheEntry.ExpiresAt = expiresAt;
}
return e;
return cacheEntry;
});

success = success && wasExpectedValue;

await StartMaintenanceAsync().AnyContext();

if (_logger.IsEnabled(LogLevel.Trace))
Expand Down
2 changes: 0 additions & 2 deletions tests/Foundatio.Tests/Caching/InMemoryCacheClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Foundatio.Caching;
using Foundatio.Utility;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading.Tasks;
using System.Threading.Tasks;
using Foundatio.Caching;
using Foundatio.Messaging;
using Microsoft.Extensions.Logging;
Expand Down
3 changes: 1 addition & 2 deletions tests/Foundatio.Tests/Serializer/JsonNetSerializerTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Foundatio.Serializer;
using Foundatio.Serializer;
using Foundatio.TestHarness.Utility;
using Microsoft.Extensions.Logging;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Exporters.Json;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Reports;
using Foundatio.Serializer;
using Foundatio.Serializer;
using Foundatio.TestHarness.Utility;
using Microsoft.Extensions.Logging;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Foundatio.Serializer;
using Foundatio.Serializer;
using Foundatio.TestHarness.Utility;
using Microsoft.Extensions.Logging;
using Xunit;
Expand Down
3 changes: 1 addition & 2 deletions tests/Foundatio.Tests/Serializer/Utf8JsonSerializerTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Foundatio.Serializer;
using Foundatio.Serializer;
using Foundatio.TestHarness.Utility;
using Microsoft.Extensions.Logging;
using Xunit;
Expand Down
5 changes: 1 addition & 4 deletions tests/Foundatio.Tests/Utility/ConnectionStringParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using Foundatio.Xunit;
using System;
using Xunit;
using Xunit.Abstractions;
using Foundatio.Utility;

namespace Foundatio.Tests.Utility {
Expand Down
3 changes: 1 addition & 2 deletions tests/Foundatio.Tests/Utility/RunTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using Foundatio.Xunit;
using Foundatio.Xunit;
using Foundatio.Utility;
using Xunit;
using Xunit.Abstractions;
Expand Down

0 comments on commit 0eea68f

Please sign in to comment.