2014-01-22 20 views
5

У меня есть рендеринг контроллера Sitecore 7. Мне нужно изменить OutputCache с помощью настраиваемого метода.Могу ли я использовать VaryByCustom с рендерингом контроллера Sitecore 7?

В настоящее время рендеринг настроен на «Cachable», «VaryByData» и «VaryByParm» в Sitecore.

Я добавил атрибут кэша вывода для моего действия, и установить пользовательские варьируются строки:

[OutputCache(VaryByCustom = "ThisIsATest", Duration = 60)] 
public ActionResult Index() 
{ 
    ... 
} 

Мой Global.asax наследует от Sitecore.Web.Application, и я переопределен GetVaryByCustomString следующим образом :

public override string GetVaryByCustomString(HttpContext context, string custom) 
{ 
    if (custom == "ThisIsATest") 
     return "some custom key"; 
    return base.GetVaryByCustomString(context, custom); 
} 

Я никогда не видел метод огня GetVaryByCustomString, и контроллер ведет себя так, как будто это не имеет атрибута OutputCache на нем вообще ... это как если бы это на самом деле просто делает по умолчанию " Cachable "," VaryByData "," VaryByParm "от Si tecore.

Любые подсказки?

ответ

10

Хорошо, вот как я это сделал.

Я добавил поле флажка на /sitecore/templates/System/Layout/Sections/Caching под названием «VaryByMyCustomThing».

Затем я заменил конвейер GenerateCacheKey в файле Sitecore.Mvc.config с пользовательской реализацией. Я заменил это:

<processor type="Sitecore.Mvc.Pipelines.Response.RenderRendering.GenerateCacheKey, Sitecore.Mvc"/> 

С этим:

<processor type="My.Site.Pipelines.GenerateCustomCacheKey, My.Site"/> 

Мой класс GenerateCustomCacheKey выглядит следующим образом:

using System.Net.Http; 
using System.Web; 
using Sitecore.Mvc.Extensions; 
using Sitecore.Mvc.Pipelines.Response.RenderRendering; 
using Sitecore.Mvc.Presentation; 

namespace My.Site.Pipelines 
{ 
    public class GenerateCustomCacheKey : GenerateCacheKey 
    { 
     protected override string GenerateKey(Rendering rendering, RenderRenderingArgs args) 
     { 
      var varyByCountryCode = rendering.RenderingItem.InnerItem["VaryByMyCustomThing"].ToBool(); 

      var key = base.GenerateKey(rendering, args); 
      if (varyByCountryCode) 
       key = key + GetCountryCodePart(rendering); 
      return key; 
     }  

     protected string GetCountryCodePart(Rendering rendering) 
     { 
      return "_#countryCode:" + (string)HttpContext.Current.Session["CountryCode"]; 
     } 
    } 
}