在发布时停止 EPiServer 清除输出缓存

Dan*_*ord 5 caching outputcache episerver

这不是我见过的问题,通常是“EPiServer 未清除输出缓存”。我正在努力实现相反的目标。每次发布一个页面时,整个缓存都会被删除,并且由于客户端每天发布数次,这令人沮丧。

我正在使用该[ContentOutputCache]属性并尝试httpCacheVaryByCustom在 EPiServer 中实现带有随附计划任务的规则,以便在我们决定将更新捆绑在一起并在预定时间失效时使缓存失效。

我已经测试了这条规则,它的工作原理是:

public override string GetVaryByCustomString(HttpContext context, string custom)
Run Code Online (Sandbox Code Playgroud)

我的印象是,通过使用这种类型的缓存规则,它会在发布/上传媒体时停止 EPiServer 转储我的缓存。

难道没有办法阻止这种情况发生吗?

我通过使用具有相同自定义字符串规则的标准 [OutputCache] 取得了成功,唯一的问题是编辑器将始终看到他们正在编辑的页面的缓存版本。

我在 EPiServer 的 web.config 中的应用程序设置是:

<applicationSettings globalErrorHandling="Off" operationCompatibility="DynamicProperties" uiSafeHtmlTags="b,i,u,br,em,strong,p,a,img,ol,ul,li" disableVersionDeletion="false" 
                     httpCacheability="Public" uiEditorCssPaths="~/assets/css/styles.css, ~/assets/css/editor.css" urlRebaseKind="ToRootRelative" 
                     pageUseBrowserLanguagePreferences="false" uiShowGlobalizationUserInterface="false" subscriptionHandler="EPiServer.Personalization.SubscriptionMail,EPiServer" 
                     uiMaxVersions="20" pageValidateTemplate="false" utilUrl="~/util/" 
                     uiUrl="~/EPiServer/CMS/" httpCacheExpiration="01:00:00"  httpCacheVaryByCustom="invalidateSiteCache" />
Run Code Online (Sandbox Code Playgroud)

Oll*_*e P 5

自定义GetVaryByCustomString函数将确定缓存何时失效,但任何使用 的内容请求都会根据ContentOutputCache主缓存键进行检查Episerver.DataFactoryCache.Version。此版本号在内容发布、更新等任何时候都会增加,如果版本号发生更改,缓存将失效。

要了解您需要做什么,我建议使用反编译器(例如DotPeek)并查看Episerver dll中的ContentOutputCacheAttributeOutputCacheHandler类。

您将需要:

  1. 派生一个新的处理程序 EPiServer.Web.OutputCacheHandler
  2. 创建一个ValidateOutputCache(...)仍然调用OutputCacheHandler.UseOutputCache(...)但忽略缓存版本号的替代方法
  3. 派生一个新的属性 ContentOutputCacheAttribute
  4. OnResultExecuting(ResultExecutingContext filterContext)使用与当前方法相同的逻辑覆盖该方法(这是反编译器有用的地方),但这会向您的新验证方法而不是当前方法添加回调。不幸的是,我们无法注入新的处理程序,因为验证方法是静态传递的。

例如

public override void OnResultExecuting(ResultExecutingContext filterContext)
{        
    // Rest of method
    filterContext.HttpContext.Response.Cache.AddValidationCallback(new HttpCacheValidateHandler(CustomOutputCacheHandler.CustomValidateOutputCache), (object) tuple);
}
Run Code Online (Sandbox Code Playgroud)
  1. 使用新属性代替 [ContentOutputCache]