mardi 23 avril 2019

is there a better way to invalidate the redis cache used in asp.net mvc application?

I have a asp.net mvc 5 application with entity framework 6.1.

For caching i have used redis cache in my application. that works fine for me all add update and remove items with key are working.

what i am expecting is that whenever a request comes for data it will first check cache then go towards the db if data is not found in cache and then populate cache accordingly for the subsequent requests. I have implemented Generic repository pattern with data storage and caching strategies.

The issue i am facing is this:

  • once the data is cached then how i come to know that any updates, delete or add operation is done to the data then sync those data changes with redis cache

  • how should i store data into redis database for different tables as each table may have data with same primary key values. then while fetching record from redis how do i come to know that value with key 1 is from table A, or table B.

Below i have shown the code for my repository class with injectable redis cache. what i want for methods like GetList, firstOrDefault fisrt the data is served from cache then from my db if needed

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) { }
}

Aucun commentaire:

Enregistrer un commentaire