public function testConstructionOptions() { $key = array('apple', 'sauce'); $options = array(); $options['servers'][] = array('127.0.0.1', '11211', '50'); $options['servers'][] = array('127.0.0.1', '11211'); $options['extension'] = $this->extension; $driver = new Memcache(); $driver->setOptions($options); $item = new Item(); $poolStub = new PoolGetDriverStub(); $poolStub->setDriver($driver); $item->setPool($poolStub); $item->setKey($key); $this->assertTrue($item->set($key), 'Able to load and store memcache driver using multiple servers'); $options = array(); $options['extension'] = $this->extension; $driver = new Memcache(); $driver->setOptions($options); $item = new Item(); $poolStub = new PoolGetDriverStub(); $poolStub->setDriver($driver); $item->setPool($poolStub); $item->setKey($key); $this->assertTrue($item->set($key), 'Able to load and store memcache driver using default server'); }
protected function getAndLog($args) { $result = call_user_func_array(array($this->cache, 'get'), $args); $miss = $this->cache->isMiss(); $key = $this->cache->getKey(); if (isset($this->logger)) { $this->logger->logRequest($key, !$miss, $result); } return $result; }
public function testConstruction() { $key = array('apple', 'sauce'); $options = array(); $driver = new Sqlite(); $driver->setOptions($options); $item = new Item(); $poolSub = new PoolGetDriverStub(); $poolSub->setDriver($driver); $item->setPool($poolSub); $item->setKey($key); $this->assertTrue($item->set($key), 'Able to load and store with unconfigured extension.'); }
public function testConstruction() { $key = array('apple', 'sauce'); $options = array(); $options['servers'][] = array('127.0.0.1', '11211', '50'); $driver = new Memcache($options); $item = new Item(); $poolStub = new PoolGetDriverStub(); $poolStub->setDriver($driver); $item->setPool($poolStub); $item->setKey($key); $this->assertTrue($item->set($key)->save(), 'Able to load and store with unconfigured extension.'); }
protected function getItem($key, $exceptionDriver = false) { if ($exceptionDriver) { $fullDriver = 'Stash\\Test\\Stubs\\DriverExceptionStub'; } else { $fullDriver = 'Stash\\Driver\\Ephemeral'; } $item = new Item(); $poolStub = new PoolGetDriverStub(); $poolStub->setDriver(new $fullDriver()); $item->setPool($poolStub); $item->setKey($key); return $item; }
public function testKeyCollisions2() { $driver = new $this->driverClass(); $poolStub = new PoolGetDriverStub(); $poolStub->setDriver($driver); $item1 = new Item(); $item1->setPool($poolStub); $item1->setKey(array('#')); $item1->set('X'); $item2 = new Item(); $item2->setPool($poolStub); $item2->setKey(array(':')); $item2->set('Y'); $this->assertEquals('X', $item1->get()); }
/** * {@inheritdoc} */ public function get($invalidation = 0, $arg = null, $arg2 = null) { $result = parent::get($invalidation, $arg, $arg2); if (isset($this->tracker)) { $miss = $this->isMiss(); $key = $this->getKey(); $this->tracker->trackRequest($key, !$miss, $result); } return $result; }
/** * Test that the paths are created using the key hash function. */ public function testOptionKeyHashFunctionDirs() { $hashfunctions = array('Stash\\Test\\Driver\\strdup', 'strrev', 'md5', function ($value) { return abs(crc32($value)); }); $paths = array('one', 'two', 'three', 'four'); foreach ($hashfunctions as $hashfunction) { $driver = new FileSystem($this->getOptions(array('keyHashFunction' => $hashfunction, 'path' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'stash', 'dirSplit' => 1))); $rand = str_repeat(uniqid(), 32); $item = new Item(); $poolStub = new PoolGetDriverStub(); $poolStub->setDriver($driver); $item->setPool($poolStub); $item->setKey($paths); $item->set($rand)->save(); $allpaths = array_merge(array('cache'), $paths); $predicted = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'stash' . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, array_map($hashfunction, $allpaths)) . $this->extension; $this->assertFileExists($predicted); } }
public function testClear() { $item = new Item(); $poolStub = new PoolGetDriverStub(); $poolStub->setDriver(new DriverExceptionStub()); $item->setPool($poolStub); $item->setKey(array('path', 'to', 'clear')); $this->assertFalse($item->isDisabled()); $this->assertFalse($item->clear()); $this->assertTrue($item->isDisabled(), 'Is disabled after exception is thrown in driver'); }
public function testDisableCacheGlobally() { Item::$runtimeDisable = true; $testDriver = $this->getMockedDriver(); $item = $this->getItem(); $poolStub = new PoolGetDriverStub(); $poolStub->setDriver($this->getMockedDriver()); $item->setPool($poolStub); $item->setKey(array('test', 'key')); $this->assertDisabledStash($item); $this->assertTrue($item->isDisabled()); $this->assertFalse($testDriver->wasCalled(), 'Driver was not called after Item was disabled.'); Item::$runtimeDisable = false; }
private function assertDisabledStash(Item $stash) { $this->assertFalse($stash->set('true'), 'storeData returns false for disabled cache'); $this->assertNull($stash->get(), 'getData returns null for disabled cache'); $this->assertFalse($stash->clear(), 'clear returns false for disabled cache'); $this->assertTrue($stash->isMiss(), 'isMiss returns true for disabled cache'); $this->assertFalse($stash->extendCache(), 'extendCache returns false for disabled cache'); $this->assertTrue($stash->lock(100), 'lock returns true for disabled cache'); }
/** * {@inheritdoc} */ protected function validateRecord($validation, &$record) { $expiration = $_ =& $record['expiration']; if (isset($record['data'], $record['data']['return']) && is_array($record['data']['return']) && isset($record['data']['return'][static::DATA_FIELD], $record['data']['return'][static::TAGS_FIELD]) && !empty($record['data']['return'][static::TAGS_FIELD])) { $tags = $record['data']['return'][static::TAGS_FIELD]; foreach ($this->getTagItems(array_keys($record['data']['return'][static::TAGS_FIELD])) as $tag => $tagItem) { $tagVersion = $tagItem->get(); if ($tagVersion !== $tags[$tag]) { unset($record['expiration']); break; } } } parent::validateRecord($validation, $record); if ($expiration !== null) { $record['expiration'] = $expiration; } }