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

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

问题描述

我有一个带有菜单组件的页面。菜单用不同的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传递给组件来强制使用缓存键:

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天全站免登陆