Issues

ZF2-134: CLONE - Add multi get support ( exemple, for memcached )

Description

I think ZF should add multi get support for memcached ( included at the version 1.4 of memcache package http://pecl.php.net/package-changelog.php/… , http://it.php.net/manual/en/memcache.get.php )

Core.php in Cache


     /**
     * Test if a cache is available for the given id and (if yes) return it (false else)
     *
     * @param  string  $id                     Cache id
     * @param  boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
     * @param  boolean $doNotUnserialize       Do not serialize (even if automatic_serialization is true) => for internal use
     * @return mixed|false Cached datas
     */
    public function load($id, $doNotTestCacheValidity = false, $doNotUnserialize = false)
    {
        if (!$this->_options['caching']) {
            return false;
        }
        $id = $this->_id($id); // cache id may need prefix
        $this->_lastId = $id;
        self::_validateIdOrTag($id);
        $data = $this->_backend->load($id, $doNotTestCacheValidity);
        if ($data===false) {
            // no cache available
            return false;
        }
        if ((!$doNotUnserialize) && $this->_options['automatic_serialization']) {
            // we need to unserialize before sending the result
            return unserialize($data);
        }
        return $data;
    }

TO:


     /**
     * Test if a cache is available for the given id and (if yes) return it (false else)
     *
     * @param  string|array $id                Cache id or Array of Cache id
     * @param  boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
     * @param  boolean $doNotUnserialize       Do not serialize (even if automatic_serialization is true) => for internal use
     * @return mixed|array|false Cached datas
     */
    public function load($id, $doNotTestCacheValidity = false, $doNotUnserialize = false)
    {
        if (!$this->_options['caching']) {
            return false;
        }
        if(!is_array($id)){
            $id = $this->_id($id); // cache id may need prefix
            $this->_lastId = $id;
            self::_validateIdOrTag($id);
            $data = $this->_backend->load($id, $doNotTestCacheValidity);
            if ($data===false) {
                // no cache available
                return false;
            }
            if ((!$doNotUnserialize) && $this->_options['automatic_serialization']) {
                // we need to unserialize before sending the result
                return unserialize($data);
            }
        }else{
            $ids = $id
            $this->_lastId = end($ids);
            foreach($ids as &$id){
                $id = $this->_id($id); // cache id may need prefix
                self::_validateIdOrTag($id);
            }
            $data = $this->_backend->load($ids, $doNotTestCacheValidity);
            if ($data===false) {
                // no cache available
                return false;
            }
            if ((!$doNotUnserialize) && $this->_options['automatic_serialization']) {
                foreach($data as &$row){                
                    // we need to unserialize before sending the result
                    $row = unserialize($row);
                }
            }
        }
        return $data;
    }

Memcache.php



     /**
     * Test if a cache is available for the given id and (if yes) return it (false else)
     *
     * @param  string  $id                     Cache id
     * @param  boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
     * @return string|false cached datas
     */
    public function load($id, $doNotTestCacheValidity = false)
    {
        $tmp = $this->_memcache->get($id);
        if (is_array($tmp) && isset($tmp[0])) {
            return $tmp[0];
        }
        return false;
    }


only wiki to change TO:



     /**
     * Test if a cache is available for the given id and (if yes) return it (false else)
     * Now it supports also array of id
     * @param  string|array  $id    Cache id or an array of them
     * @param  boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
     * @return string|array|false cached datas
     */
    public function load($id, $doNotTestCacheValidity = false)
    {
        $tmp = $this->_memcache->get($id);
        if (is_array($tmp) && isset($tmp[0])) {return $tmp[0];
            
        }
        return false;
    }


for other cache engines that doesn't suppor by default multiget I think should be simply so:

Apc.php



    /**
     * Test if a cache is available for the given id and (if yes) return it (false else)
     *
     * WARNING $doNotTestCacheValidity=true is unsupported by the Apc backend
     *
     * @param  string  $id                     cache id
     * @param  boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
     * @return string cached datas (or false)
     */
    public function load($id, $doNotTestCacheValidity = false)
    {
        $tmp = apc_fetch($id);
        if (is_array($tmp)) {
            return $tmp[0];
        }
        return false;
    }

TO:



    public _last_notFound = array();
    /**
     * Test if a cache is available for the given id and (if yes) return it (false else)
     * Now it supports also array of id
     * WARNING $doNotTestCacheValidity=true is unsupported by the Apc backend
     *
     * @param  string|array  $id    Cache id or an array of them
     * @param  boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
     * @return string|array|false cached datas
     */
    public function load($id, $doNotTestCacheValidity = false)
    {
        
        if(!is_array($id){
            $tmp = apc_fetch($id);
            if (is_array($tmp)) {
                return $tmp[0];
            }
        }else{
            $this->_last_not_Found = array()
            $ids = $id;
            $results = array()
            foreach($ids as $id){
                $tmp = apc_fetch($id);
                if (is_array($tmp)) {
                    array_push($results,$tmp[0]);
                }else{
                    array_push($this->_last_not_Found,$id);
                }
            }
            if(!empty($results)){
                return $results;
            }
        }
        return false;
    }

I hope I haven't made mistakes in writing and I hope there will be this improvement in next release, thanks

Comments

fixed

Nearly all methods have a version to read/write multiple items @ once.