百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
E

EntityFrameworkCore.Cacheable

> 数据库
开源

实体框架Core 第二级缓存

195 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

实体框架Core 第二级缓存

EntityFrameworkCore.Cacheable

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

What is EF Core Cacheable?

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

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

How caching affects performance

This is a sample result of 1,000 iterations of an uncached and cached query called against a well-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 an InMemory test database, the results are significantly 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 EntityFrameworkCore.Cacheable, 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 configurations for the DbContext to support Cachable. Each sample only uses' UseSqlite' to show the pattern.

For more information about this, please read configuring DbContextOptions.

Constructor argument

Application code to initialize from constructor argument:

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

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

OnConfiguring

Context code with OnConfiguring:

csharp
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");
            optionsBuilder.UseSecondLevelCache();
        }
    }
}

Using DbContext with dependency injection

Adding the Dbcontext to dependency injection:

csharp
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext(options => options
        .UseSqlite("Data Source=blog.db"))
        .UseSecondLevelCache();
}

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

Usage

To use result caching, you simply need to add .Cacheable(... to your query and define a TTL parameter.

csharp
var cacheableQuery = cacheableContext.Books
	.Include(d => d.Pages)
	.ThenInclude(d => d.Lines)
	.Where(d => d.ID == 200)
	.Cacheable(TimeSpan.FromSeconds(60));

Custom Cache Provider

Alternatively, you can provide a custom implementation of ICacheProvider (default is MemoryCacheProvider). This provides an easy option for supporting other caching systems like redis or Memcached.

csharp
optionsBuilder.UseSecondLevelCache(new MyCachingProvider());

Contributors

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

Name GitHub
Steffen Mangold @SteffenMangold
Smit Patel @smitpatel

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#c-sharpcachedatabasedotnet-core

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类数据库
定价开源

> 相关工具

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库