Skip to content

Commit 184c17e

Browse files
authored
Hybrid cache: Check for ContentCacheNode instead of object on exists for hybrid cache to ensure correct deserialization (closes #20352) (#20383)
Checked for ContentCacheNode instead of object on exists for hybrid cache to ensure correct deserialization.
1 parent 4330a99 commit 184c17e

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

src/Umbraco.PublishedCache.HybridCache/Extensions/HybridCacheExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ internal static class HybridCacheExtensions
1717
/// Hat-tip: https://github.com/dotnet/aspnetcore/discussions/57191
1818
/// Will never add or alter the state of any items in the cache.
1919
/// </remarks>
20-
public static async Task<bool> ExistsAsync(this Microsoft.Extensions.Caching.Hybrid.HybridCache cache, string key)
20+
public static async Task<bool> ExistsAsync<T>(this Microsoft.Extensions.Caching.Hybrid.HybridCache cache, string key)
2121
{
22-
(bool exists, _) = await TryGetValueAsync<object>(cache, key);
22+
(bool exists, _) = await TryGetValueAsync<T>(cache, key);
2323
return exists;
2424
}
2525

src/Umbraco.PublishedCache.HybridCache/Services/DocumentCacheService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public async Task SeedAsync(CancellationToken cancellationToken)
205205

206206
var cacheKey = GetCacheKey(key, false);
207207

208-
var existsInCache = await _hybridCache.ExistsAsync(cacheKey);
208+
var existsInCache = await _hybridCache.ExistsAsync<ContentCacheNode>(cacheKey);
209209
if (existsInCache is false)
210210
{
211211
uncachedKeys.Add(key);
@@ -278,7 +278,7 @@ public async Task<bool> HasContentByIdAsync(int id, bool preview = false)
278278
return false;
279279
}
280280

281-
return await _hybridCache.ExistsAsync(GetCacheKey(keyAttempt.Result, preview));
281+
return await _hybridCache.ExistsAsync<ContentCacheNode>(GetCacheKey(keyAttempt.Result, preview));
282282
}
283283

284284
public async Task RefreshContentAsync(IContent content)

src/Umbraco.PublishedCache.HybridCache/Services/MediaCacheService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public async Task<bool> HasContentByIdAsync(int id)
133133
return false;
134134
}
135135

136-
return await _hybridCache.ExistsAsync($"{keyAttempt.Result}");
136+
return await _hybridCache.ExistsAsync<ContentCacheNode>($"{keyAttempt.Result}");
137137
}
138138

139139
public async Task RefreshMediaAsync(IMedia media)
@@ -170,7 +170,7 @@ public async Task SeedAsync(CancellationToken cancellationToken)
170170

171171
var cacheKey = GetCacheKey(key, false);
172172

173-
var existsInCache = await _hybridCache.ExistsAsync(cacheKey);
173+
var existsInCache = await _hybridCache.ExistsAsync<ContentCacheNode>(cacheKey);
174174
if (existsInCache is false)
175175
{
176176
uncachedKeys.Add(key);

tests/Umbraco.Tests.UnitTests/Umbraco.PublishedCache.HybridCache/Extensions/HybridCacheExtensionsTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Extensions.Caching.Hybrid;
22
using Moq;
33
using NUnit.Framework;
4+
using Umbraco.Cms.Infrastructure.HybridCache;
45
using Umbraco.Cms.Infrastructure.HybridCache.Extensions;
56

67
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.PublishedCache.HybridCache.Extensions;
@@ -27,20 +28,20 @@ public async Task ExistsAsync_WhenKeyExists_ShouldReturnTrue()
2728
{
2829
// Arrange
2930
string key = "test-key";
30-
var expectedValue = "test-value";
31+
var expectedValue = new ContentCacheNode { Id = 1234 };
3132

3233
_cacheMock
3334
.Setup(cache => cache.GetOrCreateAsync(
3435
key,
3536
null!,
36-
It.IsAny<Func<object, CancellationToken, ValueTask<object>>>(),
37+
It.IsAny<Func<object, CancellationToken, ValueTask<ContentCacheNode>>>(),
3738
It.IsAny<HybridCacheEntryOptions>(),
3839
null,
3940
CancellationToken.None))
4041
.ReturnsAsync(expectedValue);
4142

4243
// Act
43-
var exists = await HybridCacheExtensions.ExistsAsync(_cacheMock.Object, key);
44+
var exists = await HybridCacheExtensions.ExistsAsync<ContentCacheNode>(_cacheMock.Object, key);
4445

4546
// Assert
4647
Assert.IsTrue(exists);
@@ -56,14 +57,14 @@ public async Task ExistsAsync_WhenKeyDoesNotExist_ShouldReturnFalse()
5657
.Setup(cache => cache.GetOrCreateAsync(
5758
key,
5859
null!,
59-
It.IsAny<Func<object, CancellationToken, ValueTask<object>>>(),
60+
It.IsAny<Func<object, CancellationToken, ValueTask<ContentCacheNode>>>(),
6061
It.IsAny<HybridCacheEntryOptions>(),
6162
null,
6263
CancellationToken.None))
6364
.Returns((
6465
string key,
6566
object? state,
66-
Func<object, CancellationToken, ValueTask<object>> factory,
67+
Func<object, CancellationToken, ValueTask<ContentCacheNode>> factory,
6768
HybridCacheEntryOptions? options,
6869
IEnumerable<string>? tags,
6970
CancellationToken token) =>
@@ -72,7 +73,7 @@ public async Task ExistsAsync_WhenKeyDoesNotExist_ShouldReturnFalse()
7273
});
7374

7475
// Act
75-
var exists = await HybridCacheExtensions.ExistsAsync(_cacheMock.Object, key);
76+
var exists = await HybridCacheExtensions.ExistsAsync<ContentCacheNode>(_cacheMock.Object, key);
7677

7778
// Assert
7879
Assert.IsFalse(exists);

0 commit comments

Comments
 (0)