Second Level Cache
Entity Framework currently support only first level cache (cache for entities). I would like to see a second level cahce implemented in the next version (queries cache).
Todd Hubers commented
While it's an interesting idea, I disagree.
Second level cache isn't a one-size-fits-all solution. I've seen EFCache used in the past and there needs to be much more research before this is attempted - if at all. In the current world of Web and JSON RESTful webservices, [OutputCaching] attribute is preferable in my opinion, caching not only the database data, but also the serialized form. But that's all subject to the different projects, people, customers and decisions made. This is probably why EF doesn't have it built in, it's not THE solution.
The biggest problem with caching is expiry. Whereas single-level cache concerns single discrete objects from a repository, and lifetime associated with the datacontext, complex queries span multiple repositories and can have a long lifetime and hence no automatic expiry mechanism. With second-level cache the software engineer needs to notify when to expire.
If you have a query return a list of Book Titles that are overdue, the results can be affected by two things: Returning of the Book, and Change of Book Title. But that's a simple example.
Vahid Nasiri commented
Current implementations of second level caching:
@abatishchev: Of the 17 "under review" ideas I looked at today are actually already being implemented in EF7, and at least one has been completed already for EF 6.1.2, and some are still being considered, just not committed to a release.
The feedback we get in this UserVoice site is fundamental to our planning and prioritization, even if many great ideas haven't been implemented yet and we don't know exactly when we are going to implement them. I prefer to be upfront about that and I realized earlier today that we were using the "under review" status incorrectly to tag ideas that we thought were very good rather than as an intermediary step that should lead to "planned", "started", "completed" or to "rejected" once reviewed.
On the other hand, since we started this UserVoice site we have completed or started to implement about half of the top 40 voted ideas. We have also implemented many of the top voted issues in our bug database at CodePlex:
Dear Diego, you aren’t currently working on ALL TOP requests? Say honestly, you aren't.
Gazsi Robert commented
Any feedback after such a long review (over 3 years now)?
Anybody knows of an open source initiative on this?
Absolutely agree with Martin. This feature was suggested long-long time ago. All modern ORM has it supported.
Please stop reviewing and just get this done. There are so many problems with the providers offered by http://code.msdn.microsoft.com/EFProviderWrappers-c0b88f32 when using CodeFirst, particularly if you are doing a multi-tenant application with a DB per tenant scheme as I am doing, using migrations.
And yes, for Azure Database, performance is a serious concern where caching is critical. Having to ad-hoc cache at all the various points where we want to cache is too painful. At least some out of the box extension points for caching should be a no-brainer.
Simon de Kraa commented
Especially on Windows Azure where at the moment you have quite a performance penalty using SQL Azure and with the availability of Windows Azure cache to me this seems like a must have on Windows Azure.
Also a sample with dbcontext http://www.codeproject.com/Articles/435142/Entity-Framework-Second-Level-Caching-with-DbConte
Vahid Nasiri commented
Here you can find an implementation of second level caching in EF Code first:
This is already something that can be acheived http://msdn.microsoft.com/en-us/magazine/hh394143.aspx but it should be something simple to accomplish.
@Tommy compare EFCachingProvider to NHibernate's built in configurable option. 2nd level cache is a must for ORM and should be readily available to the user.
Unless I don't understand this issue correctly, this would be achieved by simply expanding upon Jaroslaw Kowalski's example of implementing first level caching:
His example already provides access to the underlying connections and commands, which you should have no problem using to extend as described?
Byung Choi commented
EF should be easily extensible. One of the main concern is caching.
Hibernate and NHibernate went through this issue years ago.
Felipe Fujiy Pessoto commented
Cache isn´t responsability of domain.
Cache is a cross-cutting concern.
ASP really need cashing support.
if that means the ability to query the datacontext offline without db connection using LINQ , then that would be really cool and much much needed
Denis Kotov commented
Caching is needed for ASP.NET applications. Possibility addition dependence on changes in a source by SqlCacheDependency.
Unai Zorrilla commented
Nooooooooooooooo, cache is responsability of domain, cache is bussines no data .......