Path: blob/master/src/infrastructure/cache/PhutilAPCKeyValueCache.php
12241 views
<?php12/**3* Interface to the APC key-value cache. This is a very high-performance cache4* which is local to the current machine.5*/6final class PhutilAPCKeyValueCache extends PhutilKeyValueCache {789/* -( Key-Value Cache Implementation )------------------------------------- */101112public function isAvailable() {13return (function_exists('apc_fetch') || function_exists('apcu_fetch')) &&14ini_get('apc.enabled') &&15(ini_get('apc.enable_cli') || php_sapi_name() != 'cli');16}1718public function getKeys(array $keys, $ttl = null) {19static $is_apcu;20if ($is_apcu === null) {21$is_apcu = self::isAPCu();22}2324$results = array();25$fetched = false;26foreach ($keys as $key) {27if ($is_apcu) {28$result = apcu_fetch($key, $fetched);29} else {30$result = apc_fetch($key, $fetched);31}3233if ($fetched) {34$results[$key] = $result;35}36}37return $results;38}3940public function setKeys(array $keys, $ttl = null) {41static $is_apcu;42if ($is_apcu === null) {43$is_apcu = self::isAPCu();44}4546// NOTE: Although modern APC supports passing an array to `apc_store()`,47// it is not supported by older version of APC or by HPHP.4849// See T13525 for discussion of use of "@" to silence this warning:50// > GC cache entry "<some-key-name>" was on gc-list for <X> seconds5152foreach ($keys as $key => $value) {53if ($is_apcu) {54@apcu_store($key, $value, $ttl);55} else {56@apc_store($key, $value, $ttl);57}58}5960return $this;61}6263public function deleteKeys(array $keys) {64static $is_apcu;65if ($is_apcu === null) {66$is_apcu = self::isAPCu();67}6869foreach ($keys as $key) {70if ($is_apcu) {71apcu_delete($key);72} else {73apc_delete($key);74}75}7677return $this;78}7980public function destroyCache() {81static $is_apcu;82if ($is_apcu === null) {83$is_apcu = self::isAPCu();84}8586if ($is_apcu) {87apcu_clear_cache();88} else {89apc_clear_cache('user');90}9192return $this;93}9495private static function isAPCu() {96return function_exists('apcu_fetch');97}9899}100101102