Blog Banner

Clear a Page’s Output Cache Programmatically in Sitefinity

Sitefinity has many options for configuring cache settings for pages. Output Cache Profiles can be created and managed in Sitefinity’s advanced settings for changing global output cache properties. These cache profiles can also be toggled on a per page basis as well.  A default 9.1 instance of Sitefinity will have all pages configured to utilize a 2 minute sliding expiration output cache. This means that when a user or visitor hits a page for the first time the html for that page will be cached for 2 minutes. Due to the sliding expiration, the page will need to go a full 2 minutes without being visited in order for the output cache to clear. 

Hawley Blog

When the “Sliding Expiration” checkbox is disabled, the cache will expire after 2 minutes (or whatever duration is specified) - regardless of users hitting the page.

Depending on the type of data being rendered and how often users are visiting a page, these settings may be adjusted to accommodate.  For instance, if sliding expiration is turned on for a high traffic page, it’s very unlikely that content will appear updated since users will likely always be on the page. If there’s a dynamic element to the data or UI being rendered, it will not be updated in this scenario. Having sliding expiration turned off is one way to remedy this type of page. However, it’s important to understand that if sliding expiration is off, the page will take a performance hit whenever it goes to fetch content every 2 minutes

Recently, we were dealing with a very custom site that had very long page cache durations set for most of their pages. The production environment was also not easily accessible to admin users. Additionally, there were many dynamic components to page widgets where it was useful to periodically clear a page’s cache to get the most updated content / HTML. For an experiment, and a somewhat useful tool, I thought it might be nice to have a tool that could programmatically clear a page’s output cache or blast the cache for all pages. Obviously, this tool would be restricted to admins only. After I did some digging, I was unable to find documentation or a resource that already did this. There is a small snippet somewhat documenting a similar feature, but I failed to implement it in a way that accomplished clearing a page’s output cache.

Cache
After some back and forth with Sitefinity support, I was able to get a working service together that cleared a page’s output cache based on its PageData ID (Guid). Below is the method that takes the ID as a guide (provided from the PageData object) and then clears that page’s output cache.

Cache 2

This method can be utilized by any additional helpers and services for clearing any page or set of pages’ output cache in the event that you need to mass clear that stale html.

Contact_US_CTA

Our marketing team is always on the prowl for useful facts and internet tidbits. Be sure to check our blog often for all of our updates and posts!

Write a review