Chipo Hamayobe
Chipo Hamayobe

Reputation: 1057

Setting maximum memory when using Distributed Memory Cache in .NET Core WebAPI

I am writing a .NET Core based WebAPI. I want to utilize a Distributed Memory Cache for my development environment by registering an IDistributedCache in Startup.ConfigureServices.

public void ConfigureServices(IServiceCollection services)
{
    if (_hostContext.IsDevelopment())
    {
        services.AddDistributedMemoryCache();
    }
    else
    {
        services.AddDistributedRedisCache(options =>
        {
          options.Configuration = "localhost";
          options.InstanceName = "SampleInstance";
        });
    }
}

However, I dont want the data caching to eat up most of my RAM. How can I limit the DistributedMemoryCache to use only 2GIG for example?

Upvotes: 2

Views: 2274

Answers (1)

nlawalker
nlawalker

Reputation: 6514

AddDistributedMemoryCache() has an overload that allows you to configure a MemoryDistributedCacheOptions. You can do:

services.AddDistributedMemoryCache(options =>
{
   options.SizeLimit = 2000 * 1024 * 1024; // 2000MB
});

It looks like the default is 200MB.

Upvotes: 6

Related Questions