2007-04-30 06:01:54 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
|
|
|
* Memcache storage engine for cache
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
|
|
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.cache
|
|
|
|
* @since CakePHP(tm) v 1.2.0.4933
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Memcache storage engine for cache
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.cache
|
|
|
|
*/
|
|
|
|
class MemcacheEngine extends CacheEngine {
|
2007-05-20 05:35:13 +00:00
|
|
|
/**
|
|
|
|
* Memcache wrapper.
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
* @access private
|
|
|
|
*/
|
2007-04-30 06:01:54 +00:00
|
|
|
var $__Memcache = null;
|
2007-05-20 05:35:13 +00:00
|
|
|
/**
|
2007-09-30 07:45:34 +00:00
|
|
|
* settings
|
|
|
|
* servers = string or array of memcache servers, default => 127.0.0.1
|
|
|
|
* compress = boolean, default => false
|
2007-05-20 05:35:13 +00:00
|
|
|
*
|
2007-09-30 07:45:34 +00:00
|
|
|
* @var array
|
|
|
|
* @access public
|
2007-05-20 05:35:13 +00:00
|
|
|
*/
|
2007-09-30 07:45:34 +00:00
|
|
|
var $settings = array();
|
2007-04-30 06:01:54 +00:00
|
|
|
/**
|
2007-09-30 07:45:34 +00:00
|
|
|
* Initialize the Cache Engine
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
|
|
|
* Called automatically by the cache frontend
|
2007-09-30 07:45:34 +00:00
|
|
|
* To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
2007-09-30 07:45:34 +00:00
|
|
|
* @param array $setting array of setting for the engine
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if the engine has been successfully initialized, false if not
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
2007-09-30 07:45:34 +00:00
|
|
|
function init($settings = array()) {
|
2007-06-20 06:15:35 +00:00
|
|
|
if (!class_exists('Memcache')) {
|
2007-04-30 07:52:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-09-30 07:45:34 +00:00
|
|
|
parent::init($settings);
|
|
|
|
$defaults = array('servers' => array('127.0.0.1'), 'compress'=> false);
|
|
|
|
$this->settings = am($this->settings, $defaults, $settings);
|
2007-04-30 06:01:54 +00:00
|
|
|
|
2007-09-30 07:45:34 +00:00
|
|
|
if ($this->settings['compress']) {
|
|
|
|
$this->settings['compress'] = MEMCACHE_COMPRESSED;
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
2007-09-30 07:45:34 +00:00
|
|
|
if (!is_array($this->settings['servers'])) {
|
|
|
|
$this->settings['servers'] = array($this->settings['servers']);
|
|
|
|
}
|
|
|
|
|
2007-04-30 06:01:54 +00:00
|
|
|
$this->__Memcache =& new Memcache();
|
|
|
|
|
2007-09-30 07:45:34 +00:00
|
|
|
foreach ($this->settings['servers'] as $server) {
|
2007-04-30 06:01:54 +00:00
|
|
|
$parts = explode(':', $server);
|
|
|
|
$host = $parts[0];
|
2007-09-30 07:45:34 +00:00
|
|
|
$port = 11211;
|
|
|
|
if (isset($parts[1])) {
|
|
|
|
$port = $parts[1];
|
|
|
|
}
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($this->__Memcache->addServer($host, $port)) {
|
2007-09-30 07:45:34 +00:00
|
|
|
return true;
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-30 07:45:34 +00:00
|
|
|
return false;
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
/**
|
2007-09-30 07:45:34 +00:00
|
|
|
* Write data for key into cache
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @param mixed $value Data to be cached
|
2007-10-22 16:11:12 +00:00
|
|
|
* @param integer $duration How long to cache the data, in seconds
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if the data was succesfully cached, false on failure
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
2007-09-30 07:45:34 +00:00
|
|
|
function write($key, &$value, $duration) {
|
|
|
|
return $this->__Memcache->set($key, $value, $this->settings['compress'], $duration);
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
/**
|
2007-09-30 07:45:34 +00:00
|
|
|
* Read a key from the cache
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
function read($key) {
|
|
|
|
return $this->__Memcache->get($key);
|
|
|
|
}
|
|
|
|
/**
|
2007-09-30 07:45:34 +00:00
|
|
|
* Delete a key from the cache
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
function delete($key) {
|
|
|
|
return $this->__Memcache->delete($key);
|
|
|
|
}
|
|
|
|
/**
|
2007-09-30 07:45:34 +00:00
|
|
|
* Delete all keys from the cache
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if the cache was succesfully cleared, false otherwise
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
function clear() {
|
|
|
|
return $this->__Memcache->flush();
|
|
|
|
}
|
2007-04-30 07:38:47 +00:00
|
|
|
/**
|
2007-10-22 06:23:57 +00:00
|
|
|
* Connects to a server in connection pool
|
2007-04-30 07:38:47 +00:00
|
|
|
*
|
2007-09-30 07:45:34 +00:00
|
|
|
* @param string $host host ip address or name
|
2007-10-22 16:11:12 +00:00
|
|
|
* @param integer $port Server port
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if memcache server was connected
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 07:38:47 +00:00
|
|
|
*/
|
2007-09-30 07:45:34 +00:00
|
|
|
function connect($host, $port = 11211) {
|
|
|
|
if ($this->__Memcache->getServerStatus($host, $port) === 0) {
|
|
|
|
if ($this->__Memcache->connect($host, $port)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2007-04-30 07:38:47 +00:00
|
|
|
}
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
?>
|