From eecceed12ed3237ed8b4415f28965aa41558f7fc Mon Sep 17 00:00:00 2001 From: Andy White Date: Thu, 11 Jan 2024 10:46:30 +0000 Subject: [PATCH] Use a different cache key for ContentInfo in ContentService. Fixes #2041 --- core/Piranha/Services/ContentService.cs | 19 ++++++++++++++----- test/Piranha.Tests/Services/ContentTests.cs | 6 ++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/core/Piranha/Services/ContentService.cs b/core/Piranha/Services/ContentService.cs index 477e92d31..0e0f6f968 100644 --- a/core/Piranha/Services/ContentService.cs +++ b/core/Piranha/Services/ContentService.cs @@ -132,9 +132,13 @@ public async Task GetByIdAsync(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($"{ languageId }_{ id }"); + model = _cache?.Get($"ContentInfo_{ languageId }_{ id }"); + } + else if (!typeof(DynamicContent).IsAssignableFrom(typeof(T))) + { + model = _cache?.Get($"Content_{ languageId }_{ id }"); } // If we have a model, let's initialize it @@ -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); } } } @@ -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 }"); } }); } diff --git a/test/Piranha.Tests/Services/ContentTests.cs b/test/Piranha.Tests/Services/ContentTests.cs index bbe4eaa0b..642465a4a 100644 --- a/test/Piranha.Tests/Services/ContentTests.cs +++ b/test/Piranha.Tests/Services/ContentTests.cs @@ -141,9 +141,12 @@ public async Task GetById() using (var api = CreateApi()) { var content = await api.Content.GetByIdAsync(ID_1); + var contentInfo = await api.Content.GetByIdAsync(ID_1); Assert.NotNull(content); + Assert.NotNull(contentInfo); Assert.Equal("My first content", content.Title); + Assert.Equal(content.Title, contentInfo.Title); } } @@ -153,9 +156,12 @@ public async Task GetTranslationById() using (var api = CreateApi()) { var content = await api.Content.GetByIdAsync(ID_1, ID_LANG); + var contentInfo = await api.Content.GetByIdAsync(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); } }