/** * Swap object data to disk * Actualy swaps data or only unloads it from memory, * if object is not changed since last swap * * @param \Zend\Memory\Container\Movable $container * @param integer $id */ private function _swap(Container\Movable $container, $id) { if ($container->isLocked()) { return; } if (!$container->isSwapped()) { $this->_cache->setItem($this->_managerId . $id, $container->getRef(), array('tags' => $this->_tags)); } $this->_memorySize -= $this->_sizes[$id]; $container->markAsSwapped(); $container->unloadValue(); }
/** * 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; }
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']); }
/** * Returns the items for a given page. * * @return Traversable */ public function getItemsByPage($pageNumber) { $pageNumber = $this->normalizePageNumber($pageNumber); if ($this->_cacheEnabled()) { $data = self::$_cache->getItem($this->_getCacheId($pageNumber)); if ($data !== false) { return $data; } } $offset = ($pageNumber - 1) * $this->getItemCountPerPage(); $items = $this->_adapter->getItems($offset, $this->getItemCountPerPage()); $filter = $this->getFilter(); if ($filter !== null) { $items = $filter->filter($items); } if (!$items instanceof Traversable) { $items = new ArrayIterator($items); } if ($this->_cacheEnabled()) { self::$_cache->setItem($this->_getCacheId($pageNumber), $items, array('tags' => array($this->_getCacheInternalId()))); } return $items; }
/** * Armazena um item no cache, identificado por uma chave * * @access public * @param string $key Chave de identificação do item do cache * @param mixed $value Valor armazenado * @return boolean O item foi armazenado no cache com sucesso? */ public function setItem(string $key, $value) : bool { return $this->adapter->setItem($key, $value); }
public function testHasItemWithNonReadable() { $this->assertTrue($this->_storage->setItem('key', 'value')); $this->_options->setReadable(false); $this->assertFalse($this->_storage->hasItem('key')); }
/** * 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; }
/** * 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; }