/
Ideas for improving caching in templates [5.2.1-RC1]

Ideas for improving caching in templates [5.2.1-RC1]

Caching in templates has been there for quite some time, but I bet nobody really know how to use it properly or that it even exists. Below I'll explain how things work currently and ways of improving it.

To start using caching 2 things are needed:

  1. enable tag caching in "Configuration > Website > Advanced" section (see screenshot below)
  2. add "cache_timeout" argument to any tag (value in seconds)

When the "cache_timeout" parameter is given to tag, than tag result is stored in Memcache for specified time (e.g. cache_timeout="3600" is 1 hour). Such approach allows for server to return affected (by caching) page contents almost instantaneously.

To be able to store parsed tag contents in Memcache a unique key for representing that data is required. Since developer isn't providing such key along with "cache_timeout" tag parameter, then it's generated automatically based on 2 input parameters:

  • element identifier
  • key

This is the comparison of values for these parameters for various tags:

TagElement IdentifierKey
m_Cache'pointer:' . abs(crc32($tag['file'])) . '_' . $tag['line']Default + given in "key" parameter
m_Include'template:' . $params['template']Default
m_RenderElement'element_' . $params['name']Default
any other tag'file=' . $template_path . ':' . $prefix . '_' . $tag . '_' . crc32(serialize($params))Default

There are some issues with uniqueness of used element identifiers, which will be discussed later, but note the "Default" key. That key is composition of different parameters (mostly coming from URL of the page) to ensure that tag usage on different pages is cached in that page context and not globally. These are the keys:

  • ID of a used language (e.g. 1 for English, 2 for Russian, etc.)
  • template name from url (e.g. "about/contact-us")
  • marker of current content mode (when viewing page from Admin Console then it will contain extra markup that shouldn't be visible for regular users)
  • ID of current category (since single template can be used to show items from different categories we also remember a category)
  • current page number and sorting (for lists)
  • serial for theme files (change in template content will cause cache to auto-expire)
  • serial for phrases (change in phrase translation, not necessarily used on that template will cause cache to auto-expire)
  • serial configuration (change of configuration settings, not necessarily used on that template will cause cache to auto-expire)

Place for improvements

  1. element identifier of the m_Include and m_RenderElement tags doesn't include their parameters (since output of template inclusion or element rendering might change based on input parameters we end up in same cached output regardless of input parameters)
  2. all but m_Cache tags have no way of specifying caching key (no way to alter caching key when needed)
  3. when cache expires then all visitors need to wait until new cache is built (this is addressed in rATV14103 commit, but not yet merged to In-Portal Repository)

Related Tasks