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.
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.
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.
Sign up for our free beginner training. Boost your credentials through advanced courses and certification. Register for Sitefinity training and certification.
To submit feedback, please update your cookie settings and allow the usage of Functional cookies.
Your feedback about this content is important