Sitecore HTML cache and external data sources

2019-06-04 07:56发布

How would you handle a situation when you want to cache some sublayout that relies on external data(search index, etc.)?

Here's the example:

  1. After updating content you click "Publish" in Sitecore.
  2. HTML cache is automatically cleared after publishing.
  3. Sublayout is rendered and put into the cache on first request, but the search index is not yet updated.
  4. Search index is updated.
  5. 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.

2条回答
对你真心纯属浪费
2楼-- · 2019-06-04 08:32

John West has recently posted related blog post -

Schedule Expiration for Output Cache Entries with the Sitecore ASP.NET CMS

You can set cache expiration on sublayouts, in most cases it seems to be the easiest solution when your control relies on external datasource.

查看更多
劳资没心,怎么记你
3楼-- · 2019-06-04 08:34

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.

E.g.

<sc:sublayout ID="slNews" Path="NewsList.ascx" Cacheable="true" VaryByParm="true" runat="server" />

Note: the Sitecore code has a typo and it's "VaryByParm" not "VaryByParam"

In 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.

查看更多
登录 后发表回答