1

I have a page with a menu component. The menu marks the active element with a different CSS class.

Now I want to cache each menu item page. The different pages come all from the same module/action, the difference is just the ID (foo/bar?item=1).

The problem is that the menu is only cached one time, but I need a cache version for every menu item.

I just tried the cache option "contextual: true", but I think this does not work because the main template (barSuccess) is always the same.

Do you guys have any idea, how to solve this problem?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ownking
  • 1,956
  • 1
  • 24
  • 34

1 Answers1

2

You can force cache key by passing the sf_cache_key to the component:

include_component('menu', 'main', array('sf_cache_key' => $sf_params->get('item')));

This way component will be cached for every value of 'item'.

Another way is to use different sets of parameters:

include_component('menu', 'main', array('item' => $sf_params->get('item')));

This way component will be cached for every value of item as well.

In the first solution you force the cache key. It's useful when you need custom logic to decide if the cache should be generated or not.

The second solution relies on fact that component is cached for every combination of parameter values passed to it (there can be more than one of course) .

Jakub Zalas
  • 35,761
  • 9
  • 93
  • 125
  • Thx for your answer. Is it possible to set this parameter in the components action? I'm asking because I use some components in layout.php and I dont't want to generate the cache keys in layout.php. – ownking Jan 17 '11 at 09:21
  • I'm not sure about that. You could always use a helper to generate the cache key. – Jakub Zalas Jan 17 '11 at 15:50
  • Do you know what has the higher priority? Setting the sf_cache_key for a component or the "contextual: true" setting in cache.yml. Should I use both at all or always only one of them? – ownking Jan 26 '11 at 16:39
  • These are different things. From symfony docs: "When caching a component or a partial, you must decide whether to store a single version for all calling templates or a version for each template. By default, a component is stored independently of the template that calls it. But contextual components, such as a component that displays a different sidebar with each action, should be stored as many times as there are templates calling it. ". If you call your component in one place only it's cache one time only. – Jakub Zalas Jan 26 '11 at 18:27
  • 1
    Cache key is automatically created out of parameters passed to the component. Your template will be cached as many times as many variations of parameter values there are. You can of course force the key by passing the sf_cache_key parameter. To summarize: you don't have to pass the sf_cache_key. You can simply pass different values in one of the component's parameters. I will modify my answer to include this solution. – Jakub Zalas Jan 26 '11 at 18:32
  • Does this also work if the different values are arrays or objects? I'm asking because the build identifier is a string. – ownking Jan 27 '11 at 17:30