Source of file Apc.php
Size: 23,181 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Cache/Storage/Adapter/Apc.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Cache\Storage\Adapter; use APCIterator as BaseApcIterator; use stdClass; use Traversable; use Zend\Cache\Exception; use Zend\Cache\Storage\AvailableSpaceCapableInterface; use Zend\Cache\Storage\Capabilities; use Zend\Cache\Storage\ClearByNamespaceInterface; use Zend\Cache\Storage\ClearByPrefixInterface; use Zend\Cache\Storage\FlushableInterface; use Zend\Cache\Storage\IterableInterface; use Zend\Cache\Storage\TotalSpaceCapableInterface; class Apc extends AbstractAdapter implements AvailableSpaceCapableInterface, ClearByNamespaceInterface, ClearByPrefixInterface, FlushableInterface, IterableInterface, TotalSpaceCapableInterface { /** * Buffered total space in bytes * * @var null|int|float */ protected $totalSpace; /** * Constructor * * @param null|array|Traversable|ApcOptions $options * @throws Exception\ExceptionInterface */ public function __construct($options = null) { if (version_compare('3.1.6', phpversion('apc')) > 0) { throw new Exception\ExtensionNotLoadedException("Missing ext/apc >= 3.1.6"); } $enabled = ini_get('apc.enabled'); if (PHP_SAPI == 'cli') { $enabled = $enabled && (bool) ini_get('apc.enable_cli'); } if (!$enabled) { throw new Exception\ExtensionNotLoadedException( "ext/apc is disabled - see 'apc.enabled' and 'apc.enable_cli'" ); } parent::__construct($options); } /* options */ /** * Set options. * * @param array|Traversable|ApcOptions $options * @return Apc * @see getOptions() */ public function setOptions($options) { if (!$options instanceof ApcOptions) { $options = new ApcOptions($options); } return parent::setOptions($options); } /** * Get options. * * @return ApcOptions * @see setOptions() */ public function getOptions() { if (!$this->options) { $this->setOptions(new ApcOptions()); } return $this->options; } /* TotalSpaceCapableInterface */ /** * Get total space in bytes * * @return int|float */ public function getTotalSpace() { if ($this->totalSpace === null) { $smaInfo = apc_sma_info(true); $this->totalSpace = $smaInfo['num_seg'] * $smaInfo['seg_size']; } return $this->totalSpace; } /* AvailableSpaceCapableInterface */ /** * Get available space in bytes * * @return int|float */ public function getAvailableSpace() { $smaInfo = apc_sma_info(true); return $smaInfo['avail_mem']; } /* IterableInterface */ /** * Get the storage iterator * * @return ApcIterator */ public function getIterator() { $options = $this->getOptions(); $namespace = $options->getNamespace(); $prefix = ''; $pattern = null; if ($namespace !== '') { $prefix = $namespace . $options->getNamespaceSeparator(); $pattern = '/^' . preg_quote($prefix, '/') . '/'; } $baseIt = new BaseApcIterator('user', $pattern, 0, 1, APC_LIST_ACTIVE); return new ApcIterator($this, $baseIt, $prefix); } /* FlushableInterface */ /** * Flush the whole storage * * @return bool */ public function flush() { return apc_clear_cache('user'); } /* ClearByNamespaceInterface */ /** * Remove items by given namespace * * @param string $namespace * @return bool */ public function clearByNamespace($namespace) { $namespace = (string) $namespace; if ($namespace === '') { throw new Exception\InvalidArgumentException('No namespace given'); } $options = $this->getOptions(); $prefix = $namespace . $options->getNamespaceSeparator(); $pattern = '/^' . preg_quote($prefix, '/') . '/'; return apc_delete(new BaseApcIterator('user', $pattern, 0, 1, APC_LIST_ACTIVE)); } /* ClearByPrefixInterface */ /** * Remove items matching given prefix * * @param string $prefix * @return bool */ public function clearByPrefix($prefix) { $prefix = (string) $prefix; if ($prefix === '') { throw new Exception\InvalidArgumentException('No prefix given'); } $options = $this->getOptions(); $namespace = $options->getNamespace(); $nsPrefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $pattern = '/^' . preg_quote($nsPrefix . $prefix, '/') . '/'; return apc_delete(new BaseApcIterator('user', $pattern, 0, 1, APC_LIST_ACTIVE)); } /* reading */ /** * Internal method to get an item. * * @param string $normalizedKey * @param bool $success * @param mixed $casToken * @return mixed Data on success, null on failure * @throws Exception\ExceptionInterface */ protected function internalGetItem(& $normalizedKey, & $success = null, & $casToken = null) { $options = $this->getOptions(); $namespace = $options->getNamespace(); $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; $result = apc_fetch($internalKey, $success); if (!$success) { return null; } $casToken = $result; return $result; } /** * Internal method to get multiple items. * * @param array $normalizedKeys * @return array Associative array of keys and values * @throws Exception\ExceptionInterface */ protected function internalGetItems(array & $normalizedKeys) { $options = $this->getOptions(); $namespace = $options->getNamespace(); if ($namespace === '') { return apc_fetch($normalizedKeys); } $prefix = $namespace . $options->getNamespaceSeparator(); $internalKeys = array(); foreach ($normalizedKeys as $normalizedKey) { $internalKeys[] = $prefix . $normalizedKey; } $fetch = apc_fetch($internalKeys); // remove namespace prefix $prefixL = strlen($prefix); $result = array(); foreach ($fetch as $internalKey => & $value) { $result[substr($internalKey, $prefixL)] = $value; } return $result; } /** * Internal method to test if an item exists. * * @param string $normalizedKey * @return bool * @throws Exception\ExceptionInterface */ protected function internalHasItem(& $normalizedKey) { $options = $this->getOptions(); $namespace = $options->getNamespace(); $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); return apc_exists($prefix . $normalizedKey); } /** * Internal method to test multiple items. * * @param array $normalizedKeys * @return array Array of found keys * @throws Exception\ExceptionInterface */ protected function internalHasItems(array & $normalizedKeys) { $options = $this->getOptions(); $namespace = $options->getNamespace(); if ($namespace === '') { // array_filter with no callback will remove entries equal to FALSE return array_keys(array_filter(apc_exists($normalizedKeys))); } $prefix = $namespace . $options->getNamespaceSeparator(); $internalKeys = array(); foreach ($normalizedKeys as $normalizedKey) { $internalKeys[] = $prefix . $normalizedKey; } $exists = apc_exists($internalKeys); $result = array(); $prefixL = strlen($prefix); foreach ($exists as $internalKey => $bool) { if ($bool === true) { $result[] = substr($internalKey, $prefixL); } } return $result; } /** * Get metadata of an item. * * @param string $normalizedKey * @return array|bool Metadata on success, false on failure * @throws Exception\ExceptionInterface */ protected function internalGetMetadata(& $normalizedKey) { $options = $this->getOptions(); $namespace = $options->getNamespace(); $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; // @see http://pecl.php.net/bugs/bug.php?id=22564 if (!apc_exists($internalKey)) { $metadata = false; } else { $format = APC_ITER_ALL ^ APC_ITER_VALUE ^ APC_ITER_TYPE ^ APC_ITER_REFCOUNT; $regexp = '/^' . preg_quote($internalKey, '/') . '$/'; $it = new BaseApcIterator('user', $regexp, $format, 100, APC_LIST_ACTIVE); $metadata = $it->current(); } if (!$metadata) { return false; } $this->normalizeMetadata($metadata); return $metadata; } /** * Get metadata of multiple items * * @param array $normalizedKeys * @return array Associative array of keys and metadata * * @triggers getMetadatas.pre(PreEvent) * @triggers getMetadatas.post(PostEvent) * @triggers getMetadatas.exception(ExceptionEvent) */ protected function internalGetMetadatas(array & $normalizedKeys) { $keysRegExp = array(); foreach ($normalizedKeys as $normalizedKey) { $keysRegExp[] = preg_quote($normalizedKey, '/'); } $options = $this->getOptions(); $namespace = $options->getNamespace(); if ($namespace === '') { $pattern = '/^(' . implode('|', $keysRegExp) . ')' . '$/'; } else { $prefix = $namespace . $options->getNamespaceSeparator(); $pattern = '/^' . preg_quote($prefix, '/') . '(' . implode('|', $keysRegExp) . ')' . '$/'; } $format = APC_ITER_ALL ^ APC_ITER_VALUE ^ APC_ITER_TYPE ^ APC_ITER_REFCOUNT; $it = new BaseApcIterator('user', $pattern, $format, 100, APC_LIST_ACTIVE); $result = array(); $prefixL = strlen($prefix); foreach ($it as $internalKey => $metadata) { // @see http://pecl.php.net/bugs/bug.php?id=22564 if (!apc_exists($internalKey)) { continue; } $this->normalizeMetadata($metadata); $result[substr($internalKey, $prefixL)] = & $metadata; } return $result; } /* writing */ /** * Internal method to store an item. * * @param string $normalizedKey * @param mixed $value * @return bool * @throws Exception\ExceptionInterface */ protected function internalSetItem(& $normalizedKey, & $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; $ttl = $options->getTtl(); if (!apc_store($internalKey, $value, $ttl)) { $type = is_object($value) ? get_class($value) : gettype($value); throw new Exception\RuntimeException( "apc_store('{$internalKey}', <{$type}>, {$ttl}) failed" ); } return true; } /** * Internal method to store multiple items. * * @param array $normalizedKeyValuePairs * @return array Array of not stored keys * @throws Exception\ExceptionInterface */ protected function internalSetItems(array & $normalizedKeyValuePairs) { $options = $this->getOptions(); $namespace = $options->getNamespace(); if ($namespace === '') { return array_keys(apc_store($normalizedKeyValuePairs, null, $options->getTtl())); } $prefix = $namespace . $options->getNamespaceSeparator(); $internalKeyValuePairs = array(); foreach ($normalizedKeyValuePairs as $normalizedKey => &$value) { $internalKey = $prefix . $normalizedKey; $internalKeyValuePairs[$internalKey] = &$value; } $failedKeys = apc_store($internalKeyValuePairs, null, $options->getTtl()); $failedKeys = array_keys($failedKeys); // remove prefix $prefixL = strlen($prefix); foreach ($failedKeys as & $key) { $key = substr($key, $prefixL); } return $failedKeys; } /** * Add an item. * * @param string $normalizedKey * @param mixed $value * @return bool * @throws Exception\ExceptionInterface */ protected function internalAddItem(& $normalizedKey, & $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; $ttl = $options->getTtl(); if (!apc_add($internalKey, $value, $ttl)) { if (apc_exists($internalKey)) { return false; } $type = is_object($value) ? get_class($value) : gettype($value); throw new Exception\RuntimeException( "apc_add('{$internalKey}', <{$type}>, {$ttl}) failed" ); } return true; } /** * Internal method to add multiple items. * * @param array $normalizedKeyValuePairs * @return array Array of not stored keys * @throws Exception\ExceptionInterface */ protected function internalAddItems(array & $normalizedKeyValuePairs) { $options = $this->getOptions(); $namespace = $options->getNamespace(); if ($namespace === '') { return array_keys(apc_add($normalizedKeyValuePairs, null, $options->getTtl())); } $prefix = $namespace . $options->getNamespaceSeparator(); $internalKeyValuePairs = array(); foreach ($normalizedKeyValuePairs as $normalizedKey => $value) { $internalKey = $prefix . $normalizedKey; $internalKeyValuePairs[$internalKey] = $value; } $failedKeys = apc_add($internalKeyValuePairs, null, $options->getTtl()); $failedKeys = array_keys($failedKeys); // remove prefix $prefixL = strlen($prefix); foreach ($failedKeys as & $key) { $key = substr($key, $prefixL); } return $failedKeys; } /** * Internal method to replace an existing item. * * @param string $normalizedKey * @param mixed $value * @return bool * @throws Exception\ExceptionInterface */ protected function internalReplaceItem(& $normalizedKey, & $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; if (!apc_exists($internalKey)) { return false; } $ttl = $options->getTtl(); if (!apc_store($internalKey, $value, $ttl)) { $type = is_object($value) ? get_class($value) : gettype($value); throw new Exception\RuntimeException( "apc_store('{$internalKey}', <{$type}>, {$ttl}) failed" ); } return true; } /** * Internal method to remove an item. * * @param string $normalizedKey * @return bool * @throws Exception\ExceptionInterface */ protected function internalRemoveItem(& $normalizedKey) { $options = $this->getOptions(); $namespace = $options->getNamespace(); $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); return apc_delete($prefix . $normalizedKey); } /** * Internal method to remove multiple items. * * @param array $normalizedKeys * @return array Array of not removed keys * @throws Exception\ExceptionInterface */ protected function internalRemoveItems(array & $normalizedKeys) { $options = $this->getOptions(); $namespace = $options->getNamespace(); if ($namespace === '') { return apc_delete($normalizedKeys); } $prefix = $namespace . $options->getNamespaceSeparator(); $internalKeys = array(); foreach ($normalizedKeys as $normalizedKey) { $internalKeys[] = $prefix . $normalizedKey; } $failedKeys = apc_delete($internalKeys); // remove prefix $prefixL = strlen($prefix); foreach ($failedKeys as & $key) { $key = substr($key, $prefixL); } return $failedKeys; } /** * Internal method to increment an item. * * @param string $normalizedKey * @param int $value * @return int|bool The new value on success, false on failure * @throws Exception\ExceptionInterface */ protected function internalIncrementItem(& $normalizedKey, & $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; $value = (int) $value; $newValue = apc_inc($internalKey, $value); // initial value if ($newValue === false) { $ttl = $options->getTtl(); $newValue = $value; if (!apc_add($internalKey, $newValue, $ttl)) { throw new Exception\RuntimeException( "apc_add('{$internalKey}', {$newValue}, {$ttl}) failed" ); } } return $newValue; } /** * Internal method to decrement an item. * * @param string $normalizedKey * @param int $value * @return int|bool The new value on success, false on failure * @throws Exception\ExceptionInterface */ protected function internalDecrementItem(& $normalizedKey, & $value) { $options = $this->getOptions(); $namespace = $options->getNamespace(); $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; $value = (int) $value; $newValue = apc_dec($internalKey, $value); // initial value if ($newValue === false) { $ttl = $options->getTtl(); $newValue = -$value; if (!apc_add($internalKey, $newValue, $ttl)) { throw new Exception\RuntimeException( "apc_add('{$internalKey}', {$newValue}, {$ttl}) failed" ); } } return $newValue; } /* status */ /** * Internal method to get capabilities of this adapter * * @return Capabilities */ protected function internalGetCapabilities() { if ($this->capabilities === null) { $marker = new stdClass(); $capabilities = new Capabilities( $this, $marker, array( 'supportedDatatypes' => array( 'NULL' => true, 'boolean' => true, 'integer' => true, 'double' => true, 'string' => true, 'array' => true, 'object' => 'object', 'resource' => false, ), 'supportedMetadata' => array( 'internal_key', 'atime', 'ctime', 'mtime', 'rtime', 'size', 'hits', 'ttl', ), 'minTtl' => 1, 'maxTtl' => 0, 'staticTtl' => true, 'ttlPrecision' => 1, 'useRequestTime' => (bool) ini_get('apc.use_request_time'), 'expiredRead' => false, 'maxKeyLength' => 5182, 'namespaceIsPrefix' => true, 'namespaceSeparator' => $this->getOptions()->getNamespaceSeparator(), ) ); // update namespace separator on change option $this->getEventManager()->attach('option', function ($event) use ($capabilities, $marker) { $params = $event->getParams(); if (isset($params['namespace_separator'])) { $capabilities->setNamespaceSeparator($marker, $params['namespace_separator']); } }); $this->capabilities = $capabilities; $this->capabilityMarker = $marker; } return $this->capabilities; } /* internal */ /** * Normalize metadata to work with APC * * @param array $metadata * @return void */ protected function normalizeMetadata(array & $metadata) { $metadata['internal_key'] = $metadata['key']; $metadata['ctime'] = $metadata['creation_time']; $metadata['atime'] = $metadata['access_time']; $metadata['rtime'] = $metadata['deletion_time']; $metadata['size'] = $metadata['mem_size']; $metadata['hits'] = $metadata['num_hits']; unset( $metadata['key'], $metadata['creation_time'], $metadata['access_time'], $metadata['deletion_time'], $metadata['mem_size'], $metadata['num_hits'] ); } /** * Internal method to set an item only if token matches * * @param mixed $token * @param string $normalizedKey * @param mixed $value * @return bool * @see getItem() * @see setItem() */ protected function internalCheckAndSetItem(& $token, & $normalizedKey, & $value) { return apc_cas($normalizedKey, $token, $value); } } |