Implement cache dependencies

Sitefinity CMS enables you to configure caching for pages. This also includes the rendered output of widgets, which are placed on the requested page. Generally, when widgets display item results, they execute complex logic that retrieves data from the database and, before generating the HTML output, performs additional operation on the queried data, such as sorting or filtering. If the underlying widget logic is executed for every user request, slowdown and performance issues can occur. By executing heavy operations only once during the initial page request, and serving cached data for subsequent page visits, you can achieve  scalability of your web application. For more information, see Administration: Cache settings

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.

Implement cache dependencies for content in WebForms widgets

The following example demonstrates how to implement cache dependencies for news items in a widget:

In the code above, you subscribe cache dependencies for news items by calling the SubscribeCacheDependency method. If the page is requested from the frontend, cache dependency keys are added to the PageData items of the HTTP context. As a result, if there are any changes in the news items, the widget invalidates the cache for the page where you dropped it.

Implement cache dependencies for dynamic content in WebForms widgets

The following code sample demonstrates how to implement cache dependencies for dynamic content items in a custom widget:

In the code above, the GetCacheDependencyObjects method covers the case when you use a list view and details view approach. If the widget is configured to show only a single item, you need to subscribe a dependency only for that particular item. Otherwise, you subscribe a dependency for your dynamic module type. By specifying that the cache dependency keys are added to the page data items of the HTTP context, the widget invalidates the cache for the page, on which you dropped it. Once you add a new item or republish an existing one, the previously generated output cache is invalidated.

Implement cache dependencies for MVC widgets

You can also implement cache dependencies for MVC widgets. 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. 

Was this article helpful?