Second Level Caching with Entity Framework
Second Level Caching is one of the features that is not present out-of-the-box in Entity Framework. In the MSDN article “Second-Level Caching in the Entity Framework and AppFabric” Julie Lerman shows how to implement Second Level Caching with Entity Framework to take advantage of caching services like Microsoft AppFabric.
Why do we need Second Level Caching?
One of the great benefits of the EF is its ability to automatically generate database persistence commands (inserts, updates and deletes) based on the state information found in the context. But if that state information is unavailable, the EF has nothing to do when it’s time to call SaveChanges.
Second-level caches are instrumental in solving this type of problem. These caches exist outside of the transaction—often outside of the application—and therefore are available to any context instance.
Julie extends the EF Caching Provider designed by Jarek Kowalski to make it work with Windows Server AppFabric. She modifies the adapter class to make it work with AppFabric client API, and extends the ObjectContext class. You can download the code sample from the article to learn more.
Second level caching is also useful for improving performance of highly transactional applications by caching commonly queried data and avoiding frequently hitting the database. Microsoft AppFabric is not the only caching solution that needs to be used with ORMs like Entity framework – other frameworks like memcached can also be used for the same purpose with appropriate changes to the code.
2nd Level caching with a distributed cache
Jon Brisbin,Stephane Maldini Nov 26, 2014