diff --git a/src/cache/PhutilAPCKeyValueCache.php b/src/cache/PhutilAPCKeyValueCache.php --- a/src/cache/PhutilAPCKeyValueCache.php +++ b/src/cache/PhutilAPCKeyValueCache.php @@ -11,16 +11,26 @@ public function isAvailable() { - return function_exists('apc_fetch') && + return (function_exists('apc_fetch') || function_exists('apcu_fetch')) && ini_get('apc.enabled') && (ini_get('apc.enable_cli') || php_sapi_name() != 'cli'); } public function getKeys(array $keys, $ttl = null) { + static $is_apcu; + if ($is_apcu === null) { + $is_apcu = self::isAPCu(); + } + $results = array(); $fetched = false; foreach ($keys as $key) { - $result = apc_fetch($key, $fetched); + if ($is_apcu) { + $result = apcu_fetch($key, $fetched); + } else { + $result = apc_fetch($key, $fetched); + } + if ($fetched) { $results[$key] = $result; } @@ -29,28 +39,59 @@ } public function setKeys(array $keys, $ttl = null) { + static $is_apcu; + if ($is_apcu === null) { + $is_apcu = self::isAPCu(); + } + // NOTE: Although modern APC supports passing an array to `apc_store()`, // it is not supported by older version of APC or by HPHP. foreach ($keys as $key => $value) { - apc_store($key, $value, $ttl); + if ($is_apcu) { + apcu_store($key, $value, $ttl); + } else { + apc_store($key, $value, $ttl); + } } return $this; } public function deleteKeys(array $keys) { + static $is_apcu; + if ($is_apcu === null) { + $is_apcu = self::isAPCu(); + } + foreach ($keys as $key) { - apc_delete($key); + if ($is_apcu) { + apcu_delete($key); + } else { + apc_delete($key); + } } return $this; } public function destroyCache() { - apc_clear_cache('user'); + static $is_apcu; + if ($is_apcu === null) { + $is_apcu = self::isAPCu(); + } + + if ($is_apcu) { + apcu_clear_cache(); + } else { + apc_clear_cache('user'); + } return $this; } + private static function isAPCU() { + return function_exists('apcu_fetch'); + } + }