Issues

ZF-9240: Zend_Cache_Manager does not allow custom backend

Issue Type: Improvement Created: 2010-02-23T06:50:52.000+0000 Last Updated: 2010-04-23T13:49:23.000+0000 Status: Resolved Fix version(s): - 1.10.4 (28/Apr/10)

Reporter: Renan de Lima (renanbr) Assignee: Ramon Henrique Ornelas (ramon) Tags: - Zend_Cache

Related issues: - ZF-9134

Attachments: - patch.diff

Description

This is a mix of improvement and bug fix. When I tried to use a custom backend class as cache backend, Zend_Cache_Manager fails. The sample below is about Zend_Cache_Backend_ZendServer_ShMem just to make easer to understand what's happening, but you can reproduce this using any other custom class.

h3. Usage

<pre class="highlight">
<?php

$manager = new Zend_Cache_Manager();
$manager->setCacheTemplate('custom',
                           array('frontend' => array('name' => 'Core'),
                                 'backend'  => array('name' => 'Zend_Cache_Backend_ZendServer_ShMem')));

$backend = $manager->getCache('custom')->getBackend();

h3. Expected result

$cache instanceof Zend_Cache_Backend_ZendServer_ShMem; // true

h3. Actual result

{quote} Fatal error: Uncaught exception 'Zend_Cache_Exception' with message 'file Zend/Cache/Backend/ZendCacheBackendZendserverShmem.php not found in include_path' in /path/to/library/Zend/Cache.php:208 Stack trace:

0 /path/to/library/Zend/Cache.php(147): Zend_Cache::throwException('file Zend/Cache...')

1 /path/to/library/Zend/Cache.php(93): Zend_Cache::_makeBackend('Zend_Cache_Back...', Array, false, false)

2 /path/to/library/Zend/Cache/Manager.php(172): Zend_Cache::factory('Core', 'Zend_Cache_Back...', Array, Array)

3 /var/www/sample.php(8): Zend_Cache_Manager->getCache('custom')

4 {main}

thrown in /path/to/library/Zend/Cache.php on line 208 {quote}

Actually it's perfectly expected, but there is no way to bypass this exception using Zend_Cache_Manager::setCacheTemplate() nor Zend_Cache_Manager::setTemplateOptions() once latest arguments to Zend_Cache::factory() is not available through Zend_Cache_Manager. Those arguments are necessary for custom backend usage. A nice improvement could be implemented.

http://framework.zend.com/manual/en/…

h3. Suggest

Zend_Cache::factory() has 4 arguments. See a slice of the code from Zend/Cache.php file:

<pre class="highlight">
/**
 * @param mixed  $frontend        frontend name (string) or Zend_Cache_Frontend_ object
 * @param mixed  $backend         backend name (string) or Zend_Cache_Backend_ object
 * @param array  $frontendOptions associative array of options for the corresponding frontend constructor
 * @param array  $backendOptions  associative array of options for the corresponding backend constructor
 * @param boolean $customFrontendNaming if true, the frontend argument is used as a complete class name ; if false, the frontend argument is used as the end of "Zend_Cache_Frontend_[...]" class name
 * @param boolean $customBackendNaming if true, the backend argument is used as a complete class name ; if false, the backend argument is used as the end of "Zend_Cache_Backend_[...]" class name
 * @param boolean $autoload if true, there will no require_once for backend and frontend (usefull only for custom backends/frontends)
 */
public static function factory($frontend, $backend, $frontendOptions = array(), $backendOptions = array(), $customFrontendNaming = false, $customBackendNaming = false, $autoload = false);

Zend_Cache_Manager doesn't allow us to use fifth, sixth nor seventh arguments of this method. We could add to Zend_Cache_Manager::$_optionTemplates new optional entries.

<pre class="highlight">
array(
    'sample' => array(
        'frontend' => array(
            'name'          => 'something',
            'custom_naming' => false,       // HERE (fifth)
            'options'       => array(),
        ),
        'backend' => array(
            'name'          => 'another-thing',
            'custom_naming' => false,       // HERE (sixth)
            'options'       => array(),
        ),
        'autoload' => false,                // and HERE (seventh)
    ),
)

Attached is a patch that implements that improvement. Below is another sample, this time using the new template arguments.

<pre class="highlight">
<?php

$manager = new Zend_Cache_Manager();
$manager->setCacheTemplate('custom',
                           array('frontend' => array('name' => 'Core'),
                                 'backend'  => array('name'          => 'Zend_Cache_Backend_ZendServer_ShMem',
                                                     'custom_naming' => true)),
                                 'autoload' => true);

$backend = $manager->getCache('custom')->getBackend();

Comments

Posted by Renan de Lima (renanbr) on 2010-02-23T06:57:02.000+0000

attached file that implements this improvement

Posted by Renan de Lima (renanbr) on 2010-02-23T07:03:59.000+0000

ZF-9240 contains a patch that solve this issue reported by ZF-9134

Posted by Ramon Henrique Ornelas (ramon) on 2010-04-23T13:44:54.000+0000

Correction in revision 21977.

Posted by Ramon Henrique Ornelas (ramon) on 2010-04-23T13:49:22.000+0000

Attributed fix version for Mini Release.

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts