Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
fixed zendframework/zendframework#6324: Redis::setItems: the last ite…
Browse files Browse the repository at this point in the history
…m is overwritten
  • Loading branch information
marc-mabe authored and Ocramius committed Nov 22, 2014
1 parent 0a8945e commit 5e75ab8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/Storage/Adapter/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ protected function internalGetItems(array & $normalizedKeys)
$redis = $this->getRedisResource();

$namespacedKeys = array();
foreach ($normalizedKeys as & $normalizedKey) {
foreach ($normalizedKeys as $normalizedKey) {
$namespacedKeys[] = $this->namespacePrefix . $normalizedKey;
}

Expand Down Expand Up @@ -252,8 +252,8 @@ protected function internalSetItems(array & $normalizedKeyValuePairs)
$ttl = $this->getOptions()->getTtl();

$namespacedKeyValuePairs = array();
foreach ($normalizedKeyValuePairs as $normalizedKey => & $value) {
$namespacedKeyValuePairs[$this->namespacePrefix . $normalizedKey] = & $value;
foreach ($normalizedKeyValuePairs as $normalizedKey => $value) {
$namespacedKeyValuePairs[$this->namespacePrefix . $normalizedKey] = $value;
}
try {
if ($ttl > 0) {
Expand Down
53 changes: 36 additions & 17 deletions test/Storage/Adapter/CommonAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -578,36 +578,55 @@ public function testSetAndGetExpiredItems()
$this->markTestSkipped("Adapter doesn't support item expiration");
}

$ttl = $capabilities->getTtlPrecision();
$this->_options->setTtl($ttl);

$items = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3'
// item definition
$itemsHigh = array(
'keyHigh1' => 'valueHigh1',
'keyHigh2' => 'valueHigh2',
'keyHigh3' => 'valueHigh3'
);
$itemsLow = array(
'keyLow1' => 'valueLow1',
'keyLow2' => 'valueLow2',
'keyLow3' => 'valueLow3'
);
$items = $itemsHigh + $itemsLow;

$this->waitForFullSecond();
// set items with high TTL
$this->_options->setTtl(123456);
$this->assertSame(array(), $this->_storage->setItems($itemsHigh));

$this->assertSame(array(), $this->_storage->setItems($items));
// set items with low TTL
$ttl = $capabilities->getTtlPrecision();
$this->_options->setTtl($ttl);
$this->waitForFullSecond();
$this->assertSame(array(), $this->_storage->setItems($itemsLow));

// 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);
}
ksort($rs); // make comparable

$this->_options->setTtl(0);
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(array(), $rs);

// after disabling TTL all items will be available
$this->_options->setTtl(0);
$rs = $this->_storage->getItems(array_keys($items));
ksort($rs);
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);
}
}

Expand Down

0 comments on commit 5e75ab8

Please sign in to comment.