Need help with EntityFrameworkCore.Cacheable?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

154 Stars 25 Forks Apache License 2.0 50 Commits 15 Opened issues


EntityFrameworkCore second level cache

Services available


Need anything else?

Contributors list

No Data


A high performance second level query cache for Entity Framework Core.

What is EF Core Cacheable?

Entity Framework (EF) Core Cacheable is an extention library for the popular Entity Framework data access technology.

It provides caching functionality for all types of query results. Based on expression tree and parameters, the context decide rather to execute query against database or returning result from memory.

How caching affects performance

This a sample result of 1,000 iterations of an uncached and cached query, called agains a really good performing MSSQL-database.

Average database query duration [+00:00:00.1698972].
Average cache query duration [+00:00:00.0000650].
Cached queries are x2,611 times faster.

Even with a InMemory test database, the results are significant faster.

Average database query duration [+00:00:00.0026076].
Average cache query duration [+00:00:00.0000411].
Cached queries are x63 times faster.

The performance gain can be even higher, depending on the database performance.

Install via NuGet

You can view the package page on NuGet.

To install

, run the following command in the Package Manager Console:
PM> Install-Package EntityFrameworkCore.Cacheable

This library also uses the Data.HashFunction and aspnet.Extensions as InMemory cache.

Configuring a DbContext

There are three types of configuring the DbContext to support

. Each sample use
as option only for showing the pattern.

For more information about this, please read configuring DbContextOptions.

Constructor argument

Application code to initialize from constructor argument:

var optionsBuilder = new DbContextOptionsBuilder();
    .UseSqlite("Data Source=blog.db")

using (var context = new CacheableBloggingContext(optionsBuilder.Options)) { // do stuff }


Context code with

public partial class CacheableBloggingContext : DbContext
    public DbSet Blogs { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    if (!optionsBuilder.IsConfigured)
        optionsBuilder.UseSqlite("Data Source=blog.db");


Using DbContext with dependency injection

Adding the Dbcontext to dependency injection:

public void ConfigureServices(IServiceCollection services)
    services.AddDbContext(options => options
        .UseSqlite("Data Source=blog.db"))

This requires adding a constructor argument to your DbContext type that accepts DbContextOptions.


To get in use of result caching, you simply need to add

to your query and define a TTL parameter.
var cacheableQuery = cacheableContext.Books
    .Include(d => d.Pages)
    .ThenInclude(d => d.Lines)
    .Where(d => d.ID == 200)

Custom Cache Provider

Alternatively you can provide a custom implementation of

(default is
). This provides a easy option for supporting other caching systems like redis or Memcached.
optionsBuilder.UseSecondLevelCache(new MyCachingProvider());


The following contributors have either created (thats only me :stuckouttonguewinkingeye:) the project, have contributed code, are actively maintaining it (including documentation), or in other ways being helpfull contributors to this project.

| | Name | GitHub | | :--------------------------------------------------------------------------------: | --------------------- | ------------------------------------------------------- | | | Steffen Mangold | @SteffenMangold | | | Smit Patel | @smitpatel |

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.