2014-01-22 72 views
5

我有一個Sitecore 7控制器渲染。我需要通過自定義方法來改變OutputCache。我可以在Sitecore 7控制器渲染中使用VaryByCustom嗎?

渲染目前在Sitecore中設置爲「Cachable」,「VaryByData」和「VaryByParm」。

我已經添加了一個輸出緩存屬性,我的行爲,並設置自定義字符串變化:

[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屬性......就好像它實際上只是做了默認「 Cachhable「,」VaryByData「,」VaryByParm「行爲tecore。

任何線索?

回答

10

好的,我是這麼做的。

我在/sitecore/templates/System/Layout/Sections/Caching上添加了一個名爲「VaryByMyCustomThing」的複選框。

然後,我用自定義實現替換了Sitecore.Mvc.config中的「GenerateCacheKey」管道。我更換了這一點:

<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"]; 
     } 
    } 
} 
相關問題