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.

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

Web Security for Sitefinity Administrators

The free standalone Web Security lesson teaches administrators how to protect your websites and Sitefinity instance from external threats. Learn to 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?