Share » Learn » eZ Publish » eZ Publish Performance Optimization...

eZ Publish Performance Optimization Part 3 of 3: Practical Cache and Template Solutions

Wednesday 24 January 2007 5:05:00 pm

  • Currently 5 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

An optimized pagelayout template should generate only two or three SQL queries with viewcache enabled. When building a site, you normally have several dynamic elements in your main template pagelayout.tpl. This template will then use most of the total processing time for your site, which is undesirable. To limit this processing time, you can use the cache-block template function.

How it works

The cache-block function stores the result of dynamic template code in a plain HTML/text file to be loaded the next time the same code is requested. For example, a navigation menu usually consists of a list of folders and / or objects. This menu is often the same for most or all pages. You can use a cache-block to store the result of the dynamic code. Consider the following main template file (pagelayout.tpl) example:

<a href="http://december.com/html/4/element/html.html"><span><html></span></a>

<a href="http://december.com/html/4/element/head.html"><head></a>
<a href="http://december.com/html/4/element/link.html"><link</a> rel="stylesheet" type="text/css" href={"stylesheets/core.css"|ezdesign} />
<a href="http://december.com/html/4/element/link.html"><link</a> rel="stylesheet" type="text/css" href={"stylesheets/debug.css"|ezdesign} />
 
{include uri="design:page_head.tpl" enable_glossary=false() enable_help=false()}
</head>
 
<a href="http://december.com/html/4/element/body.html"><body></a>
{include uri="design:page_toppath.tpl"}
 
{cache-block}
{include uri="design:left_menu.tpl"}
{/cache-block}
 
{$module_result.content}
 
{include uri="design:page_copyright.tpl"}
 
</body>
</html>

In this example, we have added a cache-block around the left menu navigation code. This means that the first time the page is loaded, this code is executed and the result is stored in a text file. During successive pageloads, this text file is loaded and no left menu code is executed. This happens until the cache-block expires (which is by default two hours).

Parameters

The cache-block function takes four parameters: keys, expiry, ignore_content_expiry and subtree_expiry.

Keys

The keys parameter is used to define the uniqueness of the cache-block. By default, eZ Publish uses the template name and position of the block as keys. This means that if the cache-block is common for all occurrences of a given template (normally pagelayout.tpl), you do not need to specify any keys. An example of this would be a menu that does not change even for different users or different areas of the site. If you need to specify a key you can use either a single variable or an array, as shown in the following example with cache-blocks that are unique for each URL:

{cache-block keys=$uri_string} ... tpl code {/cache-block}

Here is an example with cache-blocks that are unique for each URL and user:

{cache-block keys=array($uri_string,$current_user.contentobject_id)} 
... tpl code {/cache-block}

Expiry

If you do not specify the expiry parameter, the cache-block will automatically expire in two hours or if new content is published. If this expiry does not fit your needs you can specify the expiry time manually in seconds:

{cache-block expiry=120} ... tpl code {/cache-block}

Ignore content expiry

Sometimes you do not want your cache-blocks to expire when content is published. For example, a footer containing copyright information is not affected by new content and thus does not need to expire. With the ignore_content_expiry parameter you can disable the expiration when content is published:

{cache-block ignore_content_expiry} Cached content, even if an object is published. {/cache-block}

In between the default policy of always expiring the cache-blocks when content is published and the functionality of ignore_content_expiry is the subtree_expiry parameter. With this parameter, you can control the expiration of a cache-block when content in a specific subtree (like /products) is published; there might be a block inside the pagelayout template containing a list of the five latest products. In the following example, the cache-block will expire when there is something published in the /products/bargain subtree or after 30 minutes:

{cache-block expiry=1800 subtree_expiry=/producs/bargain} ... tpl code {/cache-block}

Usage tips

Since there is some processing involved in using the cache-block itself, you should use as few cache-blocks as possible and make them unique using keys. It is often very efficient to have two large cache-blocks that will cache all header / title / path information and footer information. This can be used in combination with a nested menu cache-block as in the example below:

{cache-block keys=$uri_string}   

rel="stylesheet" type="text/css" href={"stylesheets/core.css"|ezdesign} /> rel="stylesheet" type="text/css" href={"stylesheets/debug.css"|ezdesign} /> {include uri="design:page_head.tpl" enable_glossary=false() enable_help=false()} {include uri="design:page_toppath.tpl"} {cache-block} {include uri="design:left_menu.tpl"} {/cache-block} {/cache-block} {$module_result.content} {cache-block} {include uri="design:page_copyright.tpl"} {/cache-block} Note that when using nested cache-blocks, the outermost block will not know if a sub cache-block has or should have expired. As a result, the outermost block should have a shorter expiry than the sub-block.

36 542 Users on board!

Tutorial menu

Printable

Printer Friendly version of the full article on one page with plain styles

Author(s)