【问题标题】:ASP.NET Core - Can we have 2 local memory with different size optionsASP.NET Core - 我们可以有 2 个具有不同大小选项的本地内存吗
【发布时间】:2022-01-07 13:11:04
【问题描述】:

在 ASP.NET Core 中,我们可以有 2 个本地内存实例 (IMemoryCache),我们可以配置不同的大小选项吗?

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

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

【问题讨论】:

    标签: asp.net-core asp.net-core-webapi asp.net-caching


    【解决方案1】:

    我在此期间发现如下

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      • 2017-02-27
      • 2012-03-24
      • 2020-01-27
      • 2018-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多