title: Aspnet Core persist In-Memory cache data on application startup
body: <p>How to persist IMemorycache when I start and stop the website/debugging?. I was under the impression that IMemorycache is stored on the server and doesn't get impacted by website startup. Everytime I start and stop the website the IMemorycache entries are being reset to 0
  cache.Set(&quot;entryA&quot;, &quot;data1&quot;, cacheEntryOptions);
  First time this line is called in configure, I expect to have one entry in the cache after the line of code is executed. Next time within 5 seconds/minutes or before expiration time if I re-start the website (Startup is called. or stop and start debugging), there should be one entry in the cache even before the line of code gets executed. But that is not the way it is working. Is my understanding incorrect? I need a way to persist an item on the server for atleast 20min without losing everytime startup is called.</p>
  <p>I see a similar post but not sure how the marked answer is a solution. What is the advantage of doing it in Program.cs vc configure.
  <a href="https://stackoverflow.com/questions/55338211/imemorycache-does-not-save-data-at-application-startup">IMemoryCache does not save data at application startup</a></p>
  <p>This is our code</p>
  <pre><code>using Microsoft.Extensions.Caching.Memory;
       public Startup(IHostingEnvironment env)
              {
                  const string ENV_VARIABLE_SITENAME = &quot;%WEBSITE_SITE_NAME%&quot;;
                  var siteName = Environment.ExpandEnvironmentVariables(ENV_VARIABLE_SITENAME);
      
                  if (string.IsNullOrEmpty(siteName) || siteName == ENV_VARIABLE_SITENAME)
                      siteName = System.Environment.MachineName;
      
                  log.Info($&quot;*******Site Name: '{siteName}' ************&quot;);
      
                  var builder = new ConfigurationBuilder()
                      .SetBasePath(env.ContentRootPath)
                      .AddJsonFile(&quot;appsettings.json&quot;, optional: false, reloadOnChange: true)
                      .AddJsonFile($&quot;appsettings.{siteName}.json&quot;, optional: false, reloadOnChange: true)
                      .AddJsonFile($&quot;appsettings.{env.EnvironmentName}.json&quot;, optional: true)
                      .AddEnvironmentVariables();
      
                  Configuration = builder.Build();
                  MyConfiguration = new class.Configuration(Configuration);
              }
      
      
             public void Configure(
                                  IApplicationBuilder app,
                                  IHostingEnvironment env,
                                  ILoggerFactory loggerFactory,
                                  IMemoryCache cache
                              )
                      {
                          if (env.IsDevelopment())
                          {
                              app.UseDeveloperExceptionPage();
                              // Use Swagger
                              //app = SwaggerServiceExtensions.UseSwaggerDocumentation(app);
                          }
                          else
                          {
                              //app.UseExceptionHandler(&quot;/Error&quot;);
                              app.UseHsts();
                          }
              
                          var cacheEntryOptions = new MemoryCacheEntryOptions()
                              .SetSize(1)//Size amount
                              //Priority on removing when reaching size limit (memory pressure)
                              .SetPriority(CacheItemPriority.High)
                              // Keep in cache for this time, reset time if accessed.
                              .SetSlidingExpiration(TimeSpan.FromMinutes(5))
                              // Remove from cache after this time, regardless of sliding expiration
                              .SetAbsoluteExpiration(TimeSpan.FromSeconds(10));
              
                          cache.Set(&quot;entryA&quot;, &quot;data1&quot;, cacheEntryOptions);
                          
                          app.ConfigureExceptionHandler();
              
                          // Use Swagger
                          app = SwaggerServiceExtensions.UseSwaggerDocumentation(app);
              
              
                          }
              
              
              public void ConfigureServices(IServiceCollection services)
                      {
                          // for HttpContext
                          services.TryAddSingleton&lt;IHttpContextAccessor, HttpContextAccessor&gt;();
              
                          // need MVC
                          services.AddMvc(options =&gt;
                          {
                              options.Filters.Add(new ErrorHandlingFilter());
                          }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
              
                          // Memory Cache
                          services.AddMemoryCache();
          }
  </code></pre>