Forums / Developer / Cache-Block-Usage?

Cache-Block-Usage?

Author Message

Marco Zinn

Tuesday 02 December 2003 8:52:53 am

Hi,
i'm missing some good and precise documentation about the usage of cache-blocks in the pagelayout template.

I know, that the general usage should be somehow like:
{cache-block}
... menues / navigation
{/cache-block}
{$module_result.content}
{cache-block}
... footer / navigation
{/cache-block}

Also, you can add keys to determine the different "versions" of a given cache-block.
But how is this all used? What happens with a cache-block without keys? Which keys are used then? Is there any human-understandable system, which determines the cache-blocks filenames in the filesystem?

Marco
http://www.hyperroad-design.com

Paul Borgermans

Tuesday 02 December 2003 9:43:44 am

Hi Marco,

Whether or not you provide keys, the template cache system will create additional ones based on the template file name and path, the position of the cache-block in the file and the current siteaccess. So multiple cache-blocks in a template file with no explicit keys still get a unique key. This works even if different siteaccess use the same design (and templates within). The latter is necessary because the content may differ with siteaccess.

The use of keys is mandatory when you use the cache-block in a template and the content of the cache-block can be different. For view templates, I would recommend at least the node_id +the userid (if different users have different roles). You may add other variableshere too.

For use in pagelayouts and you have navigation menus which remain constant, it is safe to use no keys at all for the surrounding cache-block. Otherwise, you will need a key which discrimnates the different content (eg the user id when roles affect the menus to display).

hth

-paul

eZ Publish, eZ Find, Solr expert consulting and training
http://twitter.com/paulborgermans

Ben Pirt

Wednesday 03 December 2003 5:31:57 am

Hi Marco,
It's very true that good documentation is lacking on the use of cache-block. I have just been investigating it and here is what I found (please correct me, anyone if this is wrong, it would be useful for both me and others)

The cache-block directive caches that block of code and everywhere it is used in a template, the faster, cached version is used. This is all well and good if you have content which doesn't change, but gets more complicated when it does. So, in the case of a menu;

If you have the same menu on every page, then you can just use cache-block without setting a keys variable and it should work fine.

If you have a 2 level nested menu like;

contact
projects
---- project 1
---- project 2
other

and the second level depend which node you are looking at, you need to use the keys variable to cache a different chunk of code for each instance of this. So, in this case, if you were using $module_result.node_id to determine which sub menu to expand, you would do the following;

{cache-block keys=$module_result.node_id}
{code goes here}
{/cache-block}

which will cache a different version of this block for every node.

Going one step further (which I haven't yet, but still) if you have a section of code which is dependent upon two variables for it's output, you use both of these as the key, e.g. if it is dependent upon $module_result.node_id and $user (or whatever the variable which has the current user - I can't recall). You would do;

{cache-block keys=array($module_result.node_id, $user)}
{code goes here}
{/cache-block}

I have just implemented cache-block on my site and it speeded up server response times considerably. Well worth doing. Anyway, hope this helps (and was correct)

All the best,
Ben Pirt

Bård Farstad

Thursday 04 December 2003 2:51:54 am

I've added a new documentation page which should explain this a bit more:
http://ez.no/developer/ez_publish_3/documentation/incoming/cache_block_optimization

Documentation: http://ez.no/doc