public function testSetAndGetExpiredItems() { $capabilities = $this->_storage->getCapabilities(); if ($capabilities->getMinTtl() === 0) { $this->markTestSkipped("Adapter doesn't support item expiration"); } $ttl = $capabilities->getTtlPrecision(); $this->_options->setTtl($ttl); $items = array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'); $this->assertSame(array(), $this->_storage->setItems($items)); // wait until expired $wait = $ttl + $capabilities->getTtlPrecision(); usleep($wait * 2000000); $rs = $this->_storage->getItems(array_keys($items)); if (!$capabilities->getUseRequestTime()) { $this->assertEquals(array(), $rs); } else { ksort($rs); $this->assertEquals($items, $rs); } $this->_options->setTtl(0); if ($capabilities->getExpiredRead()) { $rs = $this->_storage->getItems(array_keys($items)); ksort($rs); $this->assertEquals($items, $rs); } }
/** * @test */ public function delegatesGetItems() { $items = ['foo']; $cached = ['foo' => 'bar']; $this->storage->getItems($items)->willReturn($cached); $return = $this->cache->getItems($items); $this->assertSame($cached, $return); }
/** * Returns a traversable set of cache items. * * @param array $keys * An indexed array of keys of items to retrieve. * * @throws InvalidArgumentException * If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException * MUST be thrown. * * @return array|\Traversable * A traversable collection of Cache Items keyed by the cache keys of * each item. A Cache item will be returned for each key, even if that * key is not found. However, if no keys are specified then an empty * traversable MUST be returned instead. */ public function getItems(array $keys = []) { $this->validateKeys($keys); try { $cacheItems = $this->storage->getItems($keys); } catch (Exception\InvalidArgumentException $e) { throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); } catch (Exception\ExceptionInterface $e) { throw new CacheException($e->getMessage(), $e->getCode(), $e); } $items = []; foreach ($cacheItems as $key => $value) { $items[$key] = new CacheItem($key, $value, true); } // Return empty items for any keys that where not found foreach (array_diff($keys, array_keys($cacheItems)) as $key) { $items[$key] = new CacheItem($key, null, false); } return $items; }
public function testSetAndGetExpiredItems() { $capabilities = $this->_storage->getCapabilities(); if ($capabilities->getMinTtl() === 0) { $this->markTestSkipped("Adapter doesn't support item expiration"); } // item definition $itemsHigh = ['keyHigh1' => 'valueHigh1', 'keyHigh2' => 'valueHigh2', 'keyHigh3' => 'valueHigh3']; $itemsLow = ['keyLow1' => 'valueLow1', 'keyLow2' => 'valueLow2', 'keyLow3' => 'valueLow3']; $items = $itemsHigh + $itemsLow; // set items with high TTL $this->_options->setTtl(123456); $this->assertSame([], $this->_storage->setItems($itemsHigh)); // set items with low TTL $ttl = $capabilities->getTtlPrecision(); $this->_options->setTtl($ttl); $this->waitForFullSecond(); $this->assertSame([], $this->_storage->setItems($itemsLow)); // wait until expired $wait = $ttl + $capabilities->getTtlPrecision(); usleep($wait * 2000000); $rs = $this->_storage->getItems(array_keys($items)); ksort($rs); // make comparable if ($capabilities->getExpiredRead()) { // if item expiration will be done on read there is no difference // between the previos set items in TTL. // -> all items will be expired $this->assertEquals([], $rs); // after disabling TTL all items will be available $this->_options->setTtl(0); $rs = $this->_storage->getItems(array_keys($items)); ksort($rs); // make comparable $this->assertEquals($items, $rs); } elseif ($capabilities->getUseRequestTime()) { // if the request time will be used as current time all items will // be available as expiration doesn't work within the same process $this->assertEquals($items, $rs); } else { $this->assertEquals($itemsHigh, $rs); } }
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); } }
public function getItems(array $keys) { return $this->storage->getItems($keys); }