82

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

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?

Boris Callens
  • 90,659
  • 85
  • 207
  • 305
  • 2
    Have you tried with VaryByParam="searchBag.property"? – Eduardo Campañó Dec 22 '08 at 11:14
  • no, I haven't. Will try what you say. But how about enumerating several parameters? – Boris Callens Dec 22 '08 at 13:34
  • 2
    VaryByParam="firstParam;secondParam;thirdParam" – Arnis Lapsa Nov 10 '09 at 12:15
  • 1
    A little late, but the Enterprise Library Caching Application Block is included in .Net 4.0, so now you can use [System.Runtime.Caching](http://msdn.microsoft.com/en-us/library/system.runtime.caching.aspx "MS Link")'s MemoryCache for similar effect. Anywhere too :) – Carl Jan 18 '12 at 17:27

4 Answers4

74

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.

Matthew
  • 2,062
  • 13
  • 11
  • 1
    I should read up on the Enterprise Library I think. Since most of the delay lies at the Data Layer I guess that will be the best solution in the end. Currently it is a read-only DB, so this eleviates the stale object problem :) – Boris Callens Dec 23 '08 at 09:10
  • 17
    The caching app block seems like a whole mess of overkill. I've found that in almost every instance that HttpRuntime.Cache is more than adequate. – Jeff Putz Jun 01 '09 at 18:43
  • 3
    Why overkill? I'm now a lot further into the development and I've found the EL's cache system really easy to use. Reference the correct library, add the correct config lines and you can start caching and retrieving objects with one line of code each. – Boris Callens Oct 29 '09 at 12:41
  • 6
    I think it's the "add the correct config lines" step that irks some people. – Mike Chamberlain Sep 19 '12 at 07:33
  • it would be good if you could also explain how to invalidate memory cache when there is update in the database , do you use a service to check often , then how you clear the cache ? – Shaiju T Mar 19 '16 at 17:44
66

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

harriyott
  • 10,505
  • 10
  • 64
  • 103
Andrei Rînea
  • 20,288
  • 17
  • 117
  • 166
12

Often, OutputCaching can be the most fast and efficient, but only when it meets your requirements. No point in having fast efficient if it's wrong! ;)

In this case, it sounds like caching at the data layer is correct because you have complex caching needs. Sometimes, you can combine the two if the set of parameters which control what output is cached is simple.

Haacked
  • 58,045
  • 14
  • 90
  • 114
0

you can use output caching something like this

[OutputCache(Duration = 10, VaryByParam = "empID")]
      public ActionResult GetEmployeeDetail(int empID)
      {
          Employee e = new Employee();
          return Content(e.getEmployeeDetails(empID));
      }

or you can use cache profiles set it in web config

<caching>
<outputCacheSettings>
    <outputCacheProfiles>
        <add name="Admin" 

        duration="86420" varyByParam="none"/>
    </outputCacheProfiles>
</outputCacheSettings>
</caching>

and use this tag
[OutputCache(CacheProfile="Admin")]
Nikki
  • 409
  • 1
  • 5
  • 15