问题描述
当您要缓存一些依赖于外部数据(搜索索引等)的子布局时,您将如何处理?
How would you handle a situation when you want to cache some sublayout that relies on external data(search index, etc.)?
下面是示例:
- 更新内容后,单击
在Sitecore中发布。 - 发布
后,HTML缓存会自动清除。 - 呈现子布局,并在
第一次请求时将其放入缓存,但是搜索索引
尚未更新。 - 搜索索引已更新。
- 在下一次缓存清除之前,子布局将显示缓存(过时)日期。
- After updating content you click"Publish" in Sitecore.
- HTML cache is automatically cleared afterpublishing.
- Sublayout is rendered and put into the cache onfirst request, but the search indexis not yet updated.
- Search index is updated.
- Until the next cache cleanup, sublayout will display the cached (obsolete) date.
有没有简单的方法可以解决此问题?
Are there any simple ways to fix this issue?
到目前为止,我只有一个主意-在索引更新结束时触发缓存清理,但是由于许多原因它可能很复杂。
I've got only one idea so far - trigger cache cleanup when the index update is over, but it might be complicated for many reasons.
推荐答案
您可以缓存子布局并因参数而异,您可以在其中定义自定义参数。这些参数可以是Lucene的唯一字符串,例如上次对其进行重建。
You can cache the sublayout and vary by params, where you define the custom params. Those params can be a unique string from Lucene, e.g the last time it was rebuilt.
例如,
<sc:sublayout ID="slNews" Path="NewsList.ascx" Cacheable="true" VaryByParm="true" runat="server" />
注意:Sitecore代码有错字,而 VaryByParm不是 VaryByParam
Note: the Sitecore code has a typo and it's "VaryByParm" not "VaryByParam"
在C#中:
string lastIndexRebuild = GetLastRebuildTimeOfIndex().ToString();
slNews.Parameters = "lastIndexRebuild=" + lastIndexRebuild;
如果可以以某种方式定义一种方法来确定上次重建索引的时间,则可以将其用作一个参数,用于根据索引的新鲜度或陈旧性定义自定义缓存实例。您甚至可以添加其他参数,例如数据源等。
If you can somehow define a method to determine when the index was last rebuilt, you can use that as a parameter to define custom cache instances based on when the index freshness or staleness. You can even tack on additional parameters, like a datasource, etc.
这篇关于Sitecore HTML缓存和外部数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!