Opera have a cache manager. The blocks implementing Opera\CoreBundle\BlockType\CacheableBlockInterface
will have added configurations to the admin.
1 minute
, 2 days
or 1 month
/post/{post_slug}
will not share the same cache if you go on page /post/my-first-post
and /post/my-other-post
/hello?query=exemple
and /hello?query=other-exemple
don’t have the same cache. On the contrary, disabled, they will share the same cache.Imagine you have a block ExempleBlock already created. (Doc: create your own block)
To make the block cachable, it must implements the Opera CacheableBlockInterface
use Opera\CoreBundle\BlockType\BlockTypeInterface;
use Opera\CoreBundle\BlockType\BaseBlock;
use Opera\CoreBundle\Entity\Block;
use Opera\CoreBundle\BlockType\CacheableBlockInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ExempleBlock extends BaseBlock implements BlockTypeInterface, CacheableBlockInterface
{
// [...]
public function getCacheConfig(OptionsResolver $resolver, Block $block)
{
// By default empty.
// So the block will be configured only by admin
$resolver->setDefaults([]);
}
}
You can also set default configuration for your block.
public function getCacheConfig(OptionsResolver $resolver, Block $block)
{
// Will still be on admin but with default values
$resolver->setDefaults([
'cache_key' => 'block_garage_'.$this->someVariable,
'vary_query_string' => false,
'expires_after' => '1 hour',
]);
}