cache-block strikes again

Author Message

Pablo Pernot

Thursday 26 March 2009 10:38:38 am

Hi all,

Need help about cache-block within menu and submenu.

here is my code :

The cache-block works well with $mainMenu, $menu but it won't work with the highlight of the submenu $subMenu, $sub and $sub.is_selected.

Is there any good practice and clue about managing menu and submenu with cache-block ?

thanks

an extract from the code below :

<div id="header">
    <ul id="menu-niv-1">
    <!--cdlBloc ordre="8"-->
    {def $secteur=fetch('content','node',hash('node_id', ezini( 'NodeSettings', 'VotreSecteurActivite', 'config.ini.append.php')))}
    {def $mainMenu=treemenu( $module_result.path, ,
                                        array('actualite','offre','groupe','contact'), 0, 1 )}
   
    {cache-block keys=array($mainMenu,$menu,$secteur)}
    {foreach $mainMenu as $menu}
        {if $menu.is_selected}
        	{if eq($menu.node.class_identifier,'actualite')}
            	<li id="actualite"><a href={$menu.url_alias|ezurl} id="actif" accesskey="2"><span>{$menu.text|upfirst}</span></a>
                    {if eq($menu.has_children, '1')}
                    	<ul id="menu-niv-2">
                        {def $subMenu=treemenu( $module_result.path, ,
                                        array('master_press','master_evenement','master_communique','gallery'), 1, 1 )}
                                        
                                        
                            {cache-block keys=array($subMenu)}            
                        	{foreach $subMenu as $sub}
                            	{cache-block keys=array($sub.node.class_identifier)}
                                {if $sub.is_selected}
                                	{if eq($sub.node.class_identifier,'master_communique')}
                                    	<li id="communiques"><a href={$sub.url_alias|ezurl} id="on"><span>{$sub.text|upfirst}</span></a></li>
                                    {/if}
                                    {if eq($sub.node.class_identifier,'master_evenement')}
                                    	<li id="evenements"><a href={$sub.url_alias|ezurl} id="on"><span>{$sub.text|upfirst}</span></a></li>
                                    {/if}
                                    {if eq($sub.node.class_identifier,'master_press')}
                                    	<li id="revue-presse"><a href={$sub.url_alias|ezurl} id="on"><span>{$sub.text|upfirst}</span></a></li>
                                    {/if}
                                    {if eq($sub.node.class_identifier,'gallery')}
                                    	<li id="phototheque"><a href={$sub.url_alias|ezurl} id="on"><span>{$sub.text|upfirst}</span></a></li>
                                    {/if}
                                {else}
                                	{if eq($sub.node.class_identifier,'master_communique')}
                                    	<li id="communiques"><a href={$sub.url_alias|ezurl}><span>{$sub.text|upfirst}</span></a></li>
                                    {/if}
                                    {if eq($sub.node.class_identifier,'master_evenement')}
                                    	<li id="evenements"><a href={$sub.url_alias|ezurl}><span>{$sub.text|upfirst}</span></a></li>
                                    {/if}
                                    {if eq($sub.node.class_identifier,'master_press')}
                                    	<li id="revue-presse"><a href={$sub.url_alias|ezurl}><span>{$sub.text|upfirst}</span></a></li>
                                    {/if}
                                    {if eq($sub.node.class_identifier,'gallery')}
                                    	<li id="phototheque"><a href={$sub.url_alias|ezurl}><span>{$sub.text|upfirst}</span></a></li>
                                    {/if}
                                {/if}
                                {/cache-block}
                            {/foreach}
                        	{/cache-block}
                        </ul>
                    {/if}
                </li>
			{/if}

[.....]

Pablo Pernot
http://www.smartview.fr
http://www.areyouagile.com

Piotrek Karaś

Thursday 26 March 2009 9:13:19 pm

Pablo,

When you think about it, it's only obvious it won't work. I don't know how your website works, but I assume you have a flat main menu and a multi-level contextual submenu. Let's say there are 10 main categories for main menu, and each category contains 20 pages with links for each page. This gives a total of 200 pages, right?

Now, you've placed submenu cache block, with its keys, within the main menu cache block. Main menu has 10 combinations, submenu has 200 combinations. When do you expect the submenu to have a chance to refresh?

{* This won't work! *}
{cache-block keys=$mainmenu_keys}
    {MENU HERE}
    {SUBMENU HERE}
{/cache}

If you need to have both menus packed into one cache-block, you have set expiry keys combination that would serve the smalles possible entity, in this case a submenu item:

{* This should work! *}
{cache-block keys=$submenu_keys}
    {MENU HERE}
    {SUBMENU HERE}
{/cache}

We hardly ever can do without two separate cacheblocks, one for main menu, one for submenu:

{* This works! *}
{cache-block keys=$mainmenu_keys}
    {MENU HERE}
{/cache}
{cache-block keys=$submenu_keys}
    {SUBMENU HERE}
{/cache}

This approach gives you the possibility to further optimize your menus separately! For $mainmenu_keys you only need an array of 10 node combinations + one combination for all other tools (login, edit, etc.).

For many websites it is not necessary to have all the nodes in the menu. Usually, you place navigation/folder-like items to act as a menu, while multiple articles are never shown there, only in the breadcrumb navigation.

Imagine this scenario: your website has:
- 10 main menu categories
- 200 submenu categories
- 10000 articles

If you base your menu cache on the entire path, you will end up with at least 10210 cache files stored in your cache directory for a menu that only has 200 combinations! You're most likely to be forced to clear your cache-blocks before it ever gets completely generated ;)

What you can do is calculate proper cache-block exipry keys based on depth, class, etc. So that they really reflect what's going on in your menu.

--
Company: mediaSELF Sp. z o.o., http://www.mediaself.pl
eZ references: http://ez.no/partners/worldwide_partners/mediaself
eZ certified developer: http://ez.no/certification/verify/272585
eZ blog: http://ez.ryba.eu

Pablo Pernot

Thursday 26 March 2009 11:31:16 pm

Thanks for your feedback.

What about nested cache-block ?


{cache-block keys=$mainmenu_keys}
   {MENU HERE}
     {cache-block keys=$submenu_keys}
        {SUBMENU HERE}
     {/cache}
     {cache-block keys=$submenu_keys}
        {SUBMENU HERE}
     {/cache}
{/cache}

Is that different than your proposition ?

{cache-block keys=$mainmenu_keys}
   {MENU HERE}
{/cache}
{cache-block keys=$submenu_keys}
   {SUBMENU HERE}
{/cache}

Pablo Pernot
http://www.smartview.fr
http://www.areyouagile.com

Piotrek Karaś

Friday 27 March 2009 12:37:50 am

What about nested cache-block ?

Nested cache-blocks work, but you can't include detailed rules in general rules because detailed rules will be cached within general rules. This would make sense:

{cache-block $submenu_keys}
    {cache-block $mainmenu_keys}
        MAINMENU
    {/cache-block}
    SUBMENU
{/cache-block}

--
Company: mediaSELF Sp. z o.o., http://www.mediaself.pl
eZ references: http://ez.no/partners/worldwide_partners/mediaself
eZ certified developer: http://ez.no/certification/verify/272585
eZ blog: http://ez.ryba.eu

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.