From 0040569fb2f501281af46ef33de7eeb3d8f220f2 Mon Sep 17 00:00:00 2001 From: EngincanV Date: Wed, 7 Apr 2021 17:54:58 +0300 Subject: [PATCH] Blogging: Publish PostChangedEvent for cache invalidation --- .../Volo/Blogging/Posts/PostAppService.cs | 20 +++++++++++++++++-- .../Blogging/Posts/PostCacheInvalidator.cs | 8 +++----- .../Volo/Blogging/Posts/PostChangedEvent.cs | 9 +++++++++ 3 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/PostChangedEvent.cs diff --git a/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs b/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs index d804e7a004..7b42d25f84 100644 --- a/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs +++ b/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.Caching.Distributed; using Volo.Abp.Caching; +using Volo.Abp.EventBus.Local; using Volo.Blogging.Comments; using Volo.Blogging.Tagging; using Volo.Blogging.Tagging.Dtos; @@ -21,13 +22,15 @@ namespace Volo.Blogging.Posts private readonly ITagRepository _tagRepository; private readonly ICommentRepository _commentRepository; private readonly IDistributedCache> _postsCache; + private readonly ILocalEventBus _localEventBus; public PostAppService( IPostRepository postRepository, ITagRepository tagRepository, ICommentRepository commentRepository, IBlogUserLookupService userLookupService, - IDistributedCache> postsCache + IDistributedCache> postsCache, + ILocalEventBus localEventBus ) { UserLookupService = userLookupService; @@ -35,6 +38,7 @@ namespace Volo.Blogging.Posts _tagRepository = tagRepository; _commentRepository = commentRepository; _postsCache = postsCache; + _localEventBus = localEventBus; } public async Task> GetListByBlogIdAndTagName(Guid id, string tagName) @@ -84,7 +88,7 @@ namespace Volo.Blogging.Posts async () => await GetTimeOrderedPostsAsync(blogId), () => new DistributedCacheEntryOptions { - AbsoluteExpiration = DateTimeOffset.Now.AddHours(6) + AbsoluteExpiration = DateTimeOffset.Now.AddHours(1) } ); @@ -155,6 +159,7 @@ namespace Volo.Blogging.Posts await _commentRepository.DeleteOfPost(id); await _postRepository.DeleteAsync(id); + await PublishPostChangedEventAsync(post.BlogId); } [Authorize(BloggingPermissions.Posts.Update)] @@ -176,6 +181,7 @@ namespace Volo.Blogging.Posts var tagList = SplitTags(input.Tags); await SaveTags(tagList, post); + await PublishPostChangedEventAsync(post.BlogId); return ObjectMapper.Map(post); } @@ -201,6 +207,7 @@ namespace Volo.Blogging.Posts var tagList = SplitTags(input.Tags); await SaveTags(tagList, post); + await PublishPostChangedEventAsync(post.BlogId); return ObjectMapper.Map(post); } @@ -297,5 +304,14 @@ namespace Volo.Blogging.Posts return Task.FromResult(filteredPostDtos); } + + private async Task PublishPostChangedEventAsync(Guid blogId) + { + await _localEventBus.PublishAsync( + new PostChangedEvent + { + BlogId = blogId + }); + } } } diff --git a/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/PostCacheInvalidator.cs b/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/PostCacheInvalidator.cs index abbfcd8d88..e18e6de23b 100644 --- a/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/PostCacheInvalidator.cs +++ b/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/PostCacheInvalidator.cs @@ -2,12 +2,11 @@ using System.Threading.Tasks; using Volo.Abp.Caching; using Volo.Abp.DependencyInjection; -using Volo.Abp.Domain.Entities.Events; using Volo.Abp.EventBus; namespace Volo.Blogging.Posts { - public class PostCacheInvalidator : ILocalEventHandler>, ITransientDependency + public class PostCacheInvalidator : ILocalEventHandler, ITransientDependency { protected IDistributedCache> Cache { get; } @@ -16,10 +15,9 @@ namespace Volo.Blogging.Posts Cache = cache; } - public virtual async Task HandleEventAsync(EntityChangedEventData eventData) + public virtual async Task HandleEventAsync(PostChangedEvent post) { - var cacheKey = eventData.Entity.BlogId.ToString(); - await Cache.RemoveAsync(cacheKey); + await Cache.RemoveAsync(post.BlogId.ToString()); } } } \ No newline at end of file diff --git a/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/PostChangedEvent.cs b/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/PostChangedEvent.cs new file mode 100644 index 0000000000..c32ed92224 --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/PostChangedEvent.cs @@ -0,0 +1,9 @@ +using System; + +namespace Volo.Blogging.Posts +{ + public class PostChangedEvent + { + public Guid BlogId { get; set; } + } +} \ No newline at end of file