Skip to content

Commit 804e701

Browse files
committed
Added parameter so cache is only used when refreshing the memory cache and we are retrieving a value we know hasn't been updated since the last request.
1 parent 615e115 commit 804e701

3 files changed

Lines changed: 25 additions & 12 deletions

File tree

src/Umbraco.PublishedCache.HybridCache/Persistence/DatabaseCacheRepository.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,14 @@ private void TruncateContent()
141141
}
142142

143143
/// <inheritdoc/>
144+
[Obsolete("Please use the method overload takng all parameters. Scheduled for removal in Umbraco 19.")]
144145
public async Task<ContentCacheNode?> GetContentSourceAsync(Guid key, bool preview = false)
146+
=> await GetContentSourceAsync(key, preview, false);
147+
148+
/// <inheritdoc/>
149+
public async Task<ContentCacheNode?> GetContentSourceAsync(Guid key, bool preview = false, bool useCache = false)
145150
{
146-
ContentSourceDto? dto = await GetContentSourceDto(key);
151+
ContentSourceDto? dto = await GetContentSourceDto(key, useCache);
147152

148153
if (dto == null)
149154
{
@@ -153,16 +158,18 @@ private void TruncateContent()
153158
return CreateContentCacheNode(dto, preview);
154159
}
155160

156-
private async Task<ContentSourceDto?> GetContentSourceDto(Guid key)
161+
private async Task<ContentSourceDto?> GetContentSourceDto(Guid key, bool useRequestCache)
157162
{
158-
// Requests for a single ContentSourceDto are only made in cache refreshing contexts, so it's
159-
// reasonable to cache the result for the lifetime of the request.
160163
var cacheKey = $"{nameof(DatabaseCacheRepository)}_ContentSourceDto_{key}";
161164

162-
ContentSourceDto? dto = AppCaches.RequestCache.GetCacheItem<ContentSourceDto>(cacheKey);
163-
if (dto is not null)
165+
ContentSourceDto? dto;
166+
if (useRequestCache)
164167
{
165-
return dto;
168+
dto = AppCaches.RequestCache.GetCacheItem<ContentSourceDto>(cacheKey);
169+
if (dto is not null)
170+
{
171+
return dto;
172+
}
166173
}
167174

168175
Sql<ISqlContext>? sql = SqlContentSourcesSelect()
@@ -172,7 +179,7 @@ private void TruncateContent()
172179

173180
dto = await Database.FirstOrDefaultAsync<ContentSourceDto>(sql);
174181

175-
if (dto is not null)
182+
if (useRequestCache && dto is not null)
176183
{
177184
AppCaches.RequestCache.Set(cacheKey, dto);
178185
}

src/Umbraco.PublishedCache.HybridCache/Persistence/IDatabaseCacheRepository.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ internal interface IDatabaseCacheRepository
1616
/// <summary>
1717
/// Gets a single cache node for a document key and preview status.
1818
/// </summary>
19+
[Obsolete("Please use the method overload takng all parameters. Scheduled for removal in Umbraco 19.")]
1920
Task<ContentCacheNode?> GetContentSourceAsync(Guid key, bool preview = false);
2021

22+
/// <summary>
23+
/// Gets a single cache node for a document key and preview status.
24+
/// </summary>
25+
Task<ContentCacheNode?> GetContentSourceAsync(Guid key, bool preview = false, bool useCache = false);
26+
2127
/// <summary>
2228
/// Gets a collection of cache nodes for a collection of document keys.
2329
/// </summary>
@@ -27,7 +33,7 @@ async Task<IEnumerable<ContentCacheNode>> GetContentSourcesAsync(IEnumerable<Gui
2733
var contentCacheNodes = new List<ContentCacheNode>();
2834
foreach (Guid key in keys)
2935
{
30-
ContentCacheNode? contentSource = await GetContentSourceAsync(key, preview);
36+
ContentCacheNode? contentSource = await GetContentSourceAsync(key, preview, false);
3137
if (contentSource is not null)
3238
{
3339
contentCacheNodes.Add(contentSource);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public DocumentCacheService(
121121
async cancel =>
122122
{
123123
using ICoreScope scope = _scopeProvider.CreateCoreScope();
124-
ContentCacheNode? contentCacheNode = await _databaseCacheRepository.GetContentSourceAsync(key, preview);
124+
ContentCacheNode? contentCacheNode = await _databaseCacheRepository.GetContentSourceAsync(key, preview, false);
125125

126126
// If we can resolve the content cache node, we still need to check if the ancestor path is published.
127127
// This does cost some performance, but it's necessary to ensure that the content is actually published.
@@ -180,13 +180,13 @@ public async Task RefreshMemoryCacheAsync(Guid key)
180180
using ICoreScope scope = _scopeProvider.CreateCoreScope();
181181
scope.ReadLock(Constants.Locks.ContentTree);
182182

183-
ContentCacheNode? draftNode = await _databaseCacheRepository.GetContentSourceAsync(key, true);
183+
ContentCacheNode? draftNode = await _databaseCacheRepository.GetContentSourceAsync(key, true, true);
184184
if (draftNode is not null)
185185
{
186186
await _hybridCache.SetAsync(GetCacheKey(draftNode.Key, true), draftNode, GetEntryOptions(draftNode.Key, true), GenerateTags(key));
187187
}
188188

189-
ContentCacheNode? publishedNode = await _databaseCacheRepository.GetContentSourceAsync(key, false);
189+
ContentCacheNode? publishedNode = await _databaseCacheRepository.GetContentSourceAsync(key, false, true);
190190
if (publishedNode is not null && _publishStatusQueryService.HasPublishedAncestorPath(publishedNode.Key))
191191
{
192192
var cacheKey = GetCacheKey(publishedNode.Key, false);

0 commit comments

Comments
 (0)