[英]Can I use VaryByCustom with a Sitecore 7 Controller Rendering?
我有一個Sitecore 7控制器渲染。 我需要通過自定義方法改變OutputCache。
渲染當前在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屬性......就好像它實際上只是做了默認的“Cachable”,“VaryByData”,“ VaryByParm“來自Sitecore的行為。
有線索嗎?
好的,這就是我做的。
我在/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"];
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.