user1725266
user1725266

Reputation: 466

Clearing .NET Cache for Sitecore

I need to clear a Cache which is being used for a custom module in Sitecore, what's the best practice to clear it?

We're currently adding keys to the cache with

System.Web.HttpContext.Current.Cache.Add(key, obj, dependencyKey, DatTime.MaxValue);

This cache does not get cleared out on publish though and I need to clear it manually. I know it is possible to hook into Sitecores publish event, but need a way to clear it.

Tried removing keys with:

System.Web.HttpContext.Current.Cache.Remove(key);

But it did not do the trick

Upvotes: 2

Views: 838

Answers (1)

Patrick Perrone
Patrick Perrone

Reputation: 326

There is no .Clear() method.

You can iterate over all the entries and remove them by their key (or ID), using the GetEnumerator method of the Cache class.

IDictionaryEnumerator enumerator = Cache.GetEnumerator();
while(enumerator.MoveNext()) 
{
    Cache.Remove(enumerator.Key); 
}

I would run this code in a publish:end:remote event.

Upvotes: 1

Related Questions