diff --git a/phpgwapi/inc/class.egw_cache.inc.php b/phpgwapi/inc/class.egw_cache.inc.php index 8daf4ca4a4..6bed07a9a9 100644 --- a/phpgwapi/inc/class.egw_cache.inc.php +++ b/phpgwapi/inc/class.egw_cache.inc.php @@ -7,7 +7,7 @@ * @package api * @subpackage cache * @author Ralf Becker - * @copyright (c) 2009 by Ralf Becker + * @copyright (c) 2009/10 by Ralf Becker * @version $Id$ */ @@ -66,9 +66,12 @@ class egw_cache * $GLOBALS['egw_info']['server']['cache_provider_instance'] and optional * $GLOBALS['egw_info']['server']['cache_provider_tree'] (defaults to instance) * + * Default is set (if not set here) after class definition to egw_cache_apc or egw_cache_files, + * depending on function 'apc_fetch' exists or not + * * @var array */ - static $default_provider = array('egw_cache_files');// array('egw_cache_memcache','localhost'); + static $default_provider; // = array('egw_cache_files');// array('egw_cache_memcache','localhost'); /** * Set some data in the cache @@ -510,6 +513,12 @@ class egw_cache } } +// setting apc as default provide, if apc_fetch function exists +if (is_null(egw_cache::$default_provider)) +{ + egw_cache::$default_provider = function_exists('apc_fetch') ? 'egw_cache_apc' : 'egw_cache_files'; +} + /** * Interface for a caching provider for tree and instance level * diff --git a/phpgwapi/inc/class.egw_cache_apc.inc.php b/phpgwapi/inc/class.egw_cache_apc.inc.php new file mode 100644 index 0000000000..85297fedab --- /dev/null +++ b/phpgwapi/inc/class.egw_cache_apc.inc.php @@ -0,0 +1,92 @@ + + * @copyright (c) 2010 by Ralf Becker + * @version $Id$ + */ + +/** + * Caching provider storing data in PHP's APC + * + * 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_apc'); + * and optional also $GLOBALS['egw_info']['server']['cache_provider_tree'] (defaults to instance) + */ +class egw_cache_apc implements egw_cache_provider +{ + /** + * 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) + { + if (!function_exists('apc_fetch')) // apc >= 3.0 + { + throw new Exception (__METHOD__.'('.array2string($params).") No function apc_fetch()!"); + } + } + + /** + * 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 apc_store(self::key($keys),$data,$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) + { + $data = apc_fetch($key=self::key($keys),$success); + + if (!$success) + { + //error_log(__METHOD__."(".array2string($keys).") key='$key' NOT found!"); + return null; + } + //error_log(__METHOD__."(".array2string($keys).") key='$key' found ".bytes(serialize($data))." bytes)."); + return $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 apc_delete(self::key($keys)); + } + + /** + * Create a single key from $keys + * + * @param array $keys + * @return string + */ + private function key(array $keys) + { + return implode('::',$keys); + } +}