Caching data greatly increase the website performance as its not going to do server round trip. I have already mentioned how you can use Output caching in web forms in earlier blog post here. Let’s see how we can do same thing with asp.net mvc. For this example I have used asp.net mvc razor. In asp.net mvc you can use OutputCache attribute to cache the output. Just like below.
[OutputCache(VaryByParam="none",Duration=60)] public ActionResult Index() { ViewModel.Message = DateTime.Now.ToString(); return View(); }Here it will cache the view for 60 second and will not go for server round trip. Let’s see How it will look into the browser.
You can also set the output caching in web.config and and create output cache profile which you can use any where like following.
<caching> <outputCacheSettings> <outputCacheProfiles> <clear/> <add name="MyOuputCacheProfile" duration="60" varyByParam="none" /> </outputCacheProfiles> </outputCacheSettings> </caching>Here how you can use that profile.
[OutputCache(CacheProfile = "MyOuputCacheProfile")] public ActionResult Index() { ViewModel.Message = DateTime.Now.ToString(); return View(); }It support four type of settings for output caching. VaryByContentEncoding, VaryByParam, VaryByCustom,VaryByHeader. Hope this will help you!! happy Programming.
0 comments:
Post a Comment
Your feedback is very important to me. Please provide your feedback via putting comments.