简体   繁体   English

如何在 Entity Framework Core 中禁用 MemoryCache?

[英]How to disable MemoryCache in Entity Framework Core?

I have this code on my Startup.我的启动中有此代码。

var connection = Configuration.GetConnectionString("DefaultConnection")?.Replace("[BD_PASS]", Environment.GetEnvironmentVariable("BD_PASS"));
services.AddDbContext<BdContext>(options => options.UseSqlServer(connection));

services.AddMemoryCache(op =>
            {
                op.SizeLimit = int.Parse(Environment.GetEnvironmentVariable("CACHE_SIZE_LIMIT") ?? "10");
            });

The problem is that I wasn't aware that Entity Framework Core would intercept my queries against the database.问题是我不知道 Entity Framework Core 会拦截我对数据库的查询。 So, I am getting a所以,我得到一个

 _context.Product.ToList();

But I am getting this message when the code above is run.但是当上面的代码运行时,我收到了这条消息。

cache entry must specify a value for size when the size limit is set设置大小限制时,缓存条目必须指定大小值

Is there anything I could do at the configuration level to say "Hey EFC, don't you bother to cache anything."有什么我可以在配置级别上说“嘿 EFC,不要费心缓存任何东西”。

Entity Framework Core uses what's called the "shared cache". Entity Framework Core 使用所谓的“共享缓存”。 It depends on IMemoryCache registered in the service container.它依赖于服务容器中注册的IMemoryCache The solution isn't to stop EF Core from using a cache, it's to use a different cache for your services that understand cache size limits.解决方案不是阻止 EF Core 使用缓存,而是为了解缓存大小限制的服务使用不同的缓存。

In other words, create a tailored cache for your own use:换句话说,为您自己的使用创建一个定制的缓存:

public class LimitedMemoryCache 
{
    public MemoryCache Cache { get; private set; }

    public LimitedMemoryCache(int limit)
    {
        Cache = new MemoryCache(new MemoryCacheOptions
        {
            SizeLimit = 10
        });
    }
}

And register it as a separate singleton:并将其注册为单独的 singleton:

var cacheLimit = int.Parse(Environment.GetEnvironmentVariable("CACHE_SIZE_LIMIT") ?? "10");
services.AddSingleton(new LimitedMemoryCache(cacheLimit));

Classes can choose to use this cache by injecting LimitedMemoryCache instead of IMemoryCache .类可以通过注入LimitedMemoryCache而不是IMemoryCache来选择使用此缓存。 Entity Framework Core and anything else that depends on IMemoryCache directly can now coexist without any problems. Entity Framework Core 和其他直接依赖于IMemoryCache的东西现在可以共存而没有任何问题。

This is mentioned on the in-memory cache docs , which is how I learned about it myself: 在内存缓存文档中提到了这一点,这就是我自己了解它的方式:

... When a size limit is set on a cache, all entries must specify a size when being added. ...在缓存上设置大小限制时,所有条目在添加时都必须指定大小。 This can lead to issues since developers may not have full control on what uses the shared cache.这可能会导致问题,因为开发人员可能无法完全控制使用共享缓存的内容。 For example, Entity Framework Core uses the shared cache and does not specify a size.例如,Entity Framework Core 使用共享缓存并且没有指定大小。 If an app sets a cache size limit and uses EF Core, the app throws an InvalidOperationException.如果应用设置缓存大小限制并使用 EF Core,则应用会引发 InvalidOperationException。 When using SetSize, Size, or SizeLimit to limit cache, create a cache singleton for caching.使用 SetSize、Size 或 SizeLimit 限制缓存时,创建缓存 singleton 进行缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM