Store data in cache for 30 minutes MVC

I have an action in my MVC controller and I want to cache its return result based on the parameters passed as a key, so the next time this action is called, it will look in the cache first if it is not found, look in the data warehouse.

 public ActionResult GetSearchResult(string zipcode, int pageSize, int currentPage)
 {
     Cache[zipcode + page + currentpage] = somedata // but it should be cleared after 30 min
 }

How can i do this? I can store data in a cache object as described above, but I want the cached objects to be cleared after 30 minutes. I can’t figure out how to set the lifetime either on a global basis or on each cached object.

+3
source share
2 answers

I highly recommend using the outputcache filter for your action, rather than manually doing it yourself

     [OutputCache(Duration=1800, VaryByParam="*")]
     public ActionResult GetSearchResult(string zipcode, int pageSize, int currentPage)
     {
  //       Cache[zipcode + page + currentpage] = somedata // but it should be cleared after 30 min
     }
+2

Cache.Insert().

Cache.Insert("key", myTimeSensitiveData, null, 
  DateTime.Now.AddMinutes(30), TimeSpan.Zero);

. ASP.NET: .

+2

All Articles