Caching queries with Entity Framework 6.1 with InMemoryCache

Posted: (EET/GMT+2)

 

Entity Framework 6.x does not support query result caching out of the box, which might be either a good of a bad thing depending on your view or situation. Nonetheless, adding caching support is not difficult, as there's a nice open-source project called "Second Level Cache for Entity Framework 6.1" on CodePlex.

This project includes the convenient (yet simple) InMemoryCache class, which allows you to improve performance of repetitive queries. This is functionality is also available in the EntityFramework.Cache NuGet package.

Note also that starting at Entity Framework (EF) version 6.1.2, you can control query parameter caching in your app.config (or web.config) file, which is a related setting, but not the same as query result caching:

<entityFramework>   
  <queryCache size="1000" cleaningIntervalInSeconds="-1" />   
</entityFramework>

Entity Framework 6.1.2 was made available late last year.

Hope this helps!