Reputation: 150253
I have Outputcache attribute above one of my controller action in an ASP.NET MVC 3 application:
[OutputCache(Duration=86400)] // One day
public JsonResult GetCompanyName(int companyId)
{
var company = _session.Get<Company>(companyId);
if (company == null)
throw new Exception();
return Json(company.Name);
}
The reason behind the cache is that the companies we support doesn't change so often, so we don't need to query the data base for each HttpRequest with the same parameter.
If we add a new company how can I force the "re-check"?
Can it be done to this action only or I have to delete all my app cache?
Update: My data base is Oracle
and my ORM is NHibernate
Upvotes: 0
Views: 457
Reputation: 1038850
You could use the RemoveOutputCacheItem method. So let's suppose that you have called the method like this to fetch the companies:
/somecontroller/getcompanyname/123
In order to expire this url from the cache so that the next time it is called it gets fresh results:
public ActionResult Expire()
{
Response.RemoveOutputCacheItem("/somecontroller/getcompanyname/123");
return Content("The cache was expired for the company with id = 123");
}
But IMHO for this scenario it would be better to use the second level cache of NHibernate to cache the results instead of caching the entire action with the OutputCache attribute. All that this action does is to return the JSON serialized object that came from the database. What is expensive is not the action call but the database call. So cache only the results of this database call (either using your ORMs cache or the system cache providers) as this will provide you with more robust control over the expiration policies of this cache.
Upvotes: 1