Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching in asp.net-mvc

I would like to cache my most database heavy actions in my asp.net-mvc site. In my research I have found

  • donut caching on Phil's blog
  • Caching/compressing filters on Kazi's blog
  • Scott Hansleman's podcast about how they cached things in SO.

But I don't feel I get it yet.
I want to be able to cache my POST request depending on several pars. These pars are in an object. So I would like to cache the result of the following request:

public ActionResult AdvancedSearch(SearchBag searchBag) 

Where searchBag is an object that holds (a bunch) of optional search parameters. My views themselves are light (as they should be), but the data access can be rather time consuming, depending on what fields are filled in in the search bag.

I have the feeling I should be caching on my datalayer, rather then on my actions.
How am I supposed to use the VaryByParam in the OutputCache attribute?

like image 582
Boris Callens Avatar asked Dec 22 '08 10:12

Boris Callens


People also ask

Where cache is stored in ASP.NET MVC?

Cache will be stored on the basis of the list of semicolon separated by a string. The example of VaryByParam is given below. Any (Default)- Content is cached in three locations- the Web Server, any proxy Servers and the Web Browser. Client- Content is cached on the Web Browser.

What are the two types of caching?

L1 cache, or primary cache, is extremely fast but relatively small, and is usually embedded in the processor chip as CPU cache. L2 cache, or secondary cache, is often more capacious than L1.

What is caching in ASP.NET web API?

Caching is the technique of storing the data which are frequently used. Those data can be served faster for any future or subsequent requests by eliminating the unnecessary requests to external data sources.


2 Answers

I like to cache in the model or data layer as well. This isolates everything to do with retrieving data from the controller/presentation. You can access the ASP.NET cache from System.Web.HttpContext.Current.Cache or use the Caching Application Block from the Enterprise Library. Create your key for the cached data from the parameters for the query. Be sure to invalidate the cache when you update the data.

like image 114
Matthew Avatar answered Sep 28 '22 11:09

Matthew


Or you can be independent of the HttpContext.Current and access Cache from HttpRuntime.Cache :)

like image 43
Andrei Rînea Avatar answered Sep 28 '22 11:09

Andrei Rînea