Если возможно - кеш ViewResults . Проще и лучше.
Для необработанного кэширования я использую это, и оно работает как положено (через несколько запросов) =>
public static class CacheManager
{
public static bool Exists
(string cacheKey, HttpContextBase context)
{
return context.Cache[cacheKey] != null;
}
public static object Get
(string cacheKey, HttpContextBase context)
{
return context.Cache[cacheKey];
}
public static T Get<T>
(string cacheKey, HttpContextBase context)
where T : class
{
return context.Cache.Get(cacheKey) as T;
}
public static T Get<T>
(string cacheKey, HttpContextBase context, Func<T> getItemCallback)
where T : class
{
T item = Get<T>(cacheKey, context);
if (item == null) {
item = getItemCallback();
//by default - caching for 1 day
if (item!=null)
context.Cache.Insert(cacheKey, item, null,
DateTime.Now.AddDays(1),TimeSpan.Zero);
}
return item;
}
public static void Save<T>
(string cacheKey, HttpContextBase context, T value)
where T : class
{
context.Cache.Insert(cacheKey, value);
}
}
Использование =>
public IList<Database> AllDatabases
{
get
{
return CacheManager.Get
(CacheKeys.AllDatabases, ControllerContext.HttpContext,
() => databaseRepository.GetAll());
}
}
Только - я считаю, что проходить контекстную базу - ненужная сложность.