2

I want to disable caching when running ASP.NET core application in Development environment. How can i do that?

I'm setting cache up in Startup:

services.AddMemoryCache();

I want to disable cache in templates, which use the <cache> tag:

<cache expires-after="@TimeSpan.FromSeconds(3600)">
3
  • 1
    Could you show how you setup caching or if you don't can you show/explain what's cached? Commented Apr 5, 2019 at 7:27
  • @AntonToshik i've added setup/use case Commented Apr 5, 2019 at 7:51
  • 1
    I assume you are looking for a 1 line of code on/off switch kinda thing and do not want to specify which parts to turn off explicitly. Commented Apr 5, 2019 at 7:58

2 Answers 2

2

You can simply inject IHostingEnvironment into the startup constructor of your Startup.cs

Like So

      private readonly IHostingEnvironment _environment;

       public Startup(IConfiguration configuration,IHostingEnvironment environment)
       {
        _environment = environment;
        Configuration = configuration;
       }

Then you can use the private IHostingEnvironment inside your configures services method .

public void ConfigureServices(IServiceCollection services) 
{
 if(!_environment.IsDevelopment())
    services.AddMemoryCache();
}

EDIT :

After rereading the question the cache tags should be disabled aswell. I would suggest adding a flag inside your appsettings.devlopment.json called"PageCachingEnabled": "false" .

On the view I would then inject the configuration like so

@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
<cache [email protected](Configuration["PageCachingEnabled"]) expires-after="@TimeSpan.FromSeconds(3600)">
Sign up to request clarification or add additional context in comments.

4 Comments

@KunalMukherjee He wants to disable caching when the asp net core environment variable is set to "Development" ie the environment variable on the machine. There is an abstraction of code contained within the IHostingEnvironment concrete implementations which provide helper functions around the current environment setup. In the above code Memory cache will only be added if the environment is Not Development therefore "disabling" it. Unless I misunderstood the question this is what he wanted to achieve
so he can just go and set the ASPNETCORE_ENVIRONMENT variable from the build settings right
@KunalMukherjee Well yes ? But he only wants this not to happen on development , I fail to see how simply changing that variable magically fixes this issue , just changing that variable may lead to completely other configuration being loaded in from the appsettings.json transform. His current code will always add memory caching regardless of environment. My answer simply introduces the concept of adding the caching when he is not in Development.
Internally _environment.IsDevelopment() just goes and checks ASPNETCORE_ENVIRONMENT if its set to Development or not
0

The trick is to use services.AddDistributedMemoryCache(); in development, this will use the memory of the running system as a cache.

if(Env.IsDevelopment()) {
  services.AddDistributedMemoryCache();
} else {
  services.AddStackExchangeRedisCache();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.