Cancellazione di un elemento dalla cache di precaricamento in 9.2

Aug 21 2020

Abbiamo basato la nostra implementazione su Cancella cache Sitecore per un elemento dalla cache a livello di codice per cancellare un elemento dalla cache di prefetch come di seguito

Cache prefetchCache = GetPrefetchCache(Database database);
prefetchCache.RemoveKeysContaining(itemID.ToString());

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;
            }
        }
    }  

Ma la classe Cache di 8.2 è stata modificata in ICacheInfo in 9.2 e RemoveKeysContain non è disponibile. C'è un modo per rimuovere un particolare elemento da prefetchCache in 9.2

Risposte

1 HishaamNamooya Aug 21 2020 at 21:29

Sitecore ha modificato l'implementazione in ICacheInfo. Tuttavia, puoi ancora utilizzare il metodo RemoveKeysContainingnella cache dopo aver applicato il casting Sitecore.Caching.Cache.

L' ICacheInfoha implementazioni multiple e la classe Cacheè uno dei implementazione.

Si prega di vedere sotto lo snippet di codice:

Cache prefetchCache = GetPrefetchCache(database);

//Check for null before applying method
if (prefetchCache != null)
{
    prefetchCache.RemoveKeysContaining(itemID.ToString());
}

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 as Cache;
        }
    }
}  

Si noti che la cache proviene dallo spazio dei nomi Sitecore.Caching.Cachedall'assemblySitecore.Kernel.dll