Implement cache dependencies

Sitefinity CMS Content widgets and Dynamic content widgets have a built-in mechanism for invalidating the output cache - when the content changes, the cached output is invalidated. When developing your own user or custom widget, to invalidate the cache, you need to implement the appropriate cache dependencies in order. This way, when you create or modify a content item, the widget cache is invalidated in order to reflect the latest changes in the content. For example, if you place an MVC widget displaying news items on the page. When you change the news content, the cache of the page with the widget is invalidated and the controller action is not called.

The following code sample demonstrates how to implement cache dependencies for news items in an MVC widget. In the example below, the Index() action adds a page cache dependency for the news items. Any page that contains the widget and calls the Index() action, has its cache invalidated whenever news content changes.

NOTE: Implementing the IHasCacheDependency interface is optional.

Implement cache dependency for widgets displaying static content

C#
using System.Collections.Generic;
using System.Web.Mvc;
using Telerik.Sitefinity.Data;
using Telerik.Sitefinity.GenericContent.Model;
using Telerik.Sitefinity.Lifecycle;
using Telerik.Sitefinity.Modules.News;
using Telerik.Sitefinity.Mvc;
using Telerik.Sitefinity.News.Model;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Web;

namespace SitefinityWebApp.Mvc.Controllers
{
   [ControllerToolboxItem(Name = "MyNewstWidget", Title = "MyNewsWidget", SectionName = "MyMvcWidgets")]
   public class MyNewsWidgetController : Controller
   {
       public ActionResult Index()
       {
           SubscribeCacheDependency();

           return View();
       }

       private void SubscribeCacheDependency()
       {
           var contextItems = SystemManager.CurrentHttpContext.Items;
           var isBackendRequest = contextItems.Contains(SystemManager.IsBackendRequestKey) && ((bool)contextItems[SystemManager.IsBackendRequestKey]);

           if (isBackendRequest)
           {
               return;
           }

           if (!contextItems.Contains(PageCacheDependencyKeys.PageData))
           {
               contextItems.Add(PageCacheDependencyKeys.PageData, new List<CacheDependencyKey>());
           }

           var existingKeys = (List<CacheDependencyKey>)contextItems[PageCacheDependencyKeys.PageData];
           var keysToAdd = GetCacheDependencyKeys();

           existingKeys.AddRange(keysToAdd);
       }

       private IEnumerable<CacheDependencyKey> GetCacheDependencyKeys()
       {
           var cacheDependencyNotifiedObjects = new List<CacheDependencyKey>();
           //To construct the key we must include two elements
           var newsItemType = typeof(NewsItem);
           var manager = NewsManager.GetManager();
           //The ApplicationName
           string applicationName = manager != null && manager.Provider != null ? manager.Provider.ApplicationName : string.Empty;
           //And the item type
           if (typeof(ILifecycleDataItem).IsAssignableFrom(newsItemType))
           {
               cacheDependencyNotifiedObjects.AddRange(
                   Telerik.Sitefinity.Model.OutputCacheDependencyHelper.GetPublishedContentCacheDependencyKeys(newsItemType, applicationName)
               );
           }
           else
           {
               cacheDependencyNotifiedObjects.Add(new CacheDependencyKey { Key = applicationName, Type = newsItemType });
           }

           return cacheDependencyNotifiedObjects;
       }
   }
}

Implement cache dependency for widgets displaying dynamic content

C#
using System.Collections.Generic;
using System.Web.Mvc;
using Telerik.Sitefinity.Data;
using Telerik.Sitefinity.DynamicModules;
using Telerik.Sitefinity.DynamicModules.Builder.Model;
using Telerik.Sitefinity.GenericContent.Model;
using Telerik.Sitefinity.Mvc;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Utilities.TypeConverters;
using Telerik.Sitefinity.Web;

namespace SitefinityWebApp.Mvc.Controllers
{
   [ControllerToolboxItem(Name = "MyDynamicContentWidget", Title = "MyDynamicContentWidget", SectionName = "MyMvcWidgets")]
   public class MyDynamicContentWidgetController : Controller
   {
       public ActionResult Index()
       {
           SubscribeCacheDependency();

           return View();
       }

       private void SubscribeCacheDependency()
       {
           var contextItems = SystemManager.CurrentHttpContext.Items;
           var isBackendRequest = contextItems.Contains(SystemManager.IsBackendRequestKey) && ((bool)contextItems[SystemManager.IsBackendRequestKey]);

           if (isBackendRequest)
           {
               return;
           }

           if (!contextItems.Contains(PageCacheDependencyKeys.PageData))
           {
               contextItems.Add(PageCacheDependencyKeys.PageData, new List<CacheDependencyKey>());
           }

           var existingKeys = (List<CacheDependencyKey>)contextItems[PageCacheDependencyKeys.PageData];
           var keysToAdd = GetCacheDependencyKeys();

           existingKeys.AddRange(keysToAdd);
       }

       private IEnumerable<CacheDependencyKey> GetCacheDependencyKeys()
       {
           var contentType = TypeResolutionService.ResolveType("YourDynamicTypeHere");
           var cacheDependencyNotifiedObjects = new List<CacheDependencyKey>();
           var manager = DynamicModuleManager.GetManager();
           string applicationName = manager != null && manager.Provider != null ? manager.Provider.ApplicationName : string.Empty;
           cacheDependencyNotifiedObjects.AddRange(Telerik.Sitefinity.Model.OutputCacheDependencyHelper.GetPublishedContentCacheDependencyKeys(contentType, applicationName));
           cacheDependencyNotifiedObjects.Add(new CacheDependencyKey() { Key = contentType.FullName, Type = typeof(DynamicModule) });

           return cacheDependencyNotifiedObjects;
       }
   }
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
New to Sitefinity?