Forums / Developer / node_view_gui and cache

node_view_gui and cache

Author Message

Atle Pedersen

Friday 10 July 2009 4:46:25 am

I have the following situation:

{foreach $nodelist as $n}
{node_view_gui content_node=$n view='complex_data''}
{/foreach}

What I would like the cache system to do here is to create and maintain a cache file for each node and the particular view. This way, when one of the nodes are changed, only the appropriate cache needs to be regenerated.

I've experimented a little bit, but it doesn't seem to work this way. I get a cache file for the main, but not anything for the custom view. So I have to recreate cache for the whole file, including all the other unchanged nodes.

Is there a mechanism for this eZ?

Gaetano Giunta

Friday 10 July 2009 5:08:57 am

Add a {cache-block} statement inside your foreach loop, using node_id as key.

Take care about your objects being modified, though: the cache block might not be expired automatically (read up the onlinde docs about cache blocks for more details)

Principal Consultant International Business
Member of the Community Project Board

Atle Pedersen

Friday 10 July 2009 6:05:52 am

Thank you, I forgot that cache-block can be used in all templates.

I ended up with something like this:

{cache-block keys=array($node.node_id,'loop1') subtree_expiry=$node.url}
  {def $nodelist=fetch(...)}
  {foreach $nodelist as $n}
    {cache-block keys=array($n.node_id,'loop1') subtree_expiry=$n.url}
      {node_view_gui content_node=$n}
    {/cache-block}
  {/foreach}
{/cache-block}

I have only tested it superficially, but it seems to work and give good speed increase compared to uncached version. I do need a {set-block scope=global variable=cache_ttl}0{/set-block} on top though. Probably because I need the list to be updated when children of children are being changed.

One short question, is using $node.url a good way to set subtree_expiry?

André R.

Friday 10 July 2009 7:15:36 am

That might work, but it kind of looks like a hack.
Next time you think about implementing stuff like this, use relations (object or attribute) or structure, then the Content View Cache system is able to clear the cache for you when content is edited meaning you don't need to use cache_ttl=0.

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Gaetano Giunta

Friday 10 July 2009 8:36:56 am

Using $node.node_id should also work as subtree_expiry key.

The ttl=0 on top is there because otherwise the whole template will only be executed when the current node changes and its result stored in the view cache. It will thus not be updated anytime any of the included object or their subchildren change.

I fully agree with Andre that it looks ugly though: using object relations is the best way to get proper change propagation within eZ view caches.

Last but not least, the main question, that has been left unsaked: how comes the standard view cache is not enough for your needs?
I can imagine 2 reasons:
- the list of nodes included in the loop is huge
- the templates used to display those nodes are inefficient
Maybe you should look into refactoring somewhere else and not just adding caches...

Principal Consultant International Business
Member of the Community Project Board