Browse Source

CmsKit - Refactoring on BlogFeature Caching

pull/7845/head
enisn 6 years ago
parent
commit
71996b6165
  1. 2
      modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/CmsKitAdminApplicationAutoMapperProfile.cs
  2. 14
      modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Blogs/IBlogFeatureCacheManager.cs
  3. 24
      modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureAppService.cs
  4. 11
      modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureCacheItem.cs
  5. 24
      modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureCacheKey.cs
  6. 46
      modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureCacheManager.cs
  7. 11
      modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureChangedHandler.cs
  8. 5
      modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/CmsKitCommonApplicationAutoMapperProfile.cs
  9. 2
      modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/PublicApplicationAutoMapperProfile.cs

2
modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/CmsKitAdminApplicationAutoMapperProfile.cs

@ -40,8 +40,6 @@ namespace Volo.CmsKit.Admin
CreateMap<Blog, BlogDto>(MemberList.Destination)
.ReverseMap();
CreateMap<BlogFeature, BlogFeatureDto>();
CreateMap<TagEntityTypeDefiniton, TagDefinitionDto>(MemberList.Destination);
CreateMap<MediaDescriptor, MediaDescriptorDto>();

14
modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Blogs/IBlogFeatureCacheManager.cs

@ -1,14 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Volo.CmsKit.Blogs
{
public interface IBlogFeatureCacheManager
{
Task<BlogFeatureDto> AddOrGetAsync(Guid blogId, string featureName, Func<Task<BlogFeatureDto>> factory);
Task ClearAsync(Guid blogId, string featureName);
}
}

24
modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureAppService.cs

@ -2,7 +2,6 @@
using System;
using System.Threading.Tasks;
using Volo.Abp.Caching;
using Volo.Abp.EventBus.Distributed;
namespace Volo.CmsKit.Blogs
{
@ -10,32 +9,31 @@ namespace Volo.CmsKit.Blogs
{
protected virtual IBlogFeatureRepository BlogFeatureRepository { get; }
protected virtual IBlogFeatureCacheManager BlogFeatureCacheManager { get; }
protected virtual IDistributedCache<BlogFeatureCacheItem, BlogFeatureCacheKey> Cache { get; }
public BlogFeatureAppService(
IBlogFeatureRepository blogFeatureRepository,
IBlogFeatureCacheManager blogFeatureCacheManager)
IDistributedCache<BlogFeatureCacheItem, BlogFeatureCacheKey> blogFeatureCacheManager)
{
BlogFeatureRepository = blogFeatureRepository;
BlogFeatureCacheManager = blogFeatureCacheManager;
Cache = blogFeatureCacheManager;
}
public virtual Task<BlogFeatureDto> GetOrDefaultAsync(Guid blogId, string featureName)
public virtual async Task<BlogFeatureDto> GetOrDefaultAsync(Guid blogId, string featureName)
{
return BlogFeatureCacheManager
.AddOrGetAsync(
blogId,
featureName,
()=> GetOrDefaultFroRepositoryAsync(blogId, featureName)
);
var cacheItem = await Cache.GetOrAddAsync(
new BlogFeatureCacheKey(blogId, featureName),
()=> GetOrDefaultFroRepositoryAsync(blogId, featureName));
return ObjectMapper.Map<BlogFeatureCacheItem, BlogFeatureDto>(cacheItem);
}
protected virtual async Task<BlogFeatureDto> GetOrDefaultFroRepositoryAsync(Guid blogId, string featureName)
protected virtual async Task<BlogFeatureCacheItem> GetOrDefaultFroRepositoryAsync(Guid blogId, string featureName)
{
var feature = await BlogFeatureRepository.FindAsync(blogId, featureName);
var blogFeature = feature ?? new BlogFeature(blogId, featureName);
return ObjectMapper.Map<BlogFeature, BlogFeatureDto>(blogFeature);
return ObjectMapper.Map<BlogFeature, BlogFeatureCacheItem>(blogFeature);
}
}
}

11
modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureCacheItem.cs

