From 4530b445b58a76eabdcf401779a790400e2e280c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Sun, 18 Sep 2022 00:42:05 +0300 Subject: [PATCH] Introduce IEntityCache and its default implementations. --- .../Entities/Caching/EntityCacheBase.cs | 40 ++++++++++++++++++ .../EntityCacheServiceCollectionExtensions.cs | 41 +++++++++++++++++++ .../Caching/EntityCacheWithObjectMapper.cs | 35 ++++++++++++++++ .../EntityCacheWithObjectMapperContext.cs | 22 ++++++++++ .../Caching/EntityCacheWithoutCacheItem.cs | 24 +++++++++++ .../Domain/Entities/Caching/IEntityCache.cs | 22 ++++++++++ 6 files changed, 184 insertions(+) create mode 100644 framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheBase.cs create mode 100644 framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs create mode 100644 framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapper.cs create mode 100644 framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapperContext.cs create mode 100644 framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithoutCacheItem.cs create mode 100644 framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/IEntityCache.cs diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheBase.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheBase.cs new file mode 100644 index 0000000000..1d641ea953 --- /dev/null +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheBase.cs @@ -0,0 +1,40 @@ +using System.Threading.Tasks; +using Volo.Abp.Caching; +using Volo.Abp.Domain.Repositories; + +namespace Volo.Abp.Domain.Entities.Caching; + +public abstract class EntityCacheBase : + IEntityCache + where TEntity : Entity + where TEntityCacheItem : class +{ + protected IReadOnlyRepository Repository { get; } + protected IDistributedCache Cache { get; } + + protected EntityCacheBase( + IReadOnlyRepository repository, + IDistributedCache cache) + { + Repository = repository; + Cache = cache; + } + + public virtual async Task FindAsync(TKey id) + { + return await Cache.GetOrAddAsync( + id, + async () => MapToCacheItem(await Repository.FindAsync(id)) + ); + } + + public virtual async Task GetAsync(TKey id) + { + return await Cache.GetOrAddAsync( + id, + async () => MapToCacheItem(await Repository.FindAsync(id)) + ); + } + + protected abstract TEntityCacheItem MapToCacheItem(TEntity entity); +} \ No newline at end of file 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 new file mode 100644 index 0000000000..6bded3c401 --- /dev/null +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs @@ -0,0 +1,41 @@ +using Microsoft.Extensions.DependencyInjection; + +namespace Volo.Abp.Domain.Entities.Caching; + +public static class EntityCacheServiceCollectionExtensions +{ + public static IServiceCollection AddEntityCache( + this IServiceCollection services) + where TEntity : Entity + { + return services + .AddTransient< + IEntityCache, + EntityCacheWithoutCacheItem + >(); + } + + public static IServiceCollection AddEntityCache( + this IServiceCollection services) + where TEntity : Entity + where TEntityCacheItem : class + { + return services + .AddTransient< + IEntityCache, + EntityCacheWithObjectMapper + >(); + } + + public static IServiceCollection AddEntityCache( + this IServiceCollection services) + where TEntity : Entity + where TEntityCacheItem : class + { + return services + .AddTransient< + IEntityCache, + EntityCacheWithObjectMapperContext + >(); + } +} \ 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 new file mode 100644 index 0000000000..bcc2157761 --- /dev/null +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapper.cs @@ -0,0 +1,35 @@ +using System; +using Volo.Abp.Caching; +using Volo.Abp.Domain.Repositories; +using Volo.Abp.ObjectMapping; + +namespace Volo.Abp.Domain.Entities.Caching; + +public class EntityCacheWithObjectMapper : + EntityCacheBase + where TEntity : Entity + where TEntityCacheItem : class +{ + protected IObjectMapper ObjectMapper { get; } + + public EntityCacheWithObjectMapper( + IReadOnlyRepository repository, + IDistributedCache cache, + IObjectMapper objectMapper) + : base( + repository, + cache) + { + ObjectMapper = objectMapper; + } + + protected override TEntityCacheItem MapToCacheItem(TEntity entity) + { + if (typeof(TEntity) == typeof(TEntityCacheItem)) + { + return entity.As(); + } + + return ObjectMapper.Map(entity); + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapperContext.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapperContext.cs new file mode 100644 index 0000000000..e5eba5781c --- /dev/null +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapperContext.cs @@ -0,0 +1,22 @@ +using Volo.Abp.Caching; +using Volo.Abp.Domain.Repositories; +using Volo.Abp.ObjectMapping; + +namespace Volo.Abp.Domain.Entities.Caching; + +public class EntityCacheWithObjectMapperContext : + EntityCacheWithObjectMapper + where TEntity : Entity + where TEntityCacheItem : class +{ + public EntityCacheWithObjectMapperContext( + IReadOnlyRepository repository, + IDistributedCache cache, + IObjectMapper objectMapper) // Intentionally injected with TContext + : base( + repository, + cache, + objectMapper) + { + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithoutCacheItem.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithoutCacheItem.cs new file mode 100644 index 0000000000..33af591556 --- /dev/null +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithoutCacheItem.cs @@ -0,0 +1,24 @@ +using Volo.Abp.Caching; +using Volo.Abp.Domain.Repositories; + +namespace Volo.Abp.Domain.Entities.Caching; + +public class EntityCacheWithoutCacheItem : + EntityCacheBase + where TEntity : Entity +{ + public EntityCacheWithoutCacheItem( + IReadOnlyRepository repository, + IDistributedCache cache) + : base( + repository, + cache) + { + + } + + protected override TEntity MapToCacheItem(TEntity entity) + { + return entity; + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/IEntityCache.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/IEntityCache.cs new file mode 100644 index 0000000000..84f4486c65 --- /dev/null +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/IEntityCache.cs @@ -0,0 +1,22 @@ +using System.Threading.Tasks; +using JetBrains.Annotations; + +namespace Volo.Abp.Domain.Entities.Caching; + +public interface IEntityCache + where TEntityCacheItem : class +{ + /// + /// Gets the entity with given , + /// or returns null if the entity was not found. + /// + [ItemCanBeNull] + Task FindAsync(TKey id); + + /// + /// Gets the entity with given , + /// or throws if the entity was not found. + /// + [ItemNotNull] + Task GetAsync(TKey id); +} \ No newline at end of file