Cache invalidation


When data is stored in Sitefinity CMS cache using the built-in CacheManager, there is a mechanism to invalidate the cache when the items have been updated or a given time has passed. To do this, you need to pass ICacheItemExpiration objects when adding items to the cache.


You can use one of the built-in ICacheItemExpiration types - AbsoluteTime, SlidingTime, or DataItemCacheDependency, or you can implement a custom ICacheItemExpiration type.

AbsoluteTime and SlidingTime

These two ICacheItemExpiration types implement the time-based expiration.

  • AbsoluteTime expiration sets how long the data will stay in cache after it has been added.

  • SlidingTime is similar to AbsoluteTime, but whenever the cached items are accessed, the expiration time is reset to the given value. This is used to keep items that are frequently used in the cache and discard the items that are not.

NOTE: If cached items are accessed frequently and only sliding time is used as cache expiration, it can take a long time before the cache is invalidated. It is best practice to use sliding time expiration in combination with DataItemCacheDependency


Absolute time and sliding time invalidation options are great when it is not important whether users get outdated data for given periods of time. If it is important that users must get the latest version of the data items, you need to use DataItemCacheDependency. It invalidates the cache when a particular item or items of a particular type are created, updated, or deleted.

It is a good practice to use DataItemCacheDependency in combination with SlidingTime or AbsoluteTime expiration objects.

DataItemCacheDependency can be instantiated using one of the available overloads of the constructor:

  • DataItemCacheDependency(IDataItem trackedItem)
    Cache is invalidated when a particular item is updated or deleted.
  • DataItemCacheDependency(Type trackedItemType, Guid trackedItemId)
    Cache is invalidated when an item of a particular type and with a particularID is created, updated, or deleted.
  • DataItemCacheDependency(Type trackedItemType, string key)
    Cache is invalidated when item of a particular type with provided custom key is created, updated, or deleted.

Under the hood DataItemCacheDependency works using the subscribe – notify pattern. When some objects are added to the cache using DataItemCacheDependency expiration, the cache subscribes for notifications for given type and given key. Then, at some point when items are updated, a notification is fired, using the CacheDependency.Notify() method, which invalidates the cache. For built-in content types and dynamic content types, created using the Module builder, when an item is created, updated, or deleted, Sitefinity CMS fires notifications with the following keys:

  • key = null

  • key = itemId

  • key = item.Provider.ApplicationName

  • key = itemStatus + separator + itemId

  • key = itemStatus + separator + item.Provider.ApplicationName

NOTE:  In NLB setup the notifications are distributed to all nodes. This way, when an item is created, updated, or deleted on a given node, the cache is invalidated on all nodes.

Invalidate the cache when specific item is updated

When a single item is cached, you can monitor for changes for only this item. To do this, you instantiate the DataItemCacheDependency with the item’s type and the item’s ID. Item’s ID can be passed both as a GUID or as a string.


Invalidate the cache when any item of given type is updated

If you pass null for the value of the key, the cache is invalidated when any item of the provided type is created, updated, or deleted.


Invalidate the cache when published items are updated

You can also specify a key that invalidates the cache only when a published version of an item is modified or deleted. To do this, you can use the OutputCacheDependencyHelper class.


Invalidate the cache on custom event

The key in a DataItemCacheDependency can be any string. This means that a custom key can be created and used for invalidating the cache when some specific event occurs. To invalidate the cache by a given key, you need to call CacheDependency.Notify(). Usually, you subscribe to an IDataItem event and notify the custom key from there. For more information, see IDataEvent.


Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?