* API: caching uses by default APC(u) if available AND apc.shm_size * apc.shm_segments >= 64M, it no longer complains in cron/cli if APC is not enabled (as it should and is by default)

This commit is contained in:
Ralf Becker 2015-04-23 09:20:17 +00:00
parent c9ef30367e
commit c57bd20379

View File

@ -1,24 +1,36 @@
<?php <?php
/** /**
* EGroupware API: Caching provider storing data in PHP's APC * EGroupware API: Caching provider storing data in PHP's APC or APCu extension
* *
* @link http://www.egroupware.org * @link http://www.egroupware.org
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package api * @package api
* @subpackage cache * @subpackage cache
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de> * @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @copyright (c) 2010-12 by Ralf Becker <RalfBecker-AT-outdoor-training.de> * @copyright (c) 2010-15 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @version $Id$ * @version $Id$
*/ */
/** /**
* Caching provider storing data in PHP's APC * Caching provider storing data in PHP's APC or APCu extension / shared memory.
* *
* The provider concats all $keys with '::' to get a single string. * The provider concats all $keys with '::' to get a single string.
* *
* To use this provider set in your header.inc.php: * This provider is used by default, if it is available or explicit enabled in your header.inc.php:
* $GLOBALS['egw_info']['server']['cache_provider_instance'] = array('egw_cache_apc'); * $GLOBALS['egw_info']['server']['cache_provider_instance'] = array('egw_cache_apc');
* and optional also $GLOBALS['egw_info']['server']['cache_provider_tree'] (defaults to instance) * and optional also $GLOBALS['egw_info']['server']['cache_provider_tree'] (defaults to instance)
*
* APC(u) and CLI:
* --------------
* APC(u) is not enabled by default for CLI (apc.enable_cli), nor would it access same shared memory!
* It makes no sense to fall back to files cache, as this is probably quite outdated,
* if PHP via Webserver uses APC. Better to use no cache at all.
* egw_cache::get*() will return NULL for not found and egw_cache::[un]set*()
* false for not being able to (un)set anything.
* It also does not make sense to report failure by throwing an Exception and filling
* up cron logs.
* --> if APC(u) is available for Webserver, we report availability for CLI too,
* but use no cache at all!
*/ */
class egw_cache_apc extends egw_cache_provider_check implements egw_cache_provider class egw_cache_apc extends egw_cache_provider_check implements egw_cache_provider
{ {
@ -34,10 +46,6 @@ class egw_cache_apc extends egw_cache_provider_check implements egw_cache_provid
{ {
throw new Exception (__METHOD__.'('.array2string($params).") No function apc_fetch()!"); throw new Exception (__METHOD__.'('.array2string($params).") No function apc_fetch()!");
} }
if (PHP_SAPI == 'cli' && !ini_get('apc.enable_cli'))
{
throw new Exception (__METHOD__.'('.array2string($params).") APC NOT enabled for cli, check apc.enable_cli!");
}
} }
/** /**
@ -50,9 +58,7 @@ class egw_cache_apc extends egw_cache_provider_check implements egw_cache_provid
*/ */
public static function available() public static function available()
{ {
$available = (bool)ini_get('apc.enabled'); if (($available = (bool)ini_get('apc.enabled') && function_exists('apc_fetch')))
if ($available && function_exists('apc_fetch') && (PHP_SAPI != 'cli' || ini_get('apc.enable_cli')))
{ {
$size = ini_get('apc.shm_size'); $size = ini_get('apc.shm_size');
// ancent APC (3.1.3) in Debian 6/Squezze has size in MB without a unit // ancent APC (3.1.3) in Debian 6/Squezze has size in MB without a unit
@ -81,7 +87,7 @@ class egw_cache_apc extends egw_cache_provider_check implements egw_cache_provid
* *
* @param array $keys eg. array($level,$app,$location) * @param array $keys eg. array($level,$app,$location)
* @param mixed $data * @param mixed $data
* @param int $expiration=0 * @param int $expiration =0
* @return boolean true on success, false on error * @return boolean true on success, false on error
*/ */
function set(array $keys,$data,$expiration=0) function set(array $keys,$data,$expiration=0)
@ -97,6 +103,7 @@ class egw_cache_apc extends egw_cache_provider_check implements egw_cache_provid
*/ */
function get(array $keys) function get(array $keys)
{ {
$success = null;
$data = apc_fetch($key=self::key($keys),$success); $data = apc_fetch($key=self::key($keys),$success);
if (!$success) if (!$success)