2014-01-22 69 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"]; 
     } 
    } 
} 
相关问题