List collection caching in MVC

I am working on a search page containing several drop-down lists with different database values. I started learning caching techniques and came up with several possible solutions.

  • OutputCaching
  • HttpContext.Cache

Since I read more about the different types of caching, I remembered that this application also provides metric pages that can use the same drop-down menus from the search page. Now, instead of a search page solution, I want a solution for the entire site.

The method of obtaining individual values ​​in both cases is almost identical. I have a ViewModel with various "Available" fields. These fields are filled in the constructor.

public class SearchViewModel { public AvailableSites {set;get;} public SearchViewModel() { AvailableSites = db.SomeTable.Select(s => s.Site).Distinct(); } } 

When my model is set up this way, every time the user clicks on the search or metrics page, the application must go to the database to populate the drop-down menus. Caching will significantly reduce the load it puts on db and the network.

It seems that OutputCache only works with controller actions, so this is out of the question.

The result of a single query will change very rarely. Is this something I want to cache using an HttpContext? What type of caching will work best in this case?

Also, what would be the best way to integrate this into my MetricBase (each metric VM inherits from this) and SearchViewModel? Inheritance is almost like an unnecessary situation for this situation. I would simply include the private AvailableValue Values {set;get;} class as such and access them through Values.AvailableSites ?

+4
source share
1 answer

System.Runtime.Caching.MemoryCache provides a reliable cache in memory (see http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache(v=vs.100).aspx ).

From what I understand, it was based on System.Web.Caching, but without depending on the assembly of System.Web, so you can put it in your business layer.

This blog post should help you with the implementation: http://deanhume.com/Home/BlogPost/object-caching----net-4/37

+2
source

Source: https://habr.com/ru/post/1500627/


All Articles