ZF2 – Using Cache

One does not simply cache in ZF2. The best way I’ve found is to create a factory in the Service manager to use cache.

<?php
return array(
    'modules' => array(
        'Application',
        'Rest'
    ),
    'module_listener_options' => array(
        'config_cache_enabled' => false,
        'cache_dir' => 'data/cache',
        'config_glob_paths'    => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'ZendCacheStorageFactory' => function() {
                return ZendCacheStorageFactory::factory(
                    array(
                        'adapter' => array(
                            'name' => 'filesystem',
                            'options' => array(
                                'dirLevel' => 2,
                                'cacheDir' => 'data/cache',
                                'dirPermission' => 0755,
                                'filePermission' => 0666,
                                'namespaceSeparator' => '-db-'
                            ),
                        ),
                        'plugins' => array('serializer'),
                    )
                );
            }
        ),
        'aliases' => array(
            'cache' => 'ZendCacheStorageFactory',
        ),
    ),
);

Above is an example of my applicaiton.config.php file.  I’ve created a new StorageFactory object using the alias of “cache”.  This will allow me to create a service manager alias that I can then reference in my application.  The next step involves my controller and a class that is serviceManagerAware.

public function IndexAction()
{
        $this->assembler  = new Assembler($controller->getServiceLocator());
}

My Assembler class implements ServiceManagerAwareInterface and in the construct will simply set the service manager to the passed $sm parameter:

namespace ApplicationModelObj;

use ZendServiceManagerServiceManager;
use ZendServiceManagerServiceManagerAwareInterface;

class Assembler implements ServiceManagerAwareInterface
{
    /**
     * Creates the configuration for the service assembler
     * @param ZendServiceManagerServiceManager $request
     */
    public function __construct(ServiceManager $sm)
    {
        $this->setServiceManager($sm);
        $this->cache = $this->getServiceManager()->get('cache');
    }

    public function setServiceManager(ServiceManager $sm)
    {
        $this->_sm = $sm;
    }

    public function getServiceManager()
    {
        return $this->_sm;
    }
}

Using the cache is slightly different than ZF1.  You no longer can save tags with your cache.

# Simply return a key from the cache
$result = $this->cache->getItem($key);
if (!empty($result)) {
    return $result;
}

# Store a result into cache using the key, $saved will result in a boolean value.
$saved = $this->cache->addItem($key, $value);

# Tag the key when saving
$this->cache->setTags($key, array('tag1', 'tag2'));

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s