From f6aa1ca1aa6737924a86fa74f6b332375fa0f048 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Tue, 23 Oct 2012 07:49:42 +0000 Subject: [PATCH] * API: only cache in APC by default, if it has at least 64M of shared memory, otherwise use filesystem --- phpgwapi/inc/class.egw_cache.inc.php | 13 +++++----- phpgwapi/inc/class.egw_cache_apc.inc.php | 33 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/phpgwapi/inc/class.egw_cache.inc.php b/phpgwapi/inc/class.egw_cache.inc.php index a938d0fab8..271b7942f4 100644 --- a/phpgwapi/inc/class.egw_cache.inc.php +++ b/phpgwapi/inc/class.egw_cache.inc.php @@ -518,13 +518,6 @@ class egw_cache } } -// setting apc as default provide, if apc_fetch function exists AND not cli or apc enabled for cli -if (is_null(egw_cache::$default_provider)) -{ - egw_cache::$default_provider = function_exists('apc_fetch') && (PHP_SAPI != 'cli' || ini_get('apc.enable_cli')) ? - 'egw_cache_apc' : 'egw_cache_files'; -} - /** * Interface for a caching provider for tree and instance level * @@ -623,6 +616,12 @@ abstract class egw_cache_provider_check implements egw_cache_provider } } +// setting apc as default provider, if apc_fetch function exists AND further checks in egw_cache_apc recommed it +if (is_null(egw_cache::$default_provider)) +{ + egw_cache::$default_provider = function_exists('apc_fetch') && egw_cache_apc::available() ? 'egw_cache_apc' : 'egw_cache_files'; +} + // some testcode, if this file is called via it's URL // can be run on command-line: sudo php -d apc.enable_cli=1 -f phpgwapi/inc/class.egw_cache.inc.php /*if (isset($_SERVER['SCRIPT_FILENAME']) && realpath($_SERVER['SCRIPT_FILENAME']) == __FILE__) diff --git a/phpgwapi/inc/class.egw_cache_apc.inc.php b/phpgwapi/inc/class.egw_cache_apc.inc.php index 649735a623..6cc83c0645 100644 --- a/phpgwapi/inc/class.egw_cache_apc.inc.php +++ b/phpgwapi/inc/class.egw_cache_apc.inc.php @@ -40,6 +40,39 @@ class egw_cache_apc extends egw_cache_provider_check implements egw_cache_provid } } + /** + * Check if APC is available for caching user data + * + * Default shared memory size of 32M is just enough for the byte code cache, + * but not for caching user data, we only use APC by default if we have at least 64M. + * + * @return boolean true: apc available, false: not + */ + public static function available() + { + $available = false; + if (function_exists('apc_fetch') && (PHP_SAPI != 'cli' || ini_get('apc.enable_cli'))) + { + $size = ini_get('apc.shm_size'); + + switch(strtoupper(substr($size, -1))) + { + case 'G': + $size *= 1024; + case 'M': + $size *= 1024; + case 'K': + $size *= 1024; + } + $size *= ini_get('apc.shm_segments'); + + // only cache in APC, if we have at least 64M available (default is 32M) + $available = $size >= 64*1024*1024; + } + //error_log(__METHOD__."() size=$size returning ".array2string($available)); + return $available; + } + /** * Stores some data in the cache *