Browse Source

Blogging: Publish PostChangedEvent for cache invalidation

pull/8491/head
EngincanV 5 years ago
parent
commit
0040569fb2
  1. 20
      modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs
  2. 8
      modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/PostCacheInvalidator.cs
  3. 9
      modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/PostChangedEvent.cs

20
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<List<PostCacheItem>> _postsCache;
private readonly ILocalEventBus _localEventBus;
public PostAppService(
IPostRepository postRepository,
ITagRepository tagRepository,
ICommentRepository commentRepository,
IBlogUserLookupService userLookupService,
IDistributedCache<List<PostCacheItem>> postsCache
IDistributedCache<List<PostCacheItem>> postsCache,
ILocalEventBus localEventBus
)
{
UserLookupService = userLookupService;
@ -35,6 +38,7 @@ namespace Volo.Blogging.Posts
_tagRepository = tagRepository;
_commentRepository = commentRepository;
_postsCache = postsCache;
_localEventBus = localEventBus;
}
public async Task<ListResultDto<PostWithDetailsDto>> 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, PostWithDetailsDto>(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, PostWithDetailsDto>(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
});
}
}
}

8
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<EntityChangedEventData<Post>>, ITransientDependency
public class PostCacheInvalidator : ILocalEventHandler<PostChangedEvent>, ITransientDependency
{
protected IDistributedCache<List<PostCacheItem>> Cache { get; }
@ -16,10 +15,9 @@ namespace Volo.Blogging.Posts
Cache = cache;
}
public virtual async Task HandleEventAsync(EntityChangedEventData<Post> eventData)
public virtual async Task HandleEventAsync(PostChangedEvent post)
{
var cacheKey = eventData.Entity.BlogId.ToString();
await Cache.RemoveAsync(cacheKey);
await Cache.RemoveAsync(post.BlogId.ToString());
}
}
}

9
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; }
}
}
Loading…
Cancel
Save