Forums / Setup & design / Weird Caching problem

Weird Caching problem

Author Message

Massimo Sanna

Friday 27 January 2006 1:35:30 am

Hi there, I'm actually optimizing the speed of a ez3 website. I activated every option in site.ini (templatecompile, viewcaching, templatecompress, templateoptimize and so on).
It works really _REALLY_ fast and smoothly, but there's a problem.
I created a left menu navigation which shows the childrens under the current node, and if you arrived at the last level it keeps on showing the other elements at the same level.
If I enable the TemplateCompile option, it works ok the first time it loads it, and until the cache expires you can see this menu, then after 2 hours if you try to open again one of the pages you no longer see the left menu.
I guess it could be a key problem in cache-block or something like that?

This is the code snippet which doesn't work:

{cache-block keys=array($leftMenus, $module_result.node_id)}
					<div id="leftmenu">
<ul id="sottosezioni">

{def $conteggio=fetch('content', 'list_count', hash('parent_node_id', $module_result.node_id ) )}
{if $conteggio|ne(0)}
	{let leftMenus=treemenu($module_result.path,$module_result.node_id,true(),sub($node.depth,1),1)}
	{section name=leftMenu loop=$leftMenus}
	{section show=$leftMenu:item.id|eq($module_result.node_id)}
	<li><a href={$leftMenu:item.url_alias|ezurl} title="{$leftMenu:item.text}" class="active">{$leftMenu:item.text}</a></li>
	{section-else}
	<li><a href={$leftMenu:item.url_alias|ezurl} title="{$leftMenu:item.text}">{$leftMenu:item.text}</a></li>
	{/section}
	{/section}
	{/let}
{else}
	{let leftMenus=treemenu($module_result.path,$module_result.node_id,true(),sub($node.depth,2),1)}
	{section name=leftMenu loop=$leftMenus}
	{section show=$leftMenu:item.id|eq($module_result.node_id)}
	<li><a href={$leftMenu:item.url_alias|ezurl} title="{$leftMenu:item.text}" class="active">{$leftMenu:item.text}</a></li>
	{section-else}
	<li><a href={$leftMenu:item.url_alias|ezurl} title="{$leftMenu:item.text}">{$leftMenu:item.text}</a></li>
	{/section}
	{/section}
	{/let}	
{/if}
</ul>
					</div>
{/cache-block}

Grenland Web (Jan Kudlicka)

Friday 27 January 2006 2:28:34 am

Hi Massimo,

the problem with your code is the following:

In the cache block you are using variable $node (for example in sub($node.depth,1)). This variable does not exist when the <i>content/view</i> view will use the cached version of the page (understand the result from the <i>content/view</i> view = the output of <i>/node/view/full.tpl</i> or its override).

When you remove the caches and you are accessing a page the first time, eZp has to render <i>/node/view/full.tpl</i> (or its override) first and the template system creates $node (which you are using in the template). The rendered page is then stored in the cache and the template system continues with rendering <i>pagelayout.tpl</i>. Because the cache-block does not exist, the code is processed and everything is fine because the template system still remembers the $node variable.

But once the cache-block has expired (= in 2 hours) and the template systems tries to process the code (of the cache-block) once again, the cached version of the page is used and that's why there is no $node.

<b>Solution: do not use $node.depth. Use $module_result.content_info.node_depth instead.</b>

Recommendation: there is no need to expire cache block in the time domain. Use expiry="0" in the cache-block. The cache-block will expiry only when any content on the site is (re-)published. (You can go even further and try to use the subtree_expiry parameter.)

Jan Kudlicka | System Developer @ Grenland Web | http://www.grenlandweb.no

Massimo Sanna

Friday 27 January 2006 2:32:49 am

Thank you thank you!
You solved my problem! Now it works ok!
And the trick of putting expiry=0 works flawlessly.
I also planned to use the subtree_expiry feature on some items which could benefit from it :)
I will pay you a beer hehe

Max