Partial Sitecore Cache Clearing by Item

Sometimes you just want to clear part of the Sitecore cache and not the entire thing. Taking a page out of the Staging module philosophy, you can easily clear cache by specific items.

First, make sure you understand the basics of Sitecore cache and how requests are handled.

The example method below, clears an enumerated list of items by GUID given a database context.


public static void ClearDataItemCache(Database database, IEnumerable ids)

{

    Cache prefetchCache = GetPrefetchCache(database);

    foreach (ID id in ids)

    {

        if (!ID.IsNullOrEmpty(id))

        {

            database.Caches.ItemCache.RemoveItem(id);

            database.Caches.DataCache.RemoveItemInformation(id);

            database.Caches.StandardValuesCache.RemoveKeysContaining(id.ToString());

            if (prefetchCache != null)

            {

                prefetchCache.Remove(id);

            }

        }

    }

}

Advertisements

About Dave
Certified Sitecore Developer.

3 Responses to Partial Sitecore Cache Clearing by Item

  1. Mike Reynolds says:

    Nice and simple!

    Thanks Dave!

  2. Pingback: Tweets that mention Partial Sitecore Cache Clearing by Item « Everything Web -- Topsy.com

  3. niels says:

    Hey Dave

    Can you tell me where I or how I can found the

    “GetPrefetchCache” method

    I am using sitecore 6.1.

    Hope you can help me!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: