diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Blogs/IBlogFeatureCacheManager.cs b/modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Blogs/IBlogFeatureCacheManager.cs new file mode 100644 index 0000000000..9134b67a06 --- /dev/null +++ b/modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Blogs/IBlogFeatureCacheManager.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Volo.CmsKit.Blogs +{ + public interface IBlogFeatureCacheManager + { + Task AddOrGetAsync(Guid blogId, string featureName, Func> factory); + Task ClearAsync(Guid blogId, string featureName); + } +} diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureAppService.cs b/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureAppService.cs index 32bfa9ebdc..899535600f 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureAppService.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureAppService.cs @@ -9,28 +9,27 @@ namespace Volo.CmsKit.Blogs public class BlogFeatureAppService : CmsKitAppServiceBase, IBlogFeatureAppService { protected IBlogFeatureRepository BlogFeatureRepository { get; } - protected IDistributedCache Cache { get; } + protected IBlogFeatureCacheManager BlogFeatureCacheManager { get; } public BlogFeatureAppService( IBlogFeatureRepository blogFeatureRepository, - IDistributedCache cache) + IBlogFeatureCacheManager blogFeatureCacheManager) { BlogFeatureRepository = blogFeatureRepository; - Cache = cache; + BlogFeatureCacheManager = blogFeatureCacheManager; } - public async Task GetOrDefaultAsync(Guid blogId, string featureName) + public Task GetOrDefaultAsync(Guid blogId, string featureName) { - return await Cache.GetOrAddAsync( - $"{blogId}_{featureName}", - () => GetFromDatabaseAsync(blogId, featureName), - () => new DistributedCacheEntryOptions - { - SlidingExpiration = TimeSpan.FromMinutes(1) - }); + return BlogFeatureCacheManager + .AddOrGetAsync( + blogId, + featureName, + ()=> GetOrDefaultFromDatabaseAsync(blogId, featureName) + ); } - private async Task GetFromDatabaseAsync(Guid blogId, string featureName) + private async Task GetOrDefaultFromDatabaseAsync(Guid blogId, string featureName) { var feature = await BlogFeatureRepository.FindAsync(blogId, featureName); var blogFeature = feature ?? new BlogFeature(blogId, featureName); diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureCacheManager.cs b/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureCacheManager.cs new file mode 100644 index 0000000000..353bc8dad9 --- /dev/null +++ b/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureCacheManager.cs @@ -0,0 +1,46 @@ +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 Cache { get; } + + public BlogFeatureCacheManager(IDistributedCache cache) + { + Cache = cache; + } + + public async Task AddOrGetAsync(Guid blogId, string featureName, Func> 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}"; + } + } +} diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureChangedEto.cs b/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Blogs/BlogFeatureChangedEto.cs similarity index 75% rename from modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureChangedEto.cs rename to modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Blogs/BlogFeatureChangedEto.cs index e03b63cf34..954c851d04 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureChangedEto.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Blogs/BlogFeatureChangedEto.cs @@ -1,4 +1,8 @@ using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; using Volo.Abp.EventBus; namespace Volo.CmsKit.Blogs