keithkelly
2 years agoLeader
Can I cache a component based on **parameter** ?
I have a component:
<component id="example-component" category="foo"/>
<component id="example-component" category="bar"/>
<component id="example-component" category="baz"/>
and I'd like to cache it.
<!-- Cache for 60 seconds -->
<@liaMarkupCache ttl="60000" variation="node" anonymousOnly="false" />
<!-- Display different HTML based on parameter. Example: -->
<#assign category_val = env.context.component.getParameter("category")!"" />
<div>${category_val}</div>
But, this results in:
foo
foo
foo
But I'd like:
foo
bar
baz
It looks like Custom Variations may scratch the itch, but I'm having trouble wrapping my head around
${componentCacheSupport.setCacheVariation("user_id_var", user.id)}
Before I go splitting this component up into example_component_foo, example_component_bar, example_component_baz... is there an easier way to cache this? I'd greatly appreciate any tips or pointers!
Yes, the calls to setCacheVariations are at the wrong place as far as I understand the doc (Big caveat: I never used it myself, so just interpreting the docs). They state:
When you create custom variations of the markup cache:
- Create a wrapper component
- Define all variations in the wrapper component using componentCacheSupport
- Call the child component to be cached from within the wrapper component using the component directive
So you actually need two components:
- The wrapper component which sets the cache variation context object by calling something like the following (if you only include this component on a category page as in that case the node's id is the category id. If you want to include the component elsewhere you first need to Get ID of current category )
${componentCacheSupport.setCacheVariation("category_var", coreNode.id)}
- Your actual component which gets cached and which just refers to that variation as you already have it shared in your
<@liaMarkupCache>
🤞