Browse Source

Introduce IEntityCache and its default implementations.

pull/14055/head
Halil İbrahim Kalkan 4 years ago
parent
commit
4530b445b5
  1. 40
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheBase.cs
  2. 41
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs
  3. 35
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapper.cs
  4. 22
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithObjectMapperContext.cs
  5. 24
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheWithoutCacheItem.cs
  6. 22
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/IEntityCache.cs

40
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<TEntity, TEntityCacheItem, TKey> :
IEntityCache<TEntityCacheItem, TKey>
where TEntity : Entity<TKey>
where TEntityCacheItem : class
{
protected IReadOnlyRepository<TEntity, TKey> Repository { get; }
protected IDistributedCache<TEntityCacheItem, TKey> Cache { get; }
protected EntityCacheBase(
IReadOnlyRepository<TEntity, TKey> repository,
IDistributedCache<TEntityCacheItem, TKey> cache)
{
Repository = repository;
Cache = cache;
}
public virtual async Task<TEntityCacheItem> FindAsync(TKey id)
{
return await Cache.GetOrAddAsync(
id,
async () => MapToCacheItem(await Repository.FindAsync(id))
);
}
public virtual async Task<TEntityCacheItem> GetAsync(TKey id)
{
return await Cache.GetOrAddAsync(
id,
async () => MapToCacheItem(await Repository.FindAsync(id))
);
}
protected abstract TEntityCacheItem MapToCacheItem(TEntity entity);
}

41
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<TEntity, TKey>(
this IServiceCollection services)
where TEntity : Entity<TKey>
{
return services
.AddTransient<
IEntityCache<TEntity, TKey>,
EntityCacheWithoutCacheItem<TEntity, TKey>
>();
}
public static IServiceCollection AddEntityCache<TEntity, TEntityCacheItem, TKey>(
this IServiceCollection services)
where TEntity : Entity<TKey>
where TEntityCacheItem : class
{
return services
.AddTransient<
IEntityCache<TEntityCacheItem, TKey>,
EntityCacheWithObjectMapper<TEntity, TEntityCacheItem, TKey>
>();
}
public static IServiceCollection AddEntityCache<TObjectMapperContext, TEntity, TEntityCacheItem, TKey>(
this IServiceCollection services)
where TEntity : Entity<TKey>
where TEntityCacheItem : class
{
return services
.AddTransient<
IEntityCache<TEntityCacheItem, TKey>,
EntityCacheWithObjectMapperContext<TObjectMapperContext, TEntity, TEntityCacheItem, TKey>
>();
}
}

35
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<TEntity, TEntityCacheItem, TKey> :
EntityCacheBase<TEntity, TEntityCacheItem, TKey>
where TEntity : Entity<TKey>
where TEntityCacheItem : class
{
protected IObjectMapper ObjectMapper { get; }
public EntityCacheWithObjectMapper(
IReadOnlyRepository<TEntity, TKey> repository,
IDistributedCache<TEntityCacheItem, TKey> cache,
IObjectMapper objectMapper)
: base(
repository,
cache)
{
ObjectMapper = objectMapper;
}
protected override TEntityCacheItem MapToCacheItem(TEntity entity)
{
if (typeof(TEntity) == typeof(TEntityCacheItem))
{
return entity.As<TEntityCacheItem>();
}
return ObjectMapper.Map<TEntity, TEntityCacheItem>(entity);
}
}

22
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<TObjectMapperContext, TEntity, TEntityCacheItem, TKey> :
EntityCacheWithObjectMapper<TEntity, TEntityCacheItem, TKey>
where TEntity : Entity<TKey>
where TEntityCacheItem : class
{
public EntityCacheWithObjectMapperContext(
IReadOnlyRepository<TEntity, TKey> repository,
IDistributedCache<TEntityCacheItem, TKey> cache,
IObjectMapper<TObjectMapperContext> objectMapper) // Intentionally injected with TContext
: base(
repository,
cache,
objectMapper)
{
}
}

24
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<TEntity, TKey> :
EntityCacheBase<TEntity, TEntity, TKey>
where TEntity : Entity<TKey>
{
public EntityCacheWithoutCacheItem(
IReadOnlyRepository<TEntity, TKey> repository,
IDistributedCache<TEntity, TKey> cache)
: base(
repository,
cache)
{
}
protected override TEntity MapToCacheItem(TEntity entity)
{
return entity;
}
}

22
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<TEntityCacheItem, in TKey>
where TEntityCacheItem : class
{
/// <summary>
/// Gets the entity with given <paramref name="id"/>,
/// or returns null if the entity was not found.
/// </summary>
[ItemCanBeNull]
Task<TEntityCacheItem> FindAsync(TKey id);
/// <summary>
/// Gets the entity with given <paramref name="id"/>,
/// or throws <see cref="EntityNotFoundException"/> if the entity was not found.
/// </summary>
[ItemNotNull]
Task<TEntityCacheItem> GetAsync(TKey id);
}
Loading…
Cancel
Save