示例#1
0
 /**
  * Internal method to check if the given cache supports tags
  *
  * @return false|string
  */
 protected static function _getTagSupportForCache()
 {
     if (!self::$_cache instanceof CacheAdapter) {
         self::$_cacheTags = false;
         return false;
     }
     $capabilities = self::$_cache->getCapabilities();
     if (!$capabilities->getTagging()) {
         self::$_cacheTags = false;
         return false;
     }
     self::$_cacheTags = true;
     return true;
 }
示例#2
0
 /**
  * Load value from swap file.
  *
  * @internal
  * @param \Zend\Memory\Container\Movable $container
  * @param integer $id
  */
 public function load(Container\Movable $container, $id)
 {
     $value = $this->_cache->getItem($this->_managerId . $id, array('ttl' => 0));
     // Try to swap other objects if necessary
     // (do not include specified object into check)
     $this->_memorySize += strlen($value);
     $this->_swapCheck();
     // Add loaded obect to the end of loaded objects list
     $container->setValue($value);
     if ($this->_sizes[$id] > $this->_minSize) {
         // Add object to the end of "unload candidates list"
         $this->_unloadCandidates[$id] = $container;
     }
 }
示例#3
0
 /**
  * Change a capability
  *
  * @param  stdClass $marker
  * @param  string $name
  * @param  mixed $value
  * @return Capabilities Fluent interface
  * @throws Exception\InvalidArgumentException
  */
 protected function setCapability(stdClass $marker, $name, $value)
 {
     if ($this->marker !== $marker) {
         throw new Exception\InvalidArgumentException('Invalid marker');
     }
     $property = '_' . $name;
     if ($this->{$property} !== $value) {
         $this->{$property} = $value;
         // trigger event
         if ($this->adapter instanceof EventsCapableInterface) {
             $this->adapter->events()->trigger('capability', $this->adapter, new ArrayObject(array($name => $value)));
         }
     }
     return $this;
 }
示例#4
0
 /**
  * Returns the page item cache.
  *
  * @return array
  */
 public function getPageItemCache()
 {
     $data = array();
     if ($this->_cacheEnabled()) {
         $cacheIds = self::$_cache->find(CacheAdapter::MATCH_TAGS_OR, array('tags' => array($this->_getCacheInternalId())));
         $cacheIds = array();
         while (($item = self::$_cache->fetch()) !== false) {
             $cacheIds[] = $item['key'];
         }
         foreach ($cacheIds as $id) {
             if (preg_match('|' . self::CACHE_TAG_PREFIX . "(\\d+)_.*|", $id, $page)) {
                 $data[$page[1]] = self::$_cache->getItem($this->_getCacheId($page[1]));
             }
         }
     }
     return $data;
 }
示例#5
0
 /**
  * Saves the given cache
  * Prevents broken cache when write_control is disabled and displays problems by log or error
  *
  * @param  mixed  $data
  * @param  string $id
  * @return boolean Returns false when the cache has not been written
  */
 protected function saveCache($data, $id)
 {
     if (self::$_cacheTags) {
         self::$_cache->setItem($id, $data, array('tags' => array($this->_options['tag'])));
     } else {
         self::$_cache->setItem($id, $data);
     }
     if (!self::$_cache->hasItem($id)) {
         if (!$this->_options['disableNotices']) {
             if ($this->_options['log']) {
                 $this->_options['log']->log("Writing to cache failed.", $this->_options['logPriority']);
             } else {
                 trigger_error("Writing to cache failed.", E_USER_NOTICE);
             }
         }
         self::$_cache->removeItem($id);
         return false;
     }
     return true;
 }
示例#6
0
 public function testTagsAreUsedWhenCaching()
 {
     $capabilities = $this->_storage->getCapabilities();
     if (!$capabilities->getTagging()) {
         $this->markTestSkipped("Tags are not supported by this adapter");
     }
     // Ensure we don't have expired items in the cache for this test
     $this->_options->setTtl(60);
     $this->_storage->setItem('someitem', 'somevalue', array('tags' => array('foo')));
     $this->assertTrue($this->_storage->find(Cache\Storage\Adapter::MATCH_TAGS_OR, array('tags' => array('foo'))));
     $actualItems = array();
     while (($item = $this->_storage->fetch()) !== false) {
         // check $item
         $this->assertArrayHasKey('key', $item);
         $this->assertArrayHasKey('value', $item);
         $actualItems[$item['key']] = $item['value'];
     }
     $this->assertEquals(1, count($actualItems));
     $this->assertArrayHasKey('someitem', $actualItems);
     $this->assertEquals('somevalue', $actualItems['someitem']);
 }
示例#7
0
 public function tearDown()
 {
     $this->_cache->clear(CacheAdapter::MATCH_ALL);
     $this->_cache = null;
 }
