EmanueleMinotto / TwigCacheBundle

Symfony Bundle for asm89/twig-cache-extension
59 stars 13 forks source link

Prefix with kernel environment by default #15

Closed rvanlaak closed 6 years ago

rvanlaak commented 8 years ago

We have multiple development-environments on one machine and make use of APCu. This resulted in cache key collisions because the keys aren't prefixed by default, so we had to implement our own "prefix strategy":

use Asm89\Twig\CacheExtension\CacheStrategy\IndexedChainingCacheStrategy;

/**
 * Prefix the key while instantiating strategy with the Symfony kernel environment.
 */
class PrefixCacheStrategy extends IndexedChainingCacheStrategy
{
    private $prefix;

    public function generateKey($annotation, $value)
    {
        $annotation = $this->prefix . '_' . $annotation;
        return parent::generateKey($annotation, $value);
    }

    public function setPrefix($prefix)
    {
        $this->prefix = $prefix;
    }
}

Service definition:

app.twig.strategy.prefix:
    class: 'AppBundle\Twig\PrefixCacheStrategy'
    arguments:
         - { time: '@twig_cache.strategy.lifetime', gen: '@twig_cache.strategy.generational' }
    public: false
    calls:
        - [setPrefix, ['%kernel.environment%']]

Given you also choose IndexedChainingCacheStrategy as a default, what about prefixing the keys with the Symfony kernel environment by default too?

toooni commented 6 years ago

I am not sure here, but I don't think this is a functionality which should be supported by default. Creating your own strategy for this special usecase seems ok to me.