以编程方式从缓存清除项目的Sitecore缓存

Gab*_*bar 5 caching sitecore

我想以编程方式清除项目的Sitecore缓存.我运行下面的代码.之后,我尝试在已删除的ID上执行web.GetItem,我仍然得到null.有什么建议?

Database db = new Database("web");

        if (ID.IsID(id))
        {
            ID itemID = new ID(id);
            //clear data cache
            db.Caches.DataCache.RemoveItemInformation(itemID);

            //clear item cache
            db.Caches.ItemCache.RemoveItem(itemID);

            //clear standard values cache
            db.Caches.StandardValuesCache.RemoveKeysContaining(itemID.ToString());

            //remove path cache
            db.Caches.PathCache.RemoveKeysContaining(itemID.ToString());
        }
Run Code Online (Sandbox Code Playgroud)

pbe*_*ing 5

看起来你错过了预取缓存,以下是如何获取它:

private Cache GetPrefetchCache(Database database)
    {
        foreach (var cache in global::Sitecore.Caching.CacheManager.GetAllCaches())
        {
            if (cache.Name.Contains(string.Format("Prefetch data({0})", database.Name)))
            {
                return cache;
            }
        }
Run Code Online (Sandbox Code Playgroud)

而且html缓存还包括:

private void ClearAllHtmlCaches() 
{
     foreach (var info in Factory.GetSiteInfoList())
     {
         info.HtmlCache.Clear();
     }
}
Run Code Online (Sandbox Code Playgroud)

  • 这里的GetPrefetchCache和ClearAllHtmlCaches是清除站点的所有缓存还是只根据itemID?由于这里的要求是基于itemID (2认同)