Browse Source

CmsKit - Provide BlogFeature caching from IBlogFeatureCacheManager

pull/7745/head
enisn 6 years ago
parent
commit
61b4e1351e
  1. 14
      modules/cms-kit/src/Volo.CmsKit.Common.Application.Contracts/Volo/CmsKit/Blogs/IBlogFeatureCacheManager.cs
  2. 23
      modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureAppService.cs
  3. 46
      modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureCacheManager.cs
  4. 4
      modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Blogs/BlogFeatureChangedEto.cs

14
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<BlogFeatureDto> AddOrGetAsync(Guid blogId, string featureName, Func<Task<BlogFeatureDto>> factory);
Task ClearAsync(Guid blogId, string featureName);
}
}

23
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<BlogFeatureDto> Cache { get; }
protected IBlogFeatureCacheManager BlogFeatureCacheManager { get; }
public BlogFeatureAppService(
IBlogFeatureRepository blogFeatureRepository,
IDistributedCache<BlogFeatureDto> cache)
IBlogFeatureCacheManager blogFeatureCacheManager)
{
BlogFeatureRepository = blogFeatureRepository;
Cache = cache;
BlogFeatureCacheManager = blogFeatureCacheManager;
}
public async Task<BlogFeatureDto> GetOrDefaultAsync(Guid blogId, string featureName)
public Task<BlogFeatureDto> 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<BlogFeatureDto> GetFromDatabaseAsync(Guid blogId, string featureName)
private async Task<BlogFeatureDto> GetOrDefaultFromDatabaseAsync(Guid blogId, string featureName)
{
var feature = await BlogFeatureRepository.FindAsync(blogId, featureName);
var blogFeature = feature ?? new BlogFeature(blogId, featureName);

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

4
modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Blogs/BlogFeatureChangedEto.cs → 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
Loading…
Cancel
Save