Cache tips & tricks part 2


David te Kloese

So you've read my previous post and applied cache all over your site. All pages are lighting fast and your sysadmin reports the database seems to have a lot less heat on it. So you're done! But after a while there seem to be some complaints from editors that their updates aren't showing... what to do now?

This blog is part of a series, you can read the previous:
- Cache tips & tricks part 1

Clear cache

So you've came as far as setting cache on your just created masterpiece. But now your editor is complaining they can't see their changes. What can and should you do now?

By now you've probably found the shiny Clear cache button in the System module.

System module - clear cache button

So as soon as an editor calls, head over to this module and clear all the cache and they're good to go. Even better make them Global Admin so they can do it themselves...

I hope I don't have to explain why you don't want an editor to be a Global Admin!

It isn't even a good idea to often hit the clear cache button. I've scrolled down a bit in the screenshot to show you some statistics. And this is from a simple site. Imagine what would happen the second after you've cleared the cache and hundreds or more visitors come to your website. It needs to rebuild the cache and all at once... your happy sysadmin comes running in that both the system and database are going into overtime.

So all the benefits of having cache in place are gone. As a matter of fact if the content editors are often updating the content and clearing the cache its probably even worse now.

Now what? Clear cache after working hours? Create a scheduled task for it... or can't we just delete one of those cache items from that debug cache inspector module thing you've showed in Cache tips & tricks part 1?

Cache dependencies

Great news: Kentico has something called Cache dependencies that can do this for you. As long as you've register your cache to 'listen' to it. Cache dependencies allow Kentico to clear cached data when related objects are updated.

"The system uses dummy cache keys to create dependencies between cached data and other objects. Dummy keys are cache items without any data that represent objects or groups of objects. When an object is modified, the system "touches" the corresponding dummy keys, which causes the cache to delete all items that depend on the given dummy keys"

So how does it work? You might have noticed on the Web part properties just below the Partial cache minutes setting a checkbox for dependencies and an empty text field.

Web part property set cache dependency

By selecting the "use default cache dependencies" the cache is cleared whenever you update a property on this Web Part. Seems logical, but we want more.
 
  • Kentico has a large number of dependencies you can create by adding them in the text field
  • You can even add multiple by adding them each to a new line
  • They can be specified for Documents based on
    • path
    • all child nodes of a path
    • page type
    • etc.
  • Also other objects like Users, Custom table, Media Items, Templates

For a more detailed list have a look on Kentico Devnet!

Usually most of your content will be set based on Documents.

Some examples:



Web part showing title and body text of my contact page. Clear the cache if the contact page changes:

documentid|85

Web part shows all news items on the site. Clear the cache if something happens to a news item:

nodes|kenticosite|cms.newsitem|all

Web part showing a list of projects from its child nodes

node|kenticosite|/projects/|childnodes
 

Dynamic dependency

This is awesome! But wait do I have to set this up for every page? This CMS has dynamic content you know. My editors won't be able to create these dependencies! Luckily Kentico has a feature for this. You can use Macros in these fields! So let's look at the list one more time:
 

Web part showing title and body text of my contact the current page. Clear the cache if this page changes:
documentid|{ % CurrentDocument.DocumentID %}
 

Web part shows all news items on the current site. Clear the cache if something happens to a news item on this site:
nodes| { % CurrentSite.CodeName %}|cms.newsitem|all
 

Web part showing a list of projects any child pages of the current path
node|kenticosite|/{ % CurrentDocument.NodeAliasPath %}/|childnodes
 

What parts of the dependency you want to make dynamic is up to you. If you need to test your work you can use the same method as in Cache tips & tricks part 1, but without the need of clearing the cache manually!

If you use a macro in your cache dependency make sure you haven't disabled the rendering of macro's in you Web Part settings.

Web Part settings macro in dependency

In the next parts I'll continue about Caching in Macros, custom code and more...