带有组件的模板的上下文缓存 [英] contextual caching of templates with components

查看:23
本文介绍了带有组件的模板的上下文缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有菜单组件的页面.菜单用不同的 CSS 类标记活动元素.

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

现在我想缓存每个菜单项页面.不同的页面都来自同一个模块/动作,区别只是ID (foo/bar?item=1).

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.

我刚刚尝试了缓存选项contextual:true",但我认为这不起作用,因为主模板(barSuccess)始终相同.

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?

推荐答案

您可以通过将 sf_cache_key 传递给组件来强制缓存 key:

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')));

这种方式组件将为'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) .

这篇关于带有组件的模板的上下文缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