In-memory application data caching: MVC Web API

I am writing an MVC webAPI that will be used to return values โ€‹โ€‹that will be tied to drop-down lists or used as text results based on text on a website, and I want to cache the values โ€‹โ€‹in memory, so I donโ€™t have to do database queries when every hit API.

I am going to use the MemoryCache class, and I know that I can fill the cache the first time I log in, but I do not want the first API request to be slower than others. My question is: is there a way to automatically populate the cache the first time I start WebAPI? I see that there is a folder "App_Start", maybe I just drop something here?

After the initial aggregate, I will probably execute the hourly / daily cache update request as needed.

MemoryCache: http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx

UDPATE

Ela's answer below did the trick, basically I just needed to look at the capabilities of Global.asax. Thanks for the quick help here, this raised a separate question for me about the pros / cons of various types of caching.

Pros / cons of various ASP.NET caching settings

+22
c # caching asp.net-web-api asp.net-mvc-4
Sep 21 '13 at 20:55 on
source share
1 answer

To initialize resources, you can use the global.asax launch method to start. Resources to be used mainly in the application.

The following link should help you find more information: http://www.asp.net/web-forms/tutorials/data-access/caching-data/caching-data-at-application-startup-cs

Hint: If you use in the caching process (which usually happens if you cache something in a web context / stream), keep in mind that your web application is controlled by IIS. The standard IIS configuration will disable your web application after 20 minutes if user requests are not served. This means that any resources that you have in memory will be freed.

After that, the next time the user accesses your web application, global asax, the application will be canceled again because IIS will reinitialize your web application. If you want to prevent this behavior, you either configure the application pool idle timeout so that it does not expire after 20 minutes. Or you use a different caching strategy (persistent cache, distributed cache ...).

To configure IIS for this, you can find more information here: http://brad.kingsleyblog.com/IIS7-Application-Pool-Idle-Time-out-Settings/

+22
Sep 21 '13 at 21:11
source share



All Articles