Reputation: 1497
I am using EF5 in my current project and I will upgrade it to EF6. I know that EF has an internal query cache. This is all good, but I think it won't be enough for my needs. I want to use a cache server like memcache or redis. I know that Nhibernate have some very nice appenders like memcache. I find NCache with a quick internet search. But its documentation and samples seems outdated. So I'm not sure if I can use it. Are there any stable cache providers for EF5 or EF6?
Upvotes: 5
Views: 4315
Reputation: 317
There is no stable cache providers for EF6.1 yet but you can have a look here
Second Level Cache for EF 6.1 | Code, the Universe and everything
Upvotes: 2
Reputation: 1226
You can't do this out of the box. EF6 doesn't support Second Level Caching.
You must implement a wrapper class for your db operations.
Example:
public abstract class BaseEntity{ public int Id { get;set; }
public interface IRepository<T> where T : BaseEntity
{
T GetById(object id);
void Insert(T entity);
void Update(T entity);
void Delete(T entity);
IQueryable<T> Table { get; }
}
public class EfRepository<T> : IRepository<T> where T : BaseEntity
{
private readonly IDbContext _context;
private IDbSet<T> _entities;
public EfRepository(IDbContext context)
{
this._context = context;
}
public virtual T GetById(object id)
{
//Check cache first, if exists
return this.Entities.Find(id);
}
public virtual void Insert(T entity)
{
if (entity == null)
throw new ArgumentNullException("entity");
this.Entities.Add(entity);
//Add entity to cache
this._context.SaveChanges();
}
public virtual void Update(T entity)
{
//Update cache
if (entity == null)
throw new ArgumentNullException("entity");
this._context.SaveChanges();
}
public virtual void Delete(T entity)
{
//Remove from cache
this.Entities.Remove(entity);
this._context.SaveChanges();
}
public virtual IQueryable<T> Table
{
get
{
return this.Entities;
}
}
protected virtual IDbSet<T> Entities
{
get
{
if (_entities == null)
_entities = _context.Set<T>();
return _entities;
}
}
}
There are also oss implementations of caching provider on git hub here and a redis provider on top of it here.
Upvotes: 1
Reputation: 11348
Microsoft enterprise library caching block is worth a look.
available over nuget Install-Package EnterpriseLibrary.Caching
caching block Documentation
and
Caching Architecture Guide for .NET Framework Applications
Upvotes: 0
Reputation: 3327
Try using EntityFramework.Extended cache with any cache mechanism you prefer.
https://github.com/loresoft/EntityFramework.Extended/wiki/Query-Result-Cache
Upvotes: 2