EmanueleMinotto / TwigCacheBundle

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

Allow PSR-6 caching adapter #12

Closed rvanlaak closed 8 years ago

rvanlaak commented 8 years ago

We moved away from the DoctrineCacheBundle lately and use the PSR-6 CacheBundle now. Would be great if this bundle could support both.

@Nyholm what would it take to make this bundle compatible? Or should https://github.com/asm89/twig-cache-extension be changed for this?

Nyholm commented 8 years ago

Since the asm89/twig-cache-extension library has wrapped the Doctrine cache within DoctrineCacheAdapter you could quite easily start using PSR-6 instead of Doctrine. You just need to make sure to verify the type of the twig_cache.service. If it is PSR-6 then do not change, if it is Doctrine use the DoctrineAdapter to make it PRS-6 (see below.) And that is all the change you need to do.

But ideally, you could remove the extra abstraction layer and relay on the PSR-6 interfaces. Ie removing Asm89\Twig\CacheExtension\CacheProvider\DoctrineCacheAdapter and Asm89\Twig\CacheExtension\CacheProviderInterface. That will make the library smaller and easier to reuse.


To make it easy for users to upgrade you should look into the DoctrineAdapter. With some minor work in a compiler pass you can be PSR-6 compatible without the user need to update their configuration.

// Just to give an idea. This code needs some work..
$pool = $this->container->get('twig_cache.service');
if ($pool instanceof \Doctrine\Common\Cache\Cache) {
  $pool = new DoctrineCachePool($doctrineCache);
}
if (!$pool instanceof CacheItemPoolInterface) {
  throw new \Exception();
}
rvanlaak commented 8 years ago

Made https://github.com/asm89/twig-cache-extension/pull/32 which introduces the PsrCacheAdapter which makes it even easier to migrate because it allows injection of a PSR-6 CacheItemPoolInterface that still extends the bundle's CacheProviderInterface.

Next to the cache_adapter configuration I already had in place, adding one extra configuration line was enough:

# parameters.yml
twig_cache.adapter.class: Asm89\Twig\CacheExtension\CacheProvider\PsrCacheAdapter

Because of the Symfony CacheBundle, after that I was able to set the cache.provider.mycache service.

# config.yml
cache_adapter:
    providers:
        twig_apcu:
            factory: 'cache.factory.apcu'

twig_cache:
    service: cache.provider.twig_apcu

After the PR is accepted, I'll add a PR here too for the documentation change.

Edit: https://github.com/EmanueleMinotto/TwigCacheBundle/pull/14 is created but errors because other PR was not accepted yet

rvanlaak commented 8 years ago

Adapter was merged, you can do a composer update and use the new configuration as mentioned above.