Forums / Setup & design / Caching and variables.

Caching and variables.

Author Message

martin k

Thursday 16 March 2006 8:26:55 am

Hi,

I want to enable cache to speed up site access. I'm quite a newbie whent it comes to enabling caching, but this is what I've done in override/site.ini.append.php:

[TemplateSettings]
TemplateCache=enabled
TemplateCompile=enabled
TemplateCompression=enabled
TemplateOptimization=enabled

[RegionalSettings]
TranslationCache=enabled

[ContentSettings]
ViewCaching=enabled
PreViewCache=disabled

Now I have some problems with accessing variables like $node.node_id, etc. When I enable caching with cache blocks, I get all kind of weird problems like images not being updated in a gallery before the cache expires. I read a forum post about using $module_result.content instead, but it's only available in pagelayout.tpl? I couldn't access this variable in any of my override templates.

Anyway, here is an example of a override template where I'm using a cache block.

{* beginning of template *}

{set-block scope=root variable=cache_ttl}0{/set-block}

{cache-block expiry=20 keys=$uri_string}
{* some code for a image gallery *}
{foreach $galleries as $m}
        {if or(eq($m.node_id, $node.node_id), eq($node.parent_node_id, $m.node_id))}
                <p class="redcolor"><a class="redcolor" href={$m.url_alias|ezurl}>{$m.name|wash|upcase}</a></p>
        {else}
                <p><a href={$m.url_alias|ezurl}>{$m.name|wash|upcase}</a></p>
        {/if}
{/foreach}


{/cache-block}

Now the big question here is if I'm doing this the right way. I've had a hard time gathering some scattered information about how to implement cache in eZ. I guess I just can't use cache-blocks everywhere, and have to test and try if it works in different settings?

Betsy Gamrat

Thursday 16 March 2006 9:08:05 am

The key is in the <b>keys</b>.

I like to use the node id in the keys to ensure the content is properly cached.

For example:

keys=array("details",$node.node_id)

I disable ViewCaching and rely on the template caching.

I've never tried to preserve variable values between template accesses.

There are also additional static caching features in eZ.

If you need to support a very high level of traffic, you can do several things:

Preprocess the home page (demo at http://wirehopper.com/php/preprocess.php, article in Dr. Dobb's Journal Jan 2006)

Modify pagelayout.tpl to support a very streamlined delivery of content and extract it using a cron job.

Use the CMS to build PHP. That way, the content is still dynamic, but it is lightning fast.