Source for file Memcache.php

Documentation is available at Memcache.php

  1. <?php
  2. /*
  3.  *  $Id: Memcache.php 1080 2007-02-10 18:17:08Z romanb $
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the LGPL. For more information, see
  19.  * <http://www.phpdoctrine.com>.
  20.  */
  21. Doctrine::autoload('Doctrine_Cache_Driver');
  22. /**
  23.  * Doctrine_Cache_Memcache
  24.  *
  25.  * @package     Doctrine
  26.  * @subpackage  Doctrine_Cache
  27.  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
  28.  * @category    Object Relational Mapping
  29.  * @link        www.phpdoctrine.com
  30.  * @since       1.0
  31.  * @version     $Revision: 1080 $
  32.  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
  33.  */
  34. {
  35.     /**
  36.      * @var Memcache $_memcache     memcache object
  37.      */
  38.     protected $_memcache = null;
  39.     /**
  40.      * constructor
  41.      * 
  42.      * @param array $options        associative array of cache driver options
  43.      */
  44.     public function __construct($options array())
  45.     {      
  46.         if extension_loaded('memcache')) {
  47.             throw new Doctrine_Cache_Exception('In order to use Memcache driver, the memcache extension must be loaded.');
  48.         }
  49.         parent::__construct($options);
  50.  
  51.         if (isset($options['servers'])) {
  52.             $value$options['servers'];
  53.             if (isset($value['host'])) {
  54.                 // in this case, $value seems to be a simple associative array (one server only)
  55.                 $value array(=> $value)// let's transform it into a classical array of associative arrays
  56.             }
  57.             $this->setOption('servers'$value);
  58.         }
  59.         
  60.         $this->_memcache = new Memcache;
  61.  
  62.         foreach ($this->_options['servers'as $server{
  63.             if array_key_exists('persistent'$server)) {
  64.                 $server['persistent'true;
  65.             }
  66.             if array_key_exists('port'$server)) {
  67.                 $server['port'11211;
  68.             }
  69.             $this->_memcache->addServer($server['host']$server['port']$server['persistent']);
  70.         }
  71.     }
  72.     /**
  73.      * Test if a cache is available for the given id and (if yes) return it (false else)
  74.      *
  75.      * Note : return value is always "string" (unserialization is done by the core not by the backend)
  76.      * 
  77.      * @param string $id cache id
  78.      * @param boolean $testCacheValidity        if set to false, the cache validity won't be tested
  79.      * @return string cached datas (or false)
  80.      */
  81.     public function fetch($id$testCacheValidity true
  82.     {
  83.         $tmp $this->_memcache->get($id);
  84.  
  85.         if (is_array($tmp)) {
  86.             return $tmp[0];
  87.         }
  88.  
  89.         return false;
  90.     }
  91.     /**
  92.      * Test if a cache is available or not (for the given id)
  93.      *
  94.      * @param string $id cache id
  95.      * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
  96.      */
  97.     public function contains($id
  98.     {
  99.         return (bool) $this->_memcache->get($id);
  100.     }
  101.     /**
  102.      * Save some string datas into a cache record
  103.      *
  104.      * Note : $data is always saved as a string
  105.      *
  106.      * @param string $data      data to cache
  107.      * @param string $id        cache id
  108.      * @param int $lifeTime     if != false, set a specific lifetime for this cache record (null => infinite lifeTime)
  109.      * @return boolean true if no problem
  110.      */
  111.     public function save($id$data$lifeTime false)
  112.     {
  113.         if ($this->_options['compression']{
  114.             $flag MEMCACHE_COMPRESSED;
  115.         else {
  116.             $flag 0;
  117.         }
  118.  
  119.         $result $this->_memcache->set($id$data$flag$lifeTime);
  120.     }
  121.     /**
  122.      * Remove a cache record
  123.      * 
  124.      * @param string $id cache id
  125.      * @return boolean true if no problem
  126.      */
  127.     public function delete($id
  128.     {
  129.         return $this->_memcache->delete($id);
  130.     }
  131. }