简体   繁体   English

为什么在ASP.Net Core中获取IMemoryCache的多个实例?

[英]Why getting multiple instances of IMemoryCache in ASP.Net Core?

I have what I believe is a standard usage of IMemoryCache in my ASP.NET Core application. 我认为我的ASP.NET核心应用程序中的IMemoryCache的标准用法。

In startup.cs I have: 在startup.cs我有:

services.AddMemoryCache();

In my controllers I have: 在我的控制器中,我有:

private IMemoryCache memoryCache;
public RoleService(IMemoryCache memoryCache)
{
    this.memoryCache = memoryCache;
}

Yet when I go through debug, I end up with multiple memory caches with different items in each one. 然而,当我进行调试时,我最终得到了多个内存缓存,每个缓存中包含不同的项目。 I thought memory cache would be a singleton? 我以为内存缓存会是单身?

Updated with code sample: 更新了代码示例:

public List<FunctionRole> GetFunctionRoles()
{
    var cacheKey = "RolesList";
    var functionRoles = this.memoryCache.Get(cacheKey) as List<FunctionRole>;
    if (functionRoles == null)
    {
         functionRoles = this.functionRoleDAL.ListData(orgId);
         this.memoryCache.Set(cacheKey, functionRoles, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromDays(1)));
    }
}

If I run two clients in two different browsers, when I hit the second line I can see this.memoryCache contains different entries. 如果我在两个不同的浏览器中运行两个客户端,当我点击第二行时,我可以看到this.memoryCache包含不同的条目。

I did NOT find a reason for this. 我没有找到理由。 However, after further reading I swapped from IMemoryCache to IDistributedCache using the in-memory distributed cache and the problem is no longer occurring. 但是,在进一步阅读后,我使用内存中的分布式缓存从IMemoryCache交换到IDistributedCache,问题不再发生。 I figured going this route would allow me to easily update to a redis server if I needed multiple servers later on. 如果我以后需要多台服务器,我认为这条路线可以让我轻松更新到redis服务器。

The reason that IMemoryCache is created multiple times is that your RoleService most likely gets scoped dependencies. IMemoryCache多次创建的原因是您的RoleService很可能获得作用域依赖项。

To fix it simply add a new singleton service containing the memory cache and inject in when needed instead of IMemoryCache: 要修复它,只需添加一个包含内存缓存的新单例服务,并在需要时注入而不是IMemoryCache:

// Startup.cs:

services.AddMemoryCache();
services.AddSingleton<CacheService>();

// CacheService.cs:

public IMemoryCache Cache { get; }

public CacheService(IMemoryCache cache)
{
  Cache = cache;
}

// RoleService:

private CacheService cacheService;
public RoleService(CacheService cacheService)
{
    this.cacheService = cacheService;
}

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

相关问题 XUnit如何模拟IMemoryCache ASP.NET Core - XUnit how to mock IMemoryCache ASP.NET Core ASP.NET 内核将带 IMemoryCache 的内存缓存转换为 Redis - ASP.NET Core Converting In-Memory Cache with IMemoryCache to Redis 在 ASP.net 核心应用程序中使用 IMemoryCache 和 Unity DI Container - Use IMemoryCache with Unity DI Container in ASP.net core application 如果*不*注入依赖项,是否可以在 ASP.NET Core 中使用 IMemoryCache? - Possible to use IMemoryCache in ASP.NET Core if it is *not* dependency injected? 测试ASP.NET Core IMemoryCache的正确方法 - Proper way of testing ASP.NET Core IMemoryCache ASP.NET Core-记录器的多个实例 - ASP.NET Core - Multiple instances of Logger ASP.NET 内核 - 更新缓存列表的 1 个元素<element>在 IMemoryCache 中使用 EF Core</element> - ASP.NET Core - Update 1 Element of a cached List<Element> in IMemoryCache using EF Core ASP.NET Core 2 中多个相同类型实例的依赖注入 - Dependency injection of multiple instances of same type in ASP.NET Core 2 如何从 ASP.NET 核心中的 IMemoryCache 中删除所有对象(重置) - How to remove all objects (reset ) from IMemoryCache in ASP.NET core 将DBContext放入IMemoryCache(.NET Core / EF Core)之后为什么要处理它 - Why is DBContext is disposed after putting it in IMemoryCache (.NET Core / EF Core)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM