【发布时间】:2018-08-20 22:05:59
【问题描述】:
我的目标是缓存我的数据。我使用AspBoilerPlate 作为我代码的灵感来源。
我的 WebApi 使用 Asp.net Core。
GitHub
我在我的 GitHub 上创建了一个分支以获取更多详细信息:
CacheRepository
public class CacheRepository<TEntity, TPrimaryKey> : Repository<TEntity, TPrimaryKey>, ICacheRepository<TEntity, TPrimaryKey>
where TEntity : class, IEntity<TPrimaryKey>
{
private readonly IMemoryCache _memoryCache;
public CacheRepository(DbContext dbContext, IMemoryCache memoryCache)
: base(dbContext)
{
_memoryCache = memoryCache;
}
public override TEntity FirstOrDefault(TPrimaryKey id)
{
if (!_memoryCache.TryGetValue(GetCacheKey(id), out TEntity result))
{
result = base.FirstOrDefault(id);
if (result != null)
{
PutInCache(result);
}
}
return result;
}
public override void Delete(Expression<Func<TEntity, bool>> predicate)
{
foreach (var entity in GetAll().Where(predicate).ToList())
{
Delete(entity);
}
}
public override void Delete(TEntity entity)
{
base.Delete(entity);
RemoveFromCache(entity.Id);
}
public override void Delete(TPrimaryKey id)
{
base.Delete(id);
RemoveFromCache(id);
}
public override Task DeleteAsync(Expression<Func<TEntity, bool>> predicate)
{
return Task.Run(() => Delete(predicate));
}
public override Task DeleteAsync(TEntity entity)
{
return Task.Run(() => Delete(entity));
}
public override Task DeleteAsync(TPrimaryKey id)
{
return Task.Run(() => Delete(id));
}
public override Task<TEntity> FirstOrDefaultAsync(TPrimaryKey id)
{
return _memoryCache.GetOrCreateAsync(
GetCacheKey(id),
e => base.FirstOrDefaultAsync(id)
);
}
public override List<TEntity> GetAllList()
{
var entities = GetAllEntitiesFromCache();
if (entities == null)
{
entities = base.GetAllList();
foreach (var entity in entities)
{
PutInCache(entity);
}
}
return entities.ToList();
}
public override async Task<List<TEntity>> GetAllListAsync()
{
var entities = GetAllEntitiesFromCache();
if (entities == null)
{
entities = await base.GetAllListAsync();
foreach (var entity in entities)
{
PutInCache(entity);
}
}
return entities.ToList();
}
public override TEntity Get(TPrimaryKey id)
{
return _memoryCache.GetOrCreate(
GetCacheKey(id),
e => base.Get(id)
);
}
public override Task<TEntity> GetAsync(TPrimaryKey id)
{
return _memoryCache.GetOrCreate(
GetCacheKey(id),
e => base.GetAsync(id)
);
}
public override TEntity Insert(TEntity entity)
{
return PutInCache(base.Insert(entity));
}
public override TPrimaryKey InsertAndGetId(TEntity entity)
{
return Insert(entity).Id;
}
public override Task<TPrimaryKey> InsertAndGetIdAsync(TEntity entity)
{
return Task.FromResult(InsertAndGetId(entity));
}
public override Task<TEntity> InsertAsync(TEntity entity)
{
return Task.FromResult(Insert(entity));
}
public override TEntity Update(TEntity entity)
{
return UpdateInCache(base.Update(entity));
}
public override TEntity Update(TPrimaryKey id, Action<TEntity> updateAction)
{
return UpdateInCache(base.Update(id, updateAction));
}
public override Task<TEntity> UpdateAsync(TEntity entity)
{
return Task.FromResult(Update(entity));
}
public override async Task<TEntity> UpdateAsync(TPrimaryKey id, Func<TEntity, Task> updateAction)
{
return UpdateInCache(await base.UpdateAsync(id, updateAction));
}
#region Private
private TEntity PutInCache(TEntity entity)
{
return _memoryCache.Set(GetCacheKey(entity.Id), entity);
}
private void RemoveFromCache(TPrimaryKey id)
{
_memoryCache.Remove(GetCacheKey(id));
}
private TEntity UpdateInCache(TEntity entity)
{
RemoveFromCache(entity.Id);
return PutInCache(entity);
}
private string GetCacheKey(TPrimaryKey id)
{
return typeof(TEntity).FullName + id;
}
private IList<TEntity> GetAllEntitiesFromCache()
{
return _memoryCache.Get<List<TEntity>>(typeof(TEntity));
}
#endregion
}
我的问题:
我有很多关于我的 CacheRepository 的问题要问,但我必须减少它才能发布我的帖子。
=> 在 UnitOfWork.SubmitChanges 之前的 Insert/Edit/Delete 上直接在 CacheRepository 中添加/编辑/删除是不是一件好事?
如果您对 CacheRepository 有任何建议或建议,请分享,我真的很想了解更多。
【问题讨论】:
-
在不了解这样做的目的(也不想知道)的情况下,我只能建议您考虑使用 EF Core 内存数据库提供程序。
标签: caching asp.net-core repository-pattern unit-of-work