How do I customize the OutputCache page attribute to change request values?
Assuming I have a page request that looks like
http: // localhost / accounting / products? id = 234
and sometimes it looks like this:
http: // localhost / accounting / products? id = 152
Since product items don't change often, I want each page for a specific product ID to be cached for an hour.
So for the first request the page will be cached for product id = 234 and the subsequent request for product id = 234 within an hour will be fetched from the cache. Next request after 1 hour timed out for product id = 234, new page will be fetched from server not from cache. Etc.
How should I do it?
Ofer Zelig's answer is correct, but since you are using MVC the correct location to add the OutputCache configuration is in action.
[OutputCache(Duration=3600, VaryByParam="id")]
public ActionResult Products(int id)
{
//
return View();
}
Check out VaryByParam .
For example:
<%@ OutputCache Duration="3600" VaryByParam="id" %>
Note. The correct way to do this specifically in MVC (as opposed to web forms) is to assign an action, as Oenning demonstrated.
Just for reference, if you have multiple parameters to cache, separate them with semicolons:
// Cache 4 hours, by id and latlng
[OutputCache(Duration=60*60*4, VaryByParam="id;lat;lng")]
public async Task<ViewResult> Item(int id, double lat, double lng) . . .