Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 data caching techniques

I have a sql query (stored proc) that takes about 8-10seconds to return before the results are displayed in a webgrid. What is best practice for performance regarding cacheing in asp.net-mvc3 so the user doesn't have to take that 8-10sec hit everytime to load that data (less optimizing the query)?

like image 556
genxgeek Avatar asked Dec 13 '11 07:12

genxgeek


People also ask

What are caching techniques?

Caching is a technique of storing frequently used data/information in memory, so that, when the same data/information is needed next time, it could be directly retrieved from the memory instead of being generated by the application.

What are the different caching techniques available in .NET MVC?

ASP.NET supports three types of caching: Page Output Caching [Output caching] Page Fragment Caching [Output caching] Data Caching.

What is meant by caching in MVC?

Caching is used to improve the performance in ASP.NET MVC. Caching is a technique which stores something in memory that is being used frequently to provide better performance. In ASP.NET MVC, OutputCache attribute is used for applying Caching.


3 Answers

You could use the MemoryCache class to store the result of this query under some key. The key could be the hash of the query criterias (if you have such). And here are some guides on MSDN on how to use it.

When implementing caching bear in mind that this cache is stored in memory by default. This means that if you are running this application in a web farm it might be more interesting to use a distributed cache so that all nodes of the farm share the same cached data. This could be done by extending the ObjectCache class with some distributed caching solution. For example memcached is a popular one and it has .NET provider. Another distributed caching solution is AppFabric.

like image 181
Darin Dimitrov Avatar answered Oct 10 '22 09:10

Darin Dimitrov


It's caching this action.

[OutputCache(Duration = 300)]
public ActionResult Action(){

//some operation

return View()
}
like image 22
Oleksandr Fentsyk Avatar answered Oct 10 '22 08:10

Oleksandr Fentsyk


How often your underlaying data behind this stored procedure change? If relatively rarely, you can use very good feature - SqlCacheDependency

http://msdn.microsoft.com/en-us/library/ms178604.aspx

This way your heavy SP will be called only when needed, and result will be cached as long as possible.

like image 38
rouen Avatar answered Oct 10 '22 08:10

rouen