0

在 ASP.NET Core 中,我们是否可以有 2 个本地内存 ( ) 实例IMemoryCache,我们可以使用不同的大小选项对其进行配置?

我知道IMemoryCache是一个单例,在应用程序范围内应该只有一个实例可用。

但我只是好奇这是否可能,所以对于用例,可以说一个人想要将一组数据的大小限制为 100 MB,而另一个人想要将另一组数据的大小限制为 500MB?

4

1 回答 1

0

我同时想出如下

public class Cache_100Items
{
    private readonly IMemoryCache _cache;
    public Cache_100Items(IMemoryCache cache)
    {
        _cache = new MemoryCache(new MemoryCacheOptions()
        {
            SizeLimit =100,
            CompactionPercentage = 0.25
        }); 
    }
    //get and set methods here
}

public class Cache_500Items
{
    private readonly IMemoryCache _cache;
    public Cache_500Items(IMemoryCache cache)
    {
        _cache = new MemoryCache(new MemoryCacheOptions()
        {
            SizeLimit = 500,
            CompactionPercentage = 0.25
        });
    }
    //get and set methods here
}
于 2022-01-06T23:58:22.500 回答