From fd28ef3ff33b0717b6e8895cea3c6cc4956fe35a Mon Sep 17 00:00:00 2001 From: Enis Necipoglu Date: Tue, 26 Sep 2023 15:41:00 +0300 Subject: [PATCH] Refactor PagePublicAppService --- .../Public/Pages/PagePublicAppService.cs | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) 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 a72afd4055..cd0191e20d 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 @@ -33,23 +33,14 @@ public class PagePublicAppService : CmsKitPublicAppServiceBase, IPagePublicAppSe public virtual async Task FindBySlugAsync(string slug) { - var pageCacheItem = await PageCache.GetOrAddAsync(PageCacheItem.GetKey(slug), async () => - { - var page = await PageRepository.FindBySlugAsync(slug); - if (page is null) - { - return null; - } + var cachedPage = await FindAndCacheBySlugAsync(slug); - return ObjectMapper.Map(page); - }); - - if (pageCacheItem is null) + if (cachedPage == null) { return null; } - return ObjectMapper.Map(pageCacheItem); + return ObjectMapper.Map(cachedPage); } public virtual async Task FindDefaultHomePageAsync() @@ -74,8 +65,25 @@ public class PagePublicAppService : CmsKitPublicAppServiceBase, IPagePublicAppSe public virtual async Task DoesSlugExistAsync([NotNull] string slug) { - var cached = await FindBySlugAsync(slug); + var cached = await FindAndCacheBySlugAsync(slug); + + return cached != null; + } + + internal virtual async Task FindAndCacheBySlugAsync(string slug) + { + var pageCacheItem = await PageCache.GetOrAddAsync(PageCacheItem.GetKey(slug), async () => + { + var page = await PageRepository.FindBySlugAsync(slug); + // If page is not found, cache it as null to prevent further queries. + if (page is null) + { + return null; + } + + return ObjectMapper.Map(page); + }); - return cached is not null; + return pageCacheItem; } }