mirror of https://github.com/abpframework/abp.git
6 changed files with 184 additions and 0 deletions
@ -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); |
||||
|
} |
||||
@ -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> |
||||
|
>(); |
||||
|
} |
||||
|
} |
||||
@ -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); |
||||
|
} |
||||
|
} |
||||
@ -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) |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -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; |
||||
|
} |
||||
|
} |
||||
@ -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…
Reference in new issue