Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strategies for "Pre-Warming" an ASP.NET Cache

I have an ASP.NET MVC 3 / .NET Web Application, which is heavily data-driven, mainly around the concept of "Locations" (New York, California, etc).

Anyway, we have some pretty busy database queries, which get cached after they are finished.

E.g:

public ICollection<Location> FindXForX(string x)
{
   var result = _cache.Get(x.ToKey()) as Locaiton; // try cache

   if (result == null) {
      result = _repo.Get(x.ToKey()); // call db
      _cache.Add(x.ToKey(), result); // add to cache
   }

   return result;
}

But i don't want to the unlucky first user to be waiting for this database call.

The database call can take anywhere from 40-60 seconds, well over the default timeout for an ASP.NET request.

I want to "pre-warm" these calls for certain "popular" locations (e.g New York, California) when my app starts up, or shortly after.

I don't want to simply do this in Global asax (Application_Start), because the app will take too long to start up. (i plan to pre-cache around 15 locations, so that's a few minutes of work).

Is there any way i can fire off this logic asynchronously? Maybe a service on the side is a better option?

The only other alternative i can think of is have an admin page which has buttons for these actions. So an administrator (e.g me) can fire off these queries once the app has started up. That would be the easiest solution.

Any advice?

like image 700
RPM1984 Avatar asked Feb 08 '11 04:02

RPM1984


People also ask

What is cache warmup?

What is Cache Warming? Warming up cache is when websites artificially fill the cache to ensure that real visitors always have access to it when needed. Essentially, it warms up the cache for visits rather than serving the first visitor a cache miss (thus the word 'warming', as in warming up a car engine).

What are the different caching techniques available in .NET Core?

ASP.NET Core uses two caching techniques. In-memory caching uses the server memory to store cached data locally, and distributed caching distributes the cache across various servers. We'll explore them both below.

What is a way to cache an asp net page?

To manually cache application data, you can use the MemoryCache class in ASP.NET. ASP.NET also supports output caching, which stores the generated output of pages, controls, and HTTP responses in memory. You can configure output caching declaratively in an ASP.NET Web page or by using settings in the Web. config file.


2 Answers

Look into "Always running" app setting for IIS 7.5. What this basically do is have an app pool ready whenever the existing one is to be recycled. Of course, the very first would take the 40-60 seconds but afterwards things would be fast unless you physically restart the machine.

like image 190
Andrei Rînea Avatar answered Oct 16 '22 09:10

Andrei Rînea


The quick and dirty way would be to fire-off a Task from Application_Start

But I've found that it's nice to wrap this functionality into a bit of infrastructure so that you can create an ~/Admin/CacheInfo page to let you monitor the progress, state, and exceptions that may be in the process of loading up the cache.

like image 5
Scott Weinstein Avatar answered Oct 16 '22 08:10

Scott Weinstein