Browse Source

Add option to configure entity cache and change the expiration duration and type.

pull/14055/head
Halil İbrahim Kalkan 4 years ago
parent
commit
4feaeb47f3
  1. 15
      framework/src/Volo.Abp.Caching/Volo/Abp/Caching/AbpDistributedCacheOptions.cs
  2. 5
      framework/src/Volo.Abp.Caching/Volo/Abp/Caching/CacheNameAttribute.cs
  3. 40
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs
  4. 5
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapper.cs

15
framework/src/Volo.Abp.Caching/Volo/Abp/Caching/AbpDistributedCacheOptions.cs

@ -33,4 +33,19 @@ public class AbpDistributedCacheOptions
GlobalCacheEntryOptions = new DistributedCacheEntryOptions();
KeyPrefix = "";
}
public void ConfigureCache<TCacheItem>(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);
}
}

5
framework/src/Volo.Abp.Caching/Volo/Abp/Caching/CacheNameAttribute.cs

@ -16,6 +16,11 @@ public class CacheNameAttribute : Attribute
Name = name;
}
public static string GetCacheName<TCacheItem>()
{
return GetCacheName(typeof(TCacheItem));
}
public static string GetCacheName(Type cacheItemType)
{
var cacheNameAttribute = cacheItemType

40
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<TEntity, TKey>(
this IServiceCollection services)
this IServiceCollection services,
[CanBeNull] DistributedCacheEntryOptions cacheOptions = null)
where TEntity : Entity<TKey>
{
services
@ -16,11 +21,18 @@ public static class EntityCacheServiceCollectionExtensions
>();
services
.TryAddTransient<EntityCacheWithoutCacheItem<TEntity, TKey>>();
services.Configure<AbpDistributedCacheOptions>(options =>
{
options.ConfigureCache<TEntity>(cacheOptions ?? GetDefaultCacheOptions());
});
return services;
}
public static IServiceCollection AddEntityCache<TEntity, TEntityCacheItem, TKey>(
this IServiceCollection services)
this IServiceCollection services,
[CanBeNull] DistributedCacheEntryOptions cacheOptions = null)
where TEntity : Entity<TKey>
where TEntityCacheItem : class
{
@ -31,11 +43,18 @@ public static class EntityCacheServiceCollectionExtensions
>();
services
.TryAddTransient<EntityCacheWithObjectMapper<TEntity, TEntityCacheItem, TKey>>();
services.Configure<AbpDistributedCacheOptions>(options =>
{
options.ConfigureCache<TEntityCacheItem>(cacheOptions ?? GetDefaultCacheOptions());
});
return services;
}
public static IServiceCollection AddEntityCache<TObjectMapperContext, TEntity, TEntityCacheItem, TKey>(
this IServiceCollection services)
this IServiceCollection services,
[CanBeNull] DistributedCacheEntryOptions cacheOptions = null)
where TEntity : Entity<TKey>
where TEntityCacheItem : class
{
@ -45,6 +64,19 @@ public static class EntityCacheServiceCollectionExtensions
EntityCacheWithObjectMapperContext<TObjectMapperContext, TEntity, TEntityCacheItem, TKey>
>();
services.TryAddTransient<EntityCacheWithObjectMapperContext<TObjectMapperContext, TEntity, TEntityCacheItem, TKey>>();
services.Configure<AbpDistributedCacheOptions>(options =>
{
options.ConfigureCache<TEntityCacheItem>(cacheOptions ?? GetDefaultCacheOptions());
});
return services;
}
private static DistributedCacheEntryOptions GetDefaultCacheOptions()
{
return new DistributedCacheEntryOptions {
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(2)
};
}
}

5
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapper.cs

@ -25,6 +25,11 @@ public class EntityCacheWithObjectMapper<TEntity, TEntityCacheItem, TKey> :
protected override TEntityCacheItem MapToCacheItem(TEntity entity)
{
if (entity == null)
{
return null;
}
if (typeof(TEntity) == typeof(TEntityCacheItem))
{
return entity.As<TEntityCacheItem>();

Loading…
Cancel
Save