我可以将VaryByCustom与Sitecore 7控制器渲染一起使用吗?

Nei*_*ilD 5 c# outputcache sitecore sitecore7

我有一个Sitecore 7控制器渲染.我需要通过自定义方法改变OutputCache.

渲染当前在Sitecore中设置为"Cachable","VaryByData"和"VaryByParm".

我已经为我的操作添加了输出缓存属性,并设置了自定义变量字符串:

[OutputCache(VaryByCustom = "ThisIsATest", Duration = 60)]
public ActionResult Index()
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

我的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);
}
Run Code Online (Sandbox Code Playgroud)

我从来没有看到过GetVaryByCustomString方法,并且控制器的行为似乎根本就没有OutputCache属性......就好像它实际上只是做了默认的"Cachable","VaryByData"," VaryByParm"来自Sitecore的行为.

有线索吗?

Nei*_*ilD 10

好的,这就是我做的.

我在/sitecore/templates/System/Layout/Sections/Caching"VaryByMyCustomThing" 上添加了一个复选框字段.

然后我用自定义实现替换了Sitecore.Mvc.config中的"GenerateCacheKey"管道.我替换了这个:

<processor type="Sitecore.Mvc.Pipelines.Response.RenderRendering.GenerateCacheKey, Sitecore.Mvc"/>
Run Code Online (Sandbox Code Playgroud)

有了这个:

<processor type="My.Site.Pipelines.GenerateCustomCacheKey, My.Site"/>
Run Code Online (Sandbox Code Playgroud)

我的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"];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)