memcache.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: memcache.php 8114 2009-03-17 21:10:28Z renan.saddam $ */
00003 /**
00004  * Memcache storage engine for cache
00005  *
00006  *
00007  * PHP versions 4 and 5
00008  *
00009  * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
00010  * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00011  *
00012  * Licensed under The MIT License
00013  * Redistributions of files must retain the above copyright notice.
00014  *
00015  * @filesource
00016  * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00017  * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00018  * @package       cake
00019  * @subpackage    cake.cake.libs.cache
00020  * @since         CakePHP(tm) v 1.2.0.4933
00021  * @version       $Revision: 8114 $
00022  * @modifiedby    $LastChangedBy: renan.saddam $
00023  * @lastmodified  $Date: 2009-03-17 17:10:28 -0400 (Tue, 17 Mar 2009) $
00024  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
00025  */
00026 /**
00027  * Memcache storage engine for cache
00028  *
00029  * @package       cake
00030  * @subpackage    cake.cake.libs.cache
00031  */
00032 class MemcacheEngine extends CacheEngine {
00033 /**
00034  * Memcache wrapper.
00035  *
00036  * @var Memcache
00037  * @access private
00038  */
00039     var $__Memcache = null;
00040 /**
00041  * settings
00042  *      servers = string or array of memcache servers, default => 127.0.0.1
00043  *      compress = boolean, default => false
00044  *
00045  * @var array
00046  * @access public
00047  */
00048     var $settings = array();
00049 /**
00050  * Initialize the Cache Engine
00051  *
00052  * Called automatically by the cache frontend
00053  * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
00054  *
00055  * @param array $setting array of setting for the engine
00056  * @return boolean True if the engine has been successfully initialized, false if not
00057  * @access public
00058  */
00059     function init($settings = array()) {
00060         if (!class_exists('Memcache')) {
00061             return false;
00062         }
00063         parent::init(array_merge(array(
00064             'engine'=> 'Memcache', 'prefix' => Inflector::slug(APP_DIR) . '_', 'servers' => array('127.0.0.1'), 'compress'=> false
00065             ), $settings)
00066         );
00067 
00068         if ($this->settings['compress']) {
00069             $this->settings['compress'] = MEMCACHE_COMPRESSED;
00070         }
00071         if (!is_array($this->settings['servers'])) {
00072             $this->settings['servers'] = array($this->settings['servers']);
00073         }
00074         if (!isset($this->__Memcache)) {
00075             $return = false;
00076             $this->__Memcache =& new Memcache();
00077             foreach ($this->settings['servers'] as $server) {
00078                 $parts = explode(':', $server);
00079                 $host = $parts[0];
00080                 $port = 11211;
00081                 if (isset($parts[1])) {
00082                     $port = $parts[1];
00083                 }
00084                 if ($this->__Memcache->addServer($host, $port)) {
00085                     $return = true;
00086                 }
00087             }
00088             return $return;
00089         }
00090         return true;
00091     }
00092 /**
00093  * Write data for key into cache
00094  *
00095  * @param string $key Identifier for the data
00096  * @param mixed $value Data to be cached
00097  * @param integer $duration How long to cache the data, in seconds
00098  * @return boolean True if the data was succesfully cached, false on failure
00099  * @access public
00100  */
00101     function write($key, &$value, $duration) {
00102         $expires = time() + $duration;
00103         $this->__Memcache->set($key.'_expires', $expires, $this->settings['compress'], $expires);
00104         return $this->__Memcache->set($key, $value, $this->settings['compress'], $expires);
00105     }
00106 /**
00107  * Read a key from the cache
00108  *
00109  * @param string $key Identifier for the data
00110  * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
00111  * @access public
00112  */
00113     function read($key) {
00114         $time = time();
00115         $cachetime = intval($this->__Memcache->get($key.'_expires'));
00116         if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
00117             return false;
00118         }
00119         return $this->__Memcache->get($key);
00120     }
00121 /**
00122  * Delete a key from the cache
00123  *
00124  * @param string $key Identifier for the data
00125  * @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
00126  * @access public
00127  */
00128     function delete($key) {
00129         return $this->__Memcache->delete($key);
00130     }
00131 /**
00132  * Delete all keys from the cache
00133  *
00134  * @return boolean True if the cache was succesfully cleared, false otherwise
00135  * @access public
00136  */
00137     function clear() {
00138         return $this->__Memcache->flush();
00139     }
00140 /**
00141  * Connects to a server in connection pool
00142  *
00143  * @param string $host host ip address or name
00144  * @param integer $port Server port
00145  * @return boolean True if memcache server was connected
00146  * @access public
00147  */
00148     function connect($host, $port = 11211) {
00149         if ($this->__Memcache->getServerStatus($host, $port) === 0) {
00150             if ($this->__Memcache->connect($host, $port)) {
00151                 return true;
00152             }
00153             return false;
00154         }
00155         return true;
00156     }
00157 }
00158 ?>

Generated on Sun Nov 22 00:30:52 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7