Skip to content

Commit

Permalink
Use a different cache key for ContentInfo in ContentService. Fixes #2041
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjwwhite committed Jan 11, 2024
1 parent 1866a0e commit eecceed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
19 changes: 14 additions & 5 deletions core/Piranha/Services/ContentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,13 @@ public async Task<T> GetByIdAsync<T>(Guid id, Guid? languageId = null) where T :
}

// First, try to get the model from cache
if (!typeof(DynamicContent).IsAssignableFrom(typeof(T)))
if (typeof(T) == typeof(ContentInfo))
{
model = _cache?.Get<GenericContent>($"{ languageId }_{ id }");
model = _cache?.Get<GenericContent>($"ContentInfo_{ languageId }_{ id }");
}
else if (!typeof(DynamicContent).IsAssignableFrom(typeof(T)))
{
model = _cache?.Get<GenericContent>($"Content_{ languageId }_{ id }");
}

// If we have a model, let's initialize it
Expand Down Expand Up @@ -339,9 +343,13 @@ private async Task OnLoadAsync(GenericContent model, Guid languageId)
if (_cache != null)
{
// Store the model
if (model is not IDynamicContent)
if (model is ContentInfo)
{
_cache.Set($"ContentInfo_{ languageId }_{ model.Id }", model);
}
else if (model is not IDynamicContent)
{
_cache.Set($"{ languageId }_{ model.Id }", model);
_cache.Set($"Content_{ languageId }_{ model.Id }", model);
}
}
}
Expand All @@ -357,7 +365,8 @@ private Task RemoveFromCacheAsync(GenericContent model, Guid languageId)
{
if (_cache != null)
{
_cache.Remove($"{ languageId }_{ model.Id }");
_cache.Remove($"ContentInfo_{ languageId }_{ model.Id }");
_cache.Remove($"Content_{ languageId }_{ model.Id }");
}
});
}
Expand Down
6 changes: 6 additions & 0 deletions test/Piranha.Tests/Services/ContentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,12 @@ public async Task GetById()
using (var api = CreateApi())
{
var content = await api.Content.GetByIdAsync<MyContent>(ID_1);
var contentInfo = await api.Content.GetByIdAsync<ContentInfo>(ID_1);

Assert.NotNull(content);
Assert.NotNull(contentInfo);
Assert.Equal("My first content", content.Title);
Assert.Equal(content.Title, contentInfo.Title);
}
}

Expand All @@ -153,9 +156,12 @@ public async Task GetTranslationById()
using (var api = CreateApi())
{
var content = await api.Content.GetByIdAsync<MyContent>(ID_1, ID_LANG);
var contentInfo = await api.Content.GetByIdAsync<ContentInfo>(ID_1, ID_LANG);

Assert.NotNull(content);
Assert.NotNull(contentInfo);
Assert.Equal("Mitt första innehåll", content.Title);
Assert.Equal(content.Title, contentInfo.Title);
}
}

Expand Down

0 comments on commit eecceed

Please sign in to comment.