2015-02-26 9 views
1

Я успешно реализовал кэш Azure Redis, используя Microsoft RedisOutputCacheProvider от NuGet, который работает как ожидается для общих страниц.Azure Redis Cache и дочерние действия MVC

[ChildActionOnly] 
[ChildActionOutputCache(CacheProfile.StaticQueryStringComponent)] 
public ActionResult Show(int id) 
{ 
    // some code 
} 

Однако, похоже, я не могу заставить его работать на детские действия. До использования Redis Cache он работал с использованием OutputCacheProvider по умолчанию.

Есть ли у кого-нибудь идеи или это просто ограничение?

Заранее спасибо

ответ

1

в вашем Global.asax.cs, установить кэш вывода пользовательских ребенок действий, который говорит с Redis:

protected void Application_Start() 
{ 
    // Register Custom Memory Cache for Child Action Method Caching 
    OutputCacheAttribute.ChildActionCache = new CustomMemoryCache("My Cache"); 
} 

Этот кэш должен выводит из MemoryCache и реализации следующих членов:

/// <summary> 
/// A Custom MemoryCache Class. 
/// </summary> 
public class CustomMemoryCache : MemoryCache 
{ 
    public CustomMemoryCache(string name) 
     : base(name) 
    { 

    } 
    public override bool Add(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null) 
    { 
     // Do your custom caching here, in my example I'll use standard Http Caching 
     HttpContext.Current.Cache.Add(key, value, null, absoluteExpiration.DateTime, 
      System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null); 

     return true; 
    } 

    public override object Get(string key, string regionName = null) 
    { 
     // Do your custom caching here, in my example I'll use standard Http Caching 
     return HttpContext.Current.Cache.Get(key); 
    } 
} 

Дополнительная информация о my blog post