示例#8
0
 /**
  * Instantiate a storage adapter
  *
  * @param  string|Storage\Adapter $adapterName
  * @param  array|Traversable|Storage\Adapter\AdapterOptions $options
  * @return Storage\Adapter
  * @throws Exception\RuntimeException
  */
 public static function adapterFactory($adapterName, $options = array())
 {
     if ($adapterName instanceof Storage\Adapter) {
         // $adapterName is already an adapter object
         $adapterName->setOptions($options);
         return $adapterName;
     }
     $adapter = static::getAdapterBroker()->load($adapterName);
     $adapter->setOptions($options);
     return $adapter;
 }
示例#9
0
 /**
  * Renova a validade do item do cache, identificado por uma chave.
  *
  * @access public
  * @param string $key Chave de identificação do item do cache
  * @return boolean O item deve sua validade renovada com sucesso?
  */
 public function renovarItem(string $key) : bool
 {
     return $this->adapter->touchItem($key);
 }
示例#10
0
 public function testHasItemWithNonReadable()
 {
     $this->assertTrue($this->_storage->setItem('key', 'value'));
     $this->_options->setReadable(false);
     $this->assertFalse($this->_storage->hasItem('key'));
 }
示例#11
0
 public function tearDown()
 {
     if ($this->cache) {
         $this->cache->clear(CacheAdapter::MATCH_ALL);
     }
 }
示例#12
0
 /**
  * Initializes metadata.
  *
  * If metadata cannot be loaded from cache, adapter's describeTable() method is called to discover metadata
  * information. Returns true if and only if the metadata are loaded from cache.
  *
  * @return boolean
  * @throws \Zend\Db\Table\Exception
  */
 protected function _setupMetadata()
 {
     if ($this->metadataCacheInClass() && count($this->_metadata) > 0) {
         return true;
     }
     // Assume that metadata will be loaded from cache
     $isMetadataFromCache = true;
     // If $this has no metadata cache but the class has a default metadata cache
     if (null === $this->_metadataCache && null !== self::$_defaultMetadataCache) {
         // Make $this use the default metadata cache of the class
         $this->_setMetadataCache(self::$_defaultMetadataCache);
     }
     // If $this has a metadata cache
     if (null !== $this->_metadataCache) {
         $cacheId = $this->_cacheId();
     }
     // If $this has no metadata cache or metadata cache misses
     if (null === $this->_metadataCache || !($metadata = $this->_metadataCache->getItem($cacheId))) {
         // Metadata are not loaded from cache
         $isMetadataFromCache = false;
         // Fetch metadata from the adapter's describeTable() method
         $metadata = $this->_db->describeTable($this->_name, $this->_schema);
         // If $this has a metadata cache, then cache the metadata
         if (null !== $this->_metadataCache && !$this->_metadataCache->setItem($cacheId, $metadata)) {
             trigger_error('Failed saving metadata to metadataCache', E_USER_NOTICE);
         }
     }
     // Assign the metadata to $this
     $this->_metadata = $metadata;
     // Return whether the metadata were loaded from cache
     return $isMetadataFromCache;
 }
示例#13
0
 /**
  * Initializes metadata.
  *
  * If metadata cannot be loaded from cache, adapter's describeTable() method is called to discover metadata
  * information. Returns true if and only if the metadata are loaded from cache.
  *
  * @return boolean
  * @throws \Zend\Db\Table\Exception
  */
 protected function _setupMetadata()
 {
     if ($this->metadataCacheInClass() && count($this->_metadata) > 0) {
         return true;
     }
     // Assume that metadata will be loaded from cache
     $isMetadataFromCache = true;
     // If $this has no metadata cache but the class has a default metadata cache
     if (null === $this->_metadataCache && null !== self::$_defaultMetadataCache) {
         // Make $this use the default metadata cache of the class
         $this->_setMetadataCache(self::$_defaultMetadataCache);
     }
     // If $this has a metadata cache
     if (null !== $this->_metadataCache) {
         // Define the cache identifier where the metadata are saved
         //get db configuration
         $dbConfig = $this->_db->getConfig();
         // Define the cache identifier where the metadata are saved
         $cacheId = md5((isset($dbConfig['options']['port']) ? ':' . $dbConfig['options']['port'] : null) . (isset($dbConfig['options']['host']) ? ':' . $dbConfig['options']['host'] : null) . '/' . $dbConfig['dbname'] . ':' . $this->_schema . '.' . $this->_name);
     }
     // If $this has no metadata cache or metadata cache misses
     if (null === $this->_metadataCache || !($metadata = $this->_metadataCache->getItem($cacheId))) {
         // Metadata are not loaded from cache
         $isMetadataFromCache = false;
         // Fetch metadata from the adapter's describeTable() method
         $metadata = $this->_db->describeTable($this->_name, $this->_schema);
         // If $this has a metadata cache, then cache the metadata
         if (null !== $this->_metadataCache && !$this->_metadataCache->setItem($cacheId, $metadata)) {
             trigger_error('Failed saving metadata to metadataCache', E_USER_NOTICE);
         }
     }
     // Assign the metadata to $this
     $this->_metadata = $metadata;
     // Return whether the metadata were loaded from cache
     return $isMetadataFromCache;
 }