Issues

ZF-722: Zend_Cache_Frontend_Function::call() Supporting object methods via callback parameter

Issue Type: New Feature Created: 2007-01-05T17:45:03.000+0000 Last Updated: 2007-07-05T14:43:38.000+0000 Status: Resolved Fix version(s): Reporter: Jeremy Giberson (jeremygiberson) Assignee: Fabien MARTY (fab) Tags: - Zend_Cache

Related issues: Attachments:

Description

Currently the Zend_Cache_Frontend_Function::call() function does not support object methods. For two reasons I believe it should support object functions: 1) Use case for caching object method:

<pre class="highlight">
// part class
class part { 
     private $partnum;

     public function __construct($partnum) {
        $this->partnum = $partnum;
     }

     public function display() {
         // renders part and part information in elaborate html
         // relies on super expensive _getPartInfo() for data
         $info = $this->cache->call( array($this, '_getPartInfo') );
         echo "Part: {$this->partnum}";
         print_r($info);
     }

     private function _getPartInfo() {
         // query some databases
         // crunch some numbers
         // pretify data
         // ..
         return $info;
     }

     public function initCache() {
         // set up new Zend_Cache_Frontend_Function()
         // ..
     }
}

// main script
// parts list contains a list of part numbers
foreach ($partslist as $partnum) {
     $p = new Part($partnum);
     $p->display();
}
  1. The Zend_Cache_Frontend_Function::call() documentation specifically states: {quote}Using the call() function is the same as using call_user_func_array() in PHP: {quote}

In order to accommodate this functionality the Zend_Cache_Frontend_Function::call() function's first parameter $name needs to allow for callbacks (http://us2.php.net/manual/en/…). My first implementation resulted in:

<pre class="highlight">
//Zend/Cache/Frontend/Function.php
    private function _makeId($name, $parameters)
    {
        /** callback handling fix **/
        if ( is_array($name) && is_object($name[0]) && is_string($name[1]) ) {
            $name = get_class($name[0])."::".$name[1];
        } else if ( is_array($name) ) {
            Zend_Cache::throwException('Incorrect callback format');
        }

        if (!is_string($name)) {
            Zend_Cache::throwException('Incorrect function name');
        }
        if (!is_array($parameters)) {
            Zend_Cache::throwException('parameters argument must be an array');
        }
        return md5($name . serialize($parameters));
    }

While this technically produced working functionality it had the draw back that the cached result was the same for every instance. I believe the cached result should not only be based on parameters passed to the function but also the variables in the scope of the function (which change generally per instance of an object). To achieve the feature I came up with another modification:

<pre class="highlight">
//Zend/Cache/Frontend/Function.php
    private function _makeId($name, $parameters)
    {
        /** callback handling fix **/
        $obj = null;
        if ( is_array($name) && is_object($name[0]) && is_string($name[1]) ) {
            $obj = $name[0];
            $name = get_class($name[0])."::".$name[1];
        } else if ( is_array($name) ) {
            Zend_Cache::throwException('Incorrect callback format');
        }

        if (!is_string($name)) {
            Zend_Cache::throwException('Incorrect function name');
        }
        if (!is_array($parameters)) {
            Zend_Cache::throwException('parameters argument must be an array');
        }
        if ($obj)
            return md5($name . serialize($obj) . serialize($parameters));
        else
            return md5($name . serialize($parameters));
    }

Thus concludes the new feature. Please forgive me if this does not belong here as it seemed to me the most practical place.

Comments

Posted by Jeremy Giberson (jeremygiberson) on 2007-01-05T17:47:43.000+0000

Fixed code tags

Posted by Fabien MARTY (fab) on 2007-01-27T05:22:07.000+0000

for the point 1, the Class frontend is not ok for you ?

Posted by Fabien MARTY (fab) on 2007-02-08T15:32:20.000+0000

no feedback

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