Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can you use output caching in asp.net-mvc based on the parameters of your controller action

i want to use output caching to avoid hitting my db over and over with the same static query but my controllers have parameters that uniquely define the post.

How can i factor in my parameters and still support output caching in asp.net-mvc?

like image 247
leora Avatar asked Oct 16 '25 01:10

leora


1 Answers

Check out the VaryByParam property of the OutputCache attribute.

[OutputCache(Duration=int.MaxValue, VaryByParam="id")]
public ActionResult Details(int id)
{
}

For each unique id value, a unique cache instance will be created.

Edit:

If your caching needs go beyond simple VaryByParam scenarios then take a look at VaryByCustom. This will allow you to setup the scenarios as you see fit (cached version for logged in vs. not logged in user, etc. etc.)

like image 101
Khepri Avatar answered Oct 17 '25 14:10

Khepri