1

How could I integrate Zend Cache Factory into Symfony 2 so that I could change my cache adapter easily in my config.yml?

This is the traditional way to get an adapter using the factory.

use Zend\Cache\StorageFactory;

$cache = StorageFactory::factory(array(
    'adapter' => array(
        'name'    => 'apc',
        'options' => array('ttl' => 3600),
    ),
    'plugins' => array(
        'exception_handler' => array('throw_exceptions' => false),
    ),
));

Source: http://framework.zend.com/manual/2.2/en/modules/zend.cache.storage.adapter.html

Since the zend cache library is already installed by composer. What's the best approach to integrate it into DI container so I can get my cache configured anywhere in the app?

$this->get('my_cache.blog')
dextervip
  • 4,999
  • 16
  • 65
  • 93

1 Answers1

0

Try this. (Bear in mind there might be an issue with the YAML formatting)

services:
    my_cache.blog:
        class:          Zend\Cache\Storage\Adapter
        factory_class:  Zend\Cache\StorageFactory
        factory_method: factory
        arguments:
            - 
                adapter:
                    name: apc
                    options: { ttl: 3600 }
                plugins:
                    exception_handler: { throw_exceptions: false }

Here's more information regarding using factories in Symfony's Dependency Injection component.

Adam Elsodaney
  • 7,722
  • 6
  • 39
  • 65
  • The class Zend\Cache\Storage\Adapter doesn't exist. There is just abstract one Zend\Cache\Storage\Adapter\AbstractAdapter, Symfony DI doesn't accept it – dextervip Nov 03 '13 at 20:37
  • @dextervip Ah sorry I was looking at the docs and assumed it was a class, I see your dilemma now. I'd be surprised if Symfony didn't let you specify an interface either, like `Zend\Cache\Storage \StorageInterface`. I wonder if the `class` option can be omitted? – Adam Elsodaney Nov 03 '13 at 20:55
  • It doesn't accept interface also since it tries to instantiate and I can't omit the class otherwise I get an exception saying it is missing the class. :( – dextervip Nov 03 '13 at 21:58
  • Can you set the class the factory returns (in this case ``Zend\Cache\Storage\Adapter\Apc``) ? – mabe.berlin Nov 05 '13 at 10:05