Skip to content

Commit c08140b

Browse files
committed
Merge pull request #60 from crazedr0m/WatermarkedPeerGetList
* requested indexes in result instead of watermarked
2 parents e0171a0 + 7ef4aad commit c08140b

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
lines changed

core/Cache/CachePeer.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function getList($indexes)
150150

151151
foreach ($indexes as $key)
152152
if (null !== ($value = $this->get($key)))
153-
$out[] = $value;
153+
$out[$key] = $value;
154154

155155
return $out;
156156
}

core/Cache/WatermarkedPeer.class.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,23 @@ public function decrement($key, $value)
110110

111111
public function getList($indexes)
112112
{
113-
foreach ($indexes as &$index)
114-
$index = $this->getActualWatermark().$index;
115-
116-
return $this->peer->getList($indexes);
113+
$peerIndexMap = array();
114+
foreach ($indexes as $index)
115+
$peerIndexMap[$this->getActualWatermark().$index] = $index;
116+
117+
$peerIndexes = array_keys($peerIndexMap);
118+
$peerResult = $this->peer->getList($peerIndexes);
119+
120+
$result = array();
121+
if (!empty($peerResult)) {
122+
foreach ($peerResult as $key => $value) {
123+
$result[$peerIndexMap[$key]] = $value;
124+
}
125+
} else {
126+
$result = $peerResult;
127+
}
128+
129+
return $result;
117130
}
118131

119132
public function get($key)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
final class WatermarkedPeerTest extends TestCase
4+
{
5+
6+
public function testMultiGet()
7+
{
8+
9+
$cache = new WatermarkedPeer(new RuntimeMemory());
10+
11+
$cache->clean();
12+
13+
$cache->set('a', 'a', Cache::EXPIRES_MEDIUM);
14+
$cache->set('b', 2, Cache::EXPIRES_MEDIUM);
15+
$cache->set('c', 42.28, Cache::EXPIRES_MEDIUM);
16+
17+
$this->assertEquals($cache->get('a'), 'a');
18+
$this->assertEquals($cache->get('b'), 2);
19+
$this->assertEquals($cache->get('c'), 42.28);
20+
21+
$list = $cache->getList(array('a', 'b', 'c'));
22+
23+
$this->assertEquals(count($list), 3);
24+
25+
$this->assertEquals($list['a'], 'a');
26+
$this->assertEquals($list['b'], 2);
27+
$this->assertEquals($list['c'], 42.28);
28+
29+
$list = $cache->getList(array('a'));
30+
31+
$this->assertEquals(count($list), 1);
32+
33+
$this->assertEquals($list['a'], 'a');
34+
35+
$list = $cache->getList(array('a', 'b', 'c', 'd'));
36+
37+
$this->assertEquals(count($list), 3);
38+
39+
$this->assertEquals($list['a'], 'a');
40+
$this->assertEquals($list['b'], 2);
41+
$this->assertEquals($list['c'], 42.28);
42+
43+
$list = $cache->getList(array('d'));
44+
45+
$this->assertEquals(count($list), 0);
46+
47+
$cache->clean();
48+
}
49+
}
50+
?>

0 commit comments

Comments
 (0)