Add cache invalidation

pull/17642/head
Enis Necipoglu 1 year ago
parent fd903c25db
commit f24de03fb3
No known key found for this signature in database
GPG Key ID: 1EC55E13241E1680

@ -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<EntityCreatedEventData<Page>>,
ILocalEventHandler<EntityUpdatedEventData<Page>>,
ITransientDependency
{
protected IDistributedCache<PageCacheItem, PageCacheKey> Cache { get; }
public PageChangedHandler(IDistributedCache<PageCacheItem, PageCacheKey> cache)
{
Cache = cache;
}
public Task RemoveFromCacheAsync(string slug)
{
return Cache.RemoveAsync(new PageCacheKey(slug));
}
public Task HandleEventAsync(EntityCreatedEventData<Page> eventData)
{
return RemoveFromCacheAsync(eventData.Entity.Slug);
}
public Task HandleEventAsync(EntityUpdatedEventData<Page> eventData)
{
return RemoveFromCacheAsync(eventData.Entity.Slug);
}
}

@ -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}";
}
}

@ -19,9 +19,12 @@ public class PagePublicAppService : CmsKitPublicAppServiceBase, IPagePublicAppSe
protected IPageRepository PageRepository { get; }
protected PageManager PageManager { get; }
protected IDistributedCache<PageCacheItem> PageCache { get; }
protected IDistributedCache<PageCacheItem, PageCacheKey> PageCache { get; }
public PagePublicAppService(IPageRepository pageRepository, PageManager pageManager, IDistributedCache<PageCacheItem> pageCache)
public PagePublicAppService(
IPageRepository pageRepository,
PageManager pageManager,
IDistributedCache<PageCacheItem, PageCacheKey> pageCache)
{
PageRepository = pageRepository;
PageManager = pageManager;
@ -30,7 +33,7 @@ public class PagePublicAppService : CmsKitPublicAppServiceBase, IPagePublicAppSe
public virtual async Task<PageDto> 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<PageDto> 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, PageCacheItem>(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<bool> 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}";
}
}

Loading…
Cancel
Save