0

In ASP.NET Core, can we have 2 instances of local memory (IMemoryCache) which we can configure with different size options?

I know IMemoryCache is a singleton and only one instance should be available in application scope.

But I'm just curious is it even possible so for use case lets say one want to limit size of 100 MB for one set of data and other want to limit size of 500MB for other set of data?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

I figured out in meantime as below

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
}
Peter Csala
  • 17,736
  • 16
  • 35
  • 75