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')); }
/** * Set namespace. * * The option Memcached::OPT_PREFIX_KEY will be used as the namespace. * It can't be longer than 128 characters. * * @see AdapterOptions::setNamespace() * @see MemcachedOptions::setPrefixKey() */ public function setNamespace($namespace) { $namespace = (string) $namespace; if (128 < strlen($namespace)) { throw new Exception\InvalidArgumentException(sprintf('%s expects a prefix key of no longer than 128 characters', __METHOD__)); } return parent::setNamespace($namespace); }
public function testSetGetHasAndRemoveItemsWithNamespace() { $items = array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'); $this->_options->setNamespace('defaultns1'); $this->assertSame(array(), $this->_storage->setItems($items)); $this->_options->setNamespace('defaultns2'); $this->assertSame(array(), $this->_storage->hasItems(array_keys($items))); $this->_options->setNamespace('defaultns1'); $rs = $this->_storage->getItems(array_keys($items)); $this->assertInternalType('array', $rs); foreach ($items as $key => $value) { $this->assertArrayHasKey($key, $rs); $this->assertEquals($value, $rs[$key]); } $rs = $this->_storage->hasItems(array_keys($items)); $this->assertInternalType('array', $rs); $this->assertEquals(count($items), count($rs)); foreach ($items as $key => $value) { $this->assertContains($key, $rs); } // remove the first and the last item $this->assertSame(array('missing'), $this->_storage->removeItems(array('missing', 'key1', 'key3'))); unset($items['key1'], $items['key3']); $rs = $this->_storage->getItems(array_keys($items)); $this->assertInternalType('array', $rs); foreach ($items as $key => $value) { $this->assertArrayHasKey($key, $rs); $this->assertEquals($value, $rs[$key]); } $rs = $this->_storage->hasItems(array_keys($items)); $this->assertInternalType('array', $rs); $this->assertEquals(count($items), count($rs)); foreach ($items as $key => $value) { $this->assertContains($key, $rs); } }