Source of file XCache.php
Size: 15,111 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Cache/Storage/Adapter/XCache.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530 | <?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 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 XCache extends AbstractAdapter implements AvailableSpaceCapableInterface, ClearByNamespaceInterface, ClearByPrefixInterface, FlushableInterface, IterableInterface, TotalSpaceCapableInterface { /** * Backup HTTP authentication properties of $_SERVER array * * @var array */ protected $backupAuth = array(); /** * Total space in bytes * * @var int|float */ protected $totalSpace; /** * Constructor * * @param null|array|Traversable|ApcOptions $options * @throws Exception\ExceptionInterface */ public function __construct($options = null) { if (!extension_loaded('xcache')) { throw new Exception\ExtensionNotLoadedException('Missing ext/xcache'); } if (PHP_SAPI == 'cli') { throw new Exception\ExtensionNotLoadedException( "ext/xcache isn't available on SAPI 'cli'" ); } if (ini_get('xcache.var_size') <= 0) { throw new Exception\ExtensionNotLoadedException( "ext/xcache is disabled - see 'xcache.var_size'" ); } parent::__construct($options); } /* options */ /** * Set options. * * @param array|Traversable|ApcOptions $options * @return XCache * @see getOptions() */ public function setOptions($options) { if (!$options instanceof XCacheOptions) { $options = new XCacheOptions($options); } return parent::setOptions($options); } /** * Get options. * * @return XCacheOptions * @see setOptions() */ public function getOptions() { if (!$this->options) { $this->setOptions(new XCacheOptions()); } return $this->options; } /* TotalSpaceCapableInterface */ /** * Get total space in bytes * * @return int|float */ public function getTotalSpace() { if ($this->totalSpace === null) { $this->totalSpace = 0; $this->initAdminAuth(); $cnt = xcache_count(XC_TYPE_VAR); for ($i=0; $i < $cnt; $i++) { $info = xcache_info(XC_TYPE_VAR, $i); $this->totalSpace+= $info['size']; } $this->resetAdminAuth(); } return $this->totalSpace; } /* AvailableSpaceCapableInterface */ /** * Get available space in bytes * * @return int|float */ public function getAvailableSpace() { $availableSpace = 0; $this->initAdminAuth(); $cnt = xcache_count(XC_TYPE_VAR); for ($i = 0; $i < $cnt; $i++) { $info = xcache_info(XC_TYPE_VAR, $i); $availableSpace+= $info['avail']; } $this->resetAdminAuth(); return $availableSpace; } /* 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(); xcache_unset_by_prefix($prefix); return true; } /* 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(); $prefix = ($namespace === '') ? '' : $namespace . $options->getNamespaceSeparator() . $prefix; xcache_unset_by_prefix($prefix); return true; } /* FlushableInterface */ /** * Flush the whole storage * * @return bool */ public function flush() { $this->initAdminAuth(); $cnt = xcache_count(XC_TYPE_VAR); for ($i = 0; $i < $cnt; $i++) { xcache_clear_cache(XC_TYPE_VAR, $i); } $this->resetAdminAuth(); return true; } /* IterableInterface */ /** * Get the storage iterator * * @return KeyListIterator */ public function getIterator() { $options = $this->getOptions(); $namespace = $options->getNamespace(); $keys = array(); $this->initAdminAuth(); if ($namespace === '') { $cnt = xcache_count(XC_TYPE_VAR); for ($i=0; $i < $cnt; $i++) { $list = xcache_list(XC_TYPE_VAR, $i); foreach ($list['cache_list'] as & $item) { $keys[] = $item['name']; } } } else { $prefix = $namespace . $options->getNamespaceSeparator(); $prefixL = strlen($prefix); $cnt = xcache_count(XC_TYPE_VAR); for ($i=0; $i < $cnt; $i++) { $list = xcache_list(XC_TYPE_VAR, $i); foreach ($list['cache_list'] as & $item) { $keys[] = substr($item['name'], $prefixL); } } } $this->resetAdminAuth(); return new KeyListIterator($this, $keys); } /* 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 = xcache_get($internalKey); $success = ($result !== null); if ($success) { $casToken = $result; } 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 xcache_isset($prefix . $normalizedKey); } /** * 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; if (xcache_isset($internalKey)) { $this->initAdminAuth(); $cnt = xcache_count(XC_TYPE_VAR); for ($i=0; $i < $cnt; $i++) { $list = xcache_list(XC_TYPE_VAR, $i); foreach ($list['cache_list'] as & $metadata) { if ($metadata['name'] === $internalKey) { $this->normalizeMetadata($metadata); return $metadata; } } } $this->resetAdminAuth(); } return false; } /* 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 = ($options === '') ? '' : $namespace . $options->getNamespaceSeparator(); $internalKey = $prefix . $normalizedKey; $ttl = $options->getTtl(); if (!xcache_set($internalKey, $value, $ttl)) { $type = is_object($value) ? get_class($value) : gettype($value); throw new Exception\RuntimeException( "xcache_set('{$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(); $internalKey = $prefix . $normalizedKey; return xcache_unset($internalKey); } /** * 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; $ttl = $options->getTtl(); $value = (int) $value; return xcache_inc($internalKey, $value, $ttl); } /** * 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; $ttl = $options->getTtl(); $value = (int) $value; return xcache_dec($internalKey, $value, $ttl); } /* 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' => false, 'boolean' => true, 'integer' => true, 'double' => true, 'string' => true, 'array' => true, 'object' => 'object', 'resource' => false, ), 'supportedMetadata' => array( 'internal_key', 'size', 'refcount', 'hits', 'ctime', 'atime', 'hvalue', ), 'minTtl' => 1, 'maxTtl' => (int)ini_get('xcache.var_maxttl'), 'staticTtl' => true, 'ttlPrecision' => 1, 'useRequestTime' => true, '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 */ /** * Init authentication before calling admin functions * * @return void */ protected function initAdminAuth() { $options = $this->getOptions(); if ($options->getAdminAuth()) { $adminUser = $options->getAdminUser(); $adminPass = $options->getAdminPass(); // backup HTTP authentication properties if (isset($_SERVER['PHP_AUTH_USER'])) { $this->backupAuth['PHP_AUTH_USER'] = $_SERVER['PHP_AUTH_USER']; } if (isset($_SERVER['PHP_AUTH_PW'])) { $this->backupAuth['PHP_AUTH_PW'] = $_SERVER['PHP_AUTH_PW']; } // set authentication $_SERVER['PHP_AUTH_USER'] = $adminUser; $_SERVER['PHP_AUTH_PW'] = $adminPass; } } /** * Reset authentication after calling admin functions * * @return void */ protected function resetAdminAuth() { unset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']); $_SERVER = $this->backupAuth + $_SERVER; $this->backupAuth = array(); } /** * Normalize metadata to work with XCache * * @param array $metadata */ protected function normalizeMetadata(array & $metadata) { $metadata['internal_key'] = &$metadata['name']; unset($metadata['name']); } } |