mirror of https://github.com/abpframework/abp.git
9 changed files with 57 additions and 82 deletions
@ -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); |
|||
} |
|||
} |
|||
@ -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; } |
|||
} |
|||
} |
|||
@ -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}"; |
|||
} |
|||
} |
|||
} |
|||
@ -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}"; |
|||
} |
|||
} |
|||
} |
|||
@ -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)); |
|||
} |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue