有没有更好的方法来使asp.net mvc应用程序中使用的redis缓存无效?

时间:2019-04-23 12:46:10

标签: c# asp.net-mvc entity-framework design-patterns stackexchange.redis

我有一个带有实体框架6.1的asp.net mvc 5应用程序。

对于缓存,我在应用程序中使用了redis缓存。对我来说效果很好,所有添加更新和删除带有键的项目都有效。

我期望的是,每当有数据请求时,它将首先检查高速缓存,如果在高速缓存中未找到数据,则将其转到db,然后为后续请求相应地填充高速缓存。我已经用数据存储和缓存策略实现了通用存储库模式。

我面临的问题是:

  • 一旦数据被缓存,然后我就知道如何对数据进行任何更新,删除或添加操作,然后将这些数据更改与redis缓存同步

  • 我应该如何将数据存储到不同表的redis数据库中,因为每个表可能具有具有相同主键值的数据。然后从redis获取记录时,我怎么知道键1的值来自表A或表B。

下面,我展示了带有可注入redis缓存的存储库类的代码。我想要的诸如GetList,firstOrDefault fisrt之类的方法的数据是从缓存中提供,然后根据需要从我的数据库中提供

public abstract class GenericRepository<TEntity> :IBasicOperations<TEntity> 
where TEntity : class
{

    protected IDataStoreStrategy<TEntity> _dataStoreStrategy;    // Data Storage Strategy e.g, SQL SERVER
    protected ICachingStrategy _cachingStrategy;    // Caching Strategy e.g, Redis
    public GenericRepository(IDataStorageContext db, ICachingStrategy cache)
    {
        _cachingStrategy = cache;
        this._dataStoreStrategy = new BaseActions<TEntity>(db);
    }

    public void Add(TEntity entity, bool SaveChanges = true)
    {
        this._dataStoreStrategy.Add(entity, SaveChanges);
    }

    public async Task AddAsync(TEntity entity, bool SaveChanges = true)
    {
       await this._dataStoreStrategy.AddAsync(entity, SaveChanges);
    }

    public void AddRange(IEnumerable<TEntity> entities, bool SaveChanges = true)
    {
        this._dataStoreStrategy.AddRange(entities, SaveChanges);
    }

    public async Task AddRangeAsync(IEnumerable<TEntity> entities, bool SaveChanges = true)
    {
       await this._dataStoreStrategy.AddRangeAsync(entities, SaveChanges);
    }

    public TEntity FindById(object Id)
    {
       return this._dataStoreStrategy.FindById(Id);
    }

    public async Task<TEntity> FindByIdAsync(object Id)
    {
       return await _dataStoreStrategy.FindByIdAsync(Id);
    }

    public IQueryable<TEntity> FindWithCondition(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "")
    {
        return this._dataStoreStrategy.FindWithCondition(filter,orderBy,includeProperties);
    }

    public TEntity First(Expression<Func<TEntity, bool>> where)
    {

        return this._dataStoreStrategy.First(where);

    }

    public Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> where)
    {
        return this._dataStoreStrategy.FirstAsync(where);
    }

    public IQueryable<TEntity> GetList()
    {
        return this._dataStoreStrategy.GetList();
    }

    //public T Last(Expression<Func<T, bool>> where)
    //{
    //    return this._dataStoreStrategy.Last(where);
    //}

    public void Remove(TEntity entity, bool SaveChanges = true)
    {
        this._dataStoreStrategy.Remove(entity, SaveChanges);
    }

    public async Task RemoveAsync(TEntity entity, bool SaveChanges = true)
    {
       await this.RemoveAsync(entity,SaveChanges);
    }

    public void RemoveRange(IEnumerable<TEntity> entities, bool SaveChanges = true)
    {
        this._dataStoreStrategy.RemoveRange(entities, SaveChanges);
    }

    public async Task RemoveRangeAsync(IEnumerable<TEntity> entities, bool SaveChanges = true)
    {
       await this._dataStoreStrategy.RemoveRangeAsync(entities, SaveChanges);
    }

    public void Save()
    {

        this._dataStoreStrategy.Save();
    }

    public async Task SaveAsync()
    {
       await this._dataStoreStrategy.SaveAsync();
    }

    public TEntity Single(Expression<Func<TEntity, bool>> where)
    {
       return this._dataStoreStrategy.Single(where);
    }

    public async Task<TEntity> SingleAsync(Expression<Func<TEntity, bool>> where)
    {
        return await this._dataStoreStrategy.SingleAsync(where);
    }

    public void Update(TEntity entity, bool SaveChanges = true)
    {
        this._dataStoreStrategy.Update(entity, SaveChanges);
    }

    public async Task UpdateAsync(TEntity entity, bool SaveChanges = true)
    {
      await  this._dataStoreStrategy.UpdateAsync(entity, SaveChanges);
    }
    internal CaliberMatrixEntities ctx { get { return _dataStoreStrategy.ctx as CaliberMatrixEntities; } }
    public virtual IQueryable<TEntity> GetAll() { return null; }
    public virtual TEntity Get(long id) { return null; }
    public virtual void Delete(long id) { }
    public virtual void Change(TEntity t, Guid by) { }
    public virtual void Add(TEntity t, Guid by) { }
    public virtual void AddRange(List<TEntity> t) { }
    public virtual void DeleteRange(List<int> t) { }
}

0 个答案:

没有答案