From f24de03fb3220898b02b42028b1d8f91364ce410 Mon Sep 17 00:00:00 2001 From: Enis Necipoglu Date: Fri, 15 Sep 2023 14:48:02 +0300 Subject: [PATCH] Add cache invalidation --- .../Volo/CmsKit/Pages/PageChangedHandler.cs | 37 +++++++++++++++++++ .../Volo/CmsKit/Pages/PageCacheKey.cs | 15 ++++++++ .../Public/Pages/PagePublicAppService.cs | 20 +++++----- 3 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Pages/PageChangedHandler.cs create mode 100644 modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Pages/PageCacheKey.cs diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Pages/PageChangedHandler.cs b/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Pages/PageChangedHandler.cs new file mode 100644 index 0000000000..70016a5451 --- /dev/null +++ b/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Pages/PageChangedHandler.cs @@ -0,0 +1,37 @@ +using System; +using System.Threading.Tasks; +using Volo.Abp.Caching; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Domain.Entities.Events; +using Volo.Abp.EventBus; + +namespace Volo.CmsKit.Pages; + +public class PageChangedHandler : + ILocalEventHandler>, + ILocalEventHandler>, + ITransientDependency + +{ + protected IDistributedCache Cache { get; } + + public PageChangedHandler(IDistributedCache cache) + { + Cache = cache; + } + + public Task RemoveFromCacheAsync(string slug) + { + return Cache.RemoveAsync(new PageCacheKey(slug)); + } + + public Task HandleEventAsync(EntityCreatedEventData eventData) + { + return RemoveFromCacheAsync(eventData.Entity.Slug); + } + + public Task HandleEventAsync(EntityUpdatedEventData eventData) + { + return RemoveFromCacheAsync(eventData.Entity.Slug); + } +} diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Pages/PageCacheKey.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Pages/PageCacheKey.cs new file mode 100644 index 0000000000..e271af9e28 --- /dev/null +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Pages/PageCacheKey.cs @@ -0,0 +1,15 @@ +namespace Volo.CmsKit.Pages; +public class PageCacheKey +{ + public PageCacheKey(string slug) + { + Slug = slug; + } + + public string Slug { get; set; } + + public override string ToString() + { + return $"Page_{Slug}"; + } +} \ No newline at end of file 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 0214d7f901..99dcfc1432 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 @@ -19,9 +19,12 @@ public class PagePublicAppService : CmsKitPublicAppServiceBase, IPagePublicAppSe protected IPageRepository PageRepository { get; } protected PageManager PageManager { get; } - protected IDistributedCache PageCache { get; } + protected IDistributedCache PageCache { get; } - public PagePublicAppService(IPageRepository pageRepository, PageManager pageManager, IDistributedCache pageCache) + public PagePublicAppService( + IPageRepository pageRepository, + PageManager pageManager, + IDistributedCache pageCache) { PageRepository = pageRepository; PageManager = pageManager; @@ -30,7 +33,7 @@ public class PagePublicAppService : CmsKitPublicAppServiceBase, IPagePublicAppSe public virtual async Task FindBySlugAsync(string slug) { - var pageCacheItem = await PageCache.GetOrAddAsync(CalculatePageCacheKey(slug), async () => + var pageCacheItem = await PageCache.GetOrAddAsync(new PageCacheKey(slug), async () => { var page = await PageRepository.GetBySlugAsync(slug); if (page is null) @@ -51,7 +54,7 @@ public class PagePublicAppService : CmsKitPublicAppServiceBase, IPagePublicAppSe public virtual async Task FindDefaultHomePageAsync() { - var pageCacheItem = await PageCache.GetAsync(PageConsts.DefaultHomePageCacheKey); + var pageCacheItem = await PageCache.GetAsync(new PageCacheKey(PageConsts.DefaultHomePageCacheKey)); if (pageCacheItem is null) { var page = await PageManager.GetHomePageAsync(); @@ -62,7 +65,7 @@ public class PagePublicAppService : CmsKitPublicAppServiceBase, IPagePublicAppSe pageCacheItem = ObjectMapper.Map(page); - await PageCache.SetAsync(PageConsts.DefaultHomePageCacheKey, pageCacheItem, + await PageCache.SetAsync(new PageCacheKey(PageConsts.DefaultHomePageCacheKey), pageCacheItem, new DistributedCacheEntryOptions { AbsoluteExpiration = DateTimeOffset.Now.AddHours(1) }); } @@ -71,7 +74,7 @@ public class PagePublicAppService : CmsKitPublicAppServiceBase, IPagePublicAppSe public async Task DoesSlugExistAsync([NotNull] string slug) { - var cached = await PageCache.GetAsync(CalculatePageCacheKey(slug)); + var cached = await PageCache.GetAsync(new PageCacheKey(slug)); if (cached is not null) { return true; @@ -79,9 +82,4 @@ public class PagePublicAppService : CmsKitPublicAppServiceBase, IPagePublicAppSe return await PageRepository.ExistsAsync(slug); } - - protected virtual string CalculatePageCacheKey([NotNull] string slug) - { - return $"Page_{slug}"; - } }