From fd903c25db9b5e350907b285be993f315dc4230c Mon Sep 17 00:00:00 2001 From: Enis Necipoglu Date: Fri, 15 Sep 2023 14:30:19 +0300 Subject: [PATCH] Much better caching for pages --- .../Volo/CmsKit/Pages/PageConsts.cs | 2 +- .../Public/Pages/PagePublicAppService.cs | 35 +++++++++++++++---- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Pages/PageConsts.cs b/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Pages/PageConsts.cs index 1579a0e570..f703fbac43 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Pages/PageConsts.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Pages/PageConsts.cs @@ -16,5 +16,5 @@ public static class PageConsts public static int MaxStyleLength { get; set; } = int.MaxValue; - public static string DefaultHomePageCacheKey { get; set; } = "CmsKit_DefaultHomePage"; + public static string DefaultHomePageCacheKey { get; set; } = "__DefaultHomePage"; } diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Pages/PagePublicAppService.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Pages/PagePublicAppService.cs index 85be369bb1..0214d7f901 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Pages/PagePublicAppService.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Pages/PagePublicAppService.cs @@ -17,7 +17,7 @@ namespace Volo.CmsKit.Public.Pages; public class PagePublicAppService : CmsKitPublicAppServiceBase, IPagePublicAppService { protected IPageRepository PageRepository { get; } - protected PageManager PageManager { get; } + protected PageManager PageManager { get; } protected IDistributedCache PageCache { get; } @@ -25,18 +25,28 @@ public class PagePublicAppService : CmsKitPublicAppServiceBase, IPagePublicAppSe { PageRepository = pageRepository; PageManager = pageManager; - PageCache = pageCache; + PageCache = pageCache; } public virtual async Task FindBySlugAsync(string slug) { - var page = await PageRepository.FindBySlugAsync(slug); - if (page == null) + var pageCacheItem = await PageCache.GetOrAddAsync(CalculatePageCacheKey(slug), async () => + { + var page = await PageRepository.GetBySlugAsync(slug); + if (page is null) + { + return null; + } + + return ObjectMapper.Map(page); + }); + + if (pageCacheItem is null) { return null; } - return ObjectMapper.Map(page); + return ObjectMapper.Map(pageCacheItem); } public virtual async Task FindDefaultHomePageAsync() @@ -59,8 +69,19 @@ public class PagePublicAppService : CmsKitPublicAppServiceBase, IPagePublicAppSe return ObjectMapper.Map(pageCacheItem); } - public Task DoesSlugExistAsync([NotNull] string slug) + public async Task DoesSlugExistAsync([NotNull] string slug) + { + var cached = await PageCache.GetAsync(CalculatePageCacheKey(slug)); + if (cached is not null) + { + return true; + } + + return await PageRepository.ExistsAsync(slug); + } + + protected virtual string CalculatePageCacheKey([NotNull] string slug) { - return PageRepository.ExistsAsync(slug); + return $"Page_{slug}"; } }