egroupware/phpgwapi/inc/class.egw_cache_memcache.inc.php
Ralf Becker 189d89be1b tests for egw_cache_provider and not using APC for cli, if apc.enable_cli is not set
Also did some benchmarking on my Macbook (with a SSD and memcached running on localhost):
Checking egw_cache_memcache:
0 checks failed, 100 iterations took 0.480 sec

Checking egw_cache_apc:
0 checks failed, 100 iterations took 0.025 sec

Checking egw_cache_files:
0 checks failed, 100 iterations took 0.826 sec
--> APC is by a factor of 20 faster then memcached, which is double as fast compared to files on a SSD
2012-04-13 10:21:58 +00:00

120 lines
3.3 KiB
PHP

<?php
/**
* EGroupware API: Caching provider storing data in memcached via PHP's memcache extension
*
* @link http://www.egroupware.org
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package api
* @subpackage cache
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @copyright (c) 2009-12 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @version $Id$
*/
/**
* Caching provider storing data in memcached via PHP's memcache extension
*
* The provider concats all $keys with '::' to get a single string.
*
* To use this provider set in your header.inc.php:
* $GLOBALS['egw_info']['server']['cache_provider_instance'] = array('egw_cache_memcache','localhost'[,'otherhost:port']);
* and optional also $GLOBALS['egw_info']['server']['cache_provider_tree'] (defaults to instance)
*
* You can set more then one server and specify a port, if it's not the default one 11211.
*/
class egw_cache_memcache extends egw_cache_provider_check implements egw_cache_provider
{
/**
* Instance of Memcache
*
* @var Memcache
*/
private $memcache;
/**
* Flags used to store content
*
*/
const STORE_FLAGS = MEMCACHE_COMPRESSED;
/**
* Constructor, eg. opens the connection to the backend
*
* @throws Exception if connection to backend could not be established
* @param array $params eg. array('localhost'[,'localhost:11211',...])
*/
function __construct(array $params)
{
check_load_extension('memcache',true);
$this->memcache = new Memcache();
if (!$params) $params = array('localhost'); // some reasonable default
$ok = false;
foreach($params as $host_port)
{
list($host,$port) = explode(':',$host_port);
if (!$port) $port = 11211; // default port
$ok = $this->memcache->addServer($host,$port) || $ok;
//error_log(__METHOD__."(".array2string($params).") memcache->addServer('$host',$port) = ".(int)$ok);
}
if (!$ok)
{
throw new Exception (__METHOD__.'('.array2string($params).") Can't open connection to any memcached server!");
}
}
/**
* Stores some data in the cache
*
* @param array $keys eg. array($level,$app,$location)
* @param mixed $data
* @param int $expiration=0
* @return boolean true on success, false on error
*/
function set(array $keys,$data,$expiration=0)
{
return $this->memcache->set(self::key($keys),serialize($data),self::STORE_FLAGS,$expiration);
}
/**
* Get some data from the cache
*
* @param array $keys eg. array($level,$app,$location)
* @return mixed data stored or NULL if not found in cache
*/
function get(array $keys)
{
if (($data = $this->memcache->get($key=self::key($keys))) === false)
{
//error_log(__METHOD__."(".array2string($keys).") key='$key' NOT found!");
return null;
}
//error_log(__METHOD__."(".array2string($keys).") key='$key' found ".bytes($data)." bytes).");
return unserialize($data);
}
/**
* Delete some data from the cache
*
* @param array $keys eg. array($level,$app,$location)
* @return boolean true on success, false on error (eg. $key not set)
*/
function delete(array $keys)
{
return $this->memcache->delete(self::key($keys));
}
/**
* Create a single key from $keys
*
* @param array $keys
* @return string
*/
private function key(array $keys)
{
return implode('::',$keys);
}
}