I am working on a search page that contains several dropdowns with distinct database values. I began researching caching techniques and ran across a few possible solutions
- OutputCaching
- HttpContext.Cache
As I read more into different types of caching, I remembered that this application also provides metrics pages, which can utilize the same dropdown menus from the search page. Now instead of a search page solution, I want a site wide solution.
The way I retrieve distinct values in both cases are nearly identical. I have a ViewModel with various "Available" fields. Those fields are populated 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 a user hits a search or metrics page, the application must hit the database to populate the dropdown menus. Caching would greatly reduce the load it puts on the db and network.
It seems that OutputCache only works with controller actions, so that seems out of the question.
The result of the distinct query will change very rarely. Is this something I want to cache using HttpContext? What type of caching would 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 almost seems like overkill for this situation. Would I just include the class as such private AvailableValue Values {set;get;}
and access them via Values.AvailableSites
?