public function testClearByNamespace() { if (!$this->_storage instanceof ClearByNamespaceInterface) { $this->markTestSkipped("Storage doesn't implement ClearByNamespaceInterface"); } // write 2 items of 2 different namespaces $this->_options->setNamespace('ns1'); $this->assertTrue($this->_storage->setItem('key1', 'value1')); $this->_options->setNamespace('ns2'); $this->assertTrue($this->_storage->setItem('key2', 'value2')); // clear unknown namespace should return true but clear nothing $this->assertTrue($this->_storage->clearByNamespace('unknown')); $this->_options->setNamespace('ns1'); $this->assertTrue($this->_storage->hasItem('key1')); $this->_options->setNamespace('ns2'); $this->assertTrue($this->_storage->hasItem('key2')); // clear "ns1" $this->assertTrue($this->_storage->clearByNamespace('ns1')); $this->_options->setNamespace('ns1'); $this->assertFalse($this->_storage->hasItem('key1')); $this->_options->setNamespace('ns2'); $this->assertTrue($this->_storage->hasItem('key2')); // clear "ns2" $this->assertTrue($this->_storage->clearByNamespace('ns2')); $this->_options->setNamespace('ns1'); $this->assertFalse($this->_storage->hasItem('key1')); $this->_options->setNamespace('ns2'); $this->assertFalse($this->_storage->hasItem('key2')); }
public function testClearByNamespaceThrowsInvalidArgumentExceptionOnEmptyNamespace() { if (!$this->_storage instanceof ClearByNamespaceInterface) { $this->markTestSkipped("Storage doesn't implement ClearByNamespaceInterface"); } $this->setExpectedException('Zend\\Cache\\Exception\\InvalidArgumentException'); $this->_storage->clearByNamespace(''); }
/** * Object destructor * * Clean up cache storage */ public function __destruct() { if ($this->cache !== null) { if ($this->cache instanceof ClearByNamespaceCacheStorage) { $this->cache->clearByNamespace($this->cache->getOptions()->getNamespace()); } elseif ($this->cache instanceof FlushableCacheStorage) { $this->cache->flush(); } } }
/** * Deletes all items in the pool. * * @return bool * True if the pool was successfully cleared. False if there was an error. */ public function clear() { $cleared = false; try { /* @todo Not at all sure about this... do we really want to flush()? What happens if the storage supports /* namespaces, but none has been provided? */ $options = $this->storage->getOptions(); $namespace = $options->getNamespace(); if ($this->storage instanceof ClearByNamespaceInterface && $namespace) { $cleared = $this->storage->clearByNamespace($namespace); } elseif ($this->storage instanceof FlushableInterface) { $cleared = $this->storage->flush(); } else { throw new CacheException(sprintf("Storage %s does not support clear()", get_class($this->storage))); } } catch (Exception\InvalidArgumentException $e) { throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); } catch (Exception\ExceptionInterface $e) { throw new CacheException($e->getMessage(), $e->getCode(), $e); } return $cleared; }