From 4feaeb47f37dd340134e22022d95a8e831ba6279 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 4 Oct 2022 08:50:45 +0300 Subject: [PATCH] Add option to configure entity cache and change the expiration duration and type. --- .../Abp/Caching/AbpDistributedCacheOptions.cs | 15 +++++++ .../Volo/Abp/Caching/CacheNameAttribute.cs | 5 +++ .../EntityCacheServiceCollectionExtensions.cs | 40 +++++++++++++++++-- .../Caching/EntityCacheWithObjectMapper.cs | 5 +++ 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/AbpDistributedCacheOptions.cs b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/AbpDistributedCacheOptions.cs index 0bd48abefb..26b2ef506b 100644 --- a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/AbpDistributedCacheOptions.cs +++ b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/AbpDistributedCacheOptions.cs @@ -33,4 +33,19 @@ public class AbpDistributedCacheOptions GlobalCacheEntryOptions = new DistributedCacheEntryOptions(); KeyPrefix = ""; } + + public void ConfigureCache(DistributedCacheEntryOptions options) + { + ConfigureCache(typeof(TCacheItem), options); + } + + public void ConfigureCache(Type cacheItemType, DistributedCacheEntryOptions options) + { + ConfigureCache(CacheNameAttribute.GetCacheName(cacheItemType), options); + } + + public void ConfigureCache(string cacheName, DistributedCacheEntryOptions options) + { + CacheConfigurators.Add(name => cacheName != name ? null : options); + } } diff --git a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/CacheNameAttribute.cs b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/CacheNameAttribute.cs index 1b4ac97a70..ff162921c9 100644 --- a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/CacheNameAttribute.cs +++ b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/CacheNameAttribute.cs @@ -16,6 +16,11 @@ public class CacheNameAttribute : Attribute Name = name; } + public static string GetCacheName() + { + return GetCacheName(typeof(TCacheItem)); + } + public static string GetCacheName(Type cacheItemType) { var cacheNameAttribute = cacheItemType diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs index 8136042690..cb7e0e5ee7 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs @@ -1,12 +1,17 @@ -using Microsoft.Extensions.DependencyInjection; +using System; +using JetBrains.Annotations; +using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; +using Volo.Abp.Caching; namespace Volo.Abp.Domain.Entities.Caching; public static class EntityCacheServiceCollectionExtensions { public static IServiceCollection AddEntityCache( - this IServiceCollection services) + this IServiceCollection services, + [CanBeNull] DistributedCacheEntryOptions cacheOptions = null) where TEntity : Entity { services @@ -16,11 +21,18 @@ public static class EntityCacheServiceCollectionExtensions >(); services .TryAddTransient>(); + + services.Configure(options => + { + options.ConfigureCache(cacheOptions ?? GetDefaultCacheOptions()); + }); + return services; } public static IServiceCollection AddEntityCache( - this IServiceCollection services) + this IServiceCollection services, + [CanBeNull] DistributedCacheEntryOptions cacheOptions = null) where TEntity : Entity where TEntityCacheItem : class { @@ -31,11 +43,18 @@ public static class EntityCacheServiceCollectionExtensions >(); services .TryAddTransient>(); + + services.Configure(options => + { + options.ConfigureCache(cacheOptions ?? GetDefaultCacheOptions()); + }); + return services; } public static IServiceCollection AddEntityCache( - this IServiceCollection services) + this IServiceCollection services, + [CanBeNull] DistributedCacheEntryOptions cacheOptions = null) where TEntity : Entity where TEntityCacheItem : class { @@ -45,6 +64,19 @@ public static class EntityCacheServiceCollectionExtensions EntityCacheWithObjectMapperContext >(); services.TryAddTransient>(); + + services.Configure(options => + { + options.ConfigureCache(cacheOptions ?? GetDefaultCacheOptions()); + }); + return services; } + + private static DistributedCacheEntryOptions GetDefaultCacheOptions() + { + return new DistributedCacheEntryOptions { + AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(2) + }; + } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapper.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapper.cs index bcc2157761..0a916c9594 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapper.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapper.cs @@ -25,6 +25,11 @@ public class EntityCacheWithObjectMapper : protected override TEntityCacheItem MapToCacheItem(TEntity entity) { + if (entity == null) + { + return null; + } + if (typeof(TEntity) == typeof(TEntityCacheItem)) { return entity.As();