EntityCache.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System.Threading.Tasks;
  2. using Abp.Domain.Repositories;
  3. using Abp.Events.Bus.Entities;
  4. using Abp.Events.Bus.Handlers;
  5. using Abp.ObjectMapping;
  6. using Abp.Runtime.Caching;
  7. namespace Abp.Domain.Entities.Caching
  8. {
  9. public class EntityCache<TEntity, TCacheItem> :
  10. EntityCache<TEntity, TCacheItem, int>,
  11. IEntityCache<TCacheItem>
  12. where TEntity : class, IEntity<int>
  13. {
  14. public EntityCache(
  15. ICacheManager cacheManager,
  16. IRepository<TEntity, int> repository,
  17. string cacheName = null)
  18. : base(
  19. cacheManager,
  20. repository,
  21. cacheName)
  22. {
  23. }
  24. }
  25. public class EntityCache<TEntity, TCacheItem, TPrimaryKey> :
  26. IEventHandler<EntityChangedEventData<TEntity>>, IEntityCache<TCacheItem, TPrimaryKey>
  27. where TEntity : class, IEntity<TPrimaryKey>
  28. {
  29. public TCacheItem this[TPrimaryKey id]
  30. {
  31. get { return Get(id); }
  32. }
  33. public string CacheName { get; private set; }
  34. public ITypedCache<TPrimaryKey, TCacheItem> InternalCache
  35. {
  36. get
  37. {
  38. return CacheManager.GetCache<TPrimaryKey, TCacheItem>(CacheName);
  39. }
  40. }
  41. public IObjectMapper ObjectMapper { get; set; }
  42. protected ICacheManager CacheManager { get; private set; }
  43. protected IRepository<TEntity, TPrimaryKey> Repository { get; private set; }
  44. public EntityCache(
  45. ICacheManager cacheManager,
  46. IRepository<TEntity, TPrimaryKey> repository,
  47. string cacheName = null)
  48. {
  49. Repository = repository;
  50. CacheManager = cacheManager;
  51. CacheName = cacheName ?? GenerateDefaultCacheName();
  52. ObjectMapper = NullObjectMapper.Instance;
  53. }
  54. public virtual TCacheItem Get(TPrimaryKey id)
  55. {
  56. return InternalCache.Get(id, () => GetCacheItemFromDataSource(id));
  57. }
  58. public virtual Task<TCacheItem> GetAsync(TPrimaryKey id)
  59. {
  60. return InternalCache.GetAsync(id, () => GetCacheItemFromDataSourceAsync(id));
  61. }
  62. public virtual void HandleEvent(EntityChangedEventData<TEntity> eventData)
  63. {
  64. InternalCache.Remove(eventData.Entity.Id);
  65. }
  66. protected virtual TCacheItem GetCacheItemFromDataSource(TPrimaryKey id)
  67. {
  68. return MapToCacheItem(GetEntityFromDataSource(id));
  69. }
  70. protected virtual async Task<TCacheItem> GetCacheItemFromDataSourceAsync(TPrimaryKey id)
  71. {
  72. return MapToCacheItem(await GetEntityFromDataSourceAsync(id));
  73. }
  74. protected virtual TEntity GetEntityFromDataSource(TPrimaryKey id)
  75. {
  76. return Repository.FirstOrDefault(id);
  77. }
  78. protected virtual Task<TEntity> GetEntityFromDataSourceAsync(TPrimaryKey id)
  79. {
  80. return Repository.FirstOrDefaultAsync(id);
  81. }
  82. protected virtual TCacheItem MapToCacheItem(TEntity entity)
  83. {
  84. if (ObjectMapper is NullObjectMapper)
  85. {
  86. throw new AbpException(
  87. string.Format(
  88. "MapToCacheItem method should be overrided or IObjectMapper should be implemented in order to map {0} to {1}",
  89. typeof (TEntity),
  90. typeof (TCacheItem)
  91. )
  92. );
  93. }
  94. return ObjectMapper.Map<TCacheItem>(entity);
  95. }
  96. protected virtual string GenerateDefaultCacheName()
  97. {
  98. return GetType().FullName;
  99. }
  100. public override string ToString()
  101. {
  102. return string.Format("EntityCache {0}", CacheName);
  103. }
  104. }
  105. }