@ -0,0 +1,11 @@
using System;
namespace Volo.CmsKit.Blogs
{
public class BlogFeatureCacheItem
{
public Guid Id { get; set; }
public string FeatureName { get; set; }
public bool IsEnabled { get; set; }
}
}

24
modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureCacheKey.cs

@ -0,0 +1,24 @@
using JetBrains.Annotations;
using System;
using Volo.Abp;
namespace Volo.CmsKit.Blogs
{
public class BlogFeatureCacheKey
{
public BlogFeatureCacheKey(Guid id, [NotNull] string featureName)
{
Id = id;
FeatureName = Check.NotNullOrEmpty(featureName, nameof(featureName));
}
public Guid Id { get; set; }
public string FeatureName { get; set; }
public override string ToString()
{
return $"BlogFeature_{Id}_{FeatureName}";
}
}
}

46
modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureCacheManager.cs

@ -1,46 +0,0 @@
using JetBrains.Annotations;
using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
namespace Volo.CmsKit.Blogs
{
public class BlogFeatureCacheManager : IBlogFeatureCacheManager, ITransientDependency
{
protected IDistributedCache<BlogFeatureDto> Cache { get; }
public BlogFeatureCacheManager(IDistributedCache<BlogFeatureDto> cache)
{
Cache = cache;
}
public async Task<BlogFeatureDto> AddOrGetAsync(Guid blogId, string featureName, Func<Task<BlogFeatureDto>> factory)
{
return await Cache.GetOrAddAsync(
GetCacheKey(blogId, featureName),
factory,
() => new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1)
});
}
public Task ClearAsync(Guid blogId, string featureName)
{
return Cache.RemoveAsync(GetCacheKey(blogId, featureName));
}
private string GetCacheKey(Guid blogId, [NotNull] string featureName)
{
Check.NotNullOrWhiteSpace(featureName, nameof(featureName));
return $"{blogId}_{featureName}";
}
}
}

11
modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureChangedHandler.cs

@ -1,22 +1,23 @@
using System.Threading.Tasks;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus;
using Volo.Abp.EventBus.Distributed;
namespace Volo.CmsKit.Blogs
{
public class BlogFeatureChangedHandler : ILocalEventHandler<BlogFeatureChangedEto>, ITransientDependency
{
protected IBlogFeatureCacheManager CacheManager { get; }
protected IDistributedCache<BlogFeatureCacheItem, BlogFeatureCacheKey> Cache { get; }
public BlogFeatureChangedHandler(IBlogFeatureCacheManager cacheManager)
public BlogFeatureChangedHandler(
IDistributedCache<BlogFeatureCacheItem, BlogFeatureCacheKey> cache)
{
CacheManager = cacheManager;
Cache = cache;
}
public async Task HandleEventAsync(BlogFeatureChangedEto eventData)
{
await CacheManager.ClearAsync(eventData.BlogId, eventData.FeatureName);
await Cache.RemoveAsync(new BlogFeatureCacheKey(eventData.BlogId, eventData.FeatureName));
}
}
}

5
modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/CmsKitCommonApplicationAutoMapperProfile.cs

@ -1,4 +1,5 @@
using AutoMapper;
using Volo.CmsKit.Blogs;
using Volo.CmsKit.Contents;
using Volo.CmsKit.Pages;
using Volo.CmsKit.Tags;
@ -13,6 +14,10 @@ namespace Volo.CmsKit
CreateMap<Tag, TagDto>();
CreateMap<CmsUser, CmsUserDto>();
CreateMap<BlogFeature, BlogFeatureCacheItem>();
CreateMap<BlogFeatureCacheItem, BlogFeatureDto>().ReverseMap();
}
}
}

2
modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/PublicApplicationAutoMapperProfile.cs

@ -34,8 +34,6 @@ namespace Volo.CmsKit.Public
CreateMap<Page, PageDto>();
CreateMap<BlogPost, BlogPostPublicDto>(MemberList.None);
CreateMap<BlogFeature, BlogFeatureDto>();
}
}
}

Loading…
Cancel
Save