4

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

  1. OutputCaching
  2. 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?

4

1 回答 1

2

System.Runtime.Caching.MemoryCache 提供强大的内存缓存(请参阅http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache(v=vs.100).aspx)。

据我了解,它基于 System.Web.Caching,但不依赖于 System.Web 程序集,因此您可以将其放在业务层中。

这篇博文应该可以帮助您实现:http ://deanhume.com/Home/BlogPost/object-caching----net-4/37

于 2013-09-05T14:18:09.917 回答