如何在索引更新时更新 Sitecore Web Api 输出的缓存

How to update cache for a Sitecore Web Api output upon the index update

我正在实施 Sitecore 8 Web Api。确切地说,我在 Sitecore Services Infrastructre 中使用 ServiceApiController。我想知道如何缓存此控制器的 JSON 输出,使其仅在重建我从中获取数据的 lucene 索引时才会更新?

我没有将此控制器注册为控制器渲染,因为显然我们不需要这样做,它开箱即用。我刚刚通过 jQuery 和 javascript 代码读取 ajax 调用的 JSON 输出,HTML 标记位于 MVC 视图渲染中。我想在视图渲染上设置缓存没有意义。不是吗?

我该怎么办?

最好的选择是使用 HtmlCache 并将数据存储在那里。此缓存在发布时被完全清除,因此使用它是有意义的。

您可以使用 SetHtml 方法将条目添加到缓存中:

var cache = Sitecore.Caching.CacheManager.GetHtmlCache(Sitecore.Context.Site);
var json = cache.GetHtml("mycachekey");
if (string.IsNullOrWhiteSpace(json))
{
    var json = // build your json output here
    cache.SetHtml("mycachekey", jsonValue);
}
return json;