Skip to content

Commit 0d21194

Browse files
committed
fix getMultiple and increment / decrement on tagged cache
1 parent f09f067 commit 0d21194

File tree

3 files changed

+159
-4
lines changed

3 files changed

+159
-4
lines changed

src/Illuminate/Cache/RetrievesMultipleKeys.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ public function many(array $keys)
1616
{
1717
$return = [];
1818

19-
foreach ($keys as $key) {
20-
$return[$key] = $this->get($key);
19+
$keys = collect($keys)->mapWithKeys(function ($value, $key) {
20+
return [is_string($key) ? $key : $value => is_string($key) ? $value : null];
21+
})->all();
22+
23+
foreach ($keys as $key => $default) {
24+
$return[$key] = $this->get($key, $default);
2125
}
2226

2327
return $return;

src/Illuminate/Cache/TaggedCache.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function putMany(array $values, $ttl = null)
5656
*/
5757
public function increment($key, $value = 1)
5858
{
59-
$this->store->increment($this->itemKey($key), $value);
59+
return $this->store->increment($this->itemKey($key), $value);
6060
}
6161

6262
/**
@@ -68,7 +68,7 @@ public function increment($key, $value = 1)
6868
*/
6969
public function decrement($key, $value = 1)
7070
{
71-
$this->store->decrement($this->itemKey($key), $value);
71+
return $this->store->decrement($this->itemKey($key), $value);
7272
}
7373

7474
/**

tests/Cache/CacheTaggedCacheTest.php

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,131 @@ public function testTagsWithStringArgument()
5656
$this->assertSame('bar', $store->tags('bop')->get('foo'));
5757
}
5858

59+
public function testWithIncrement()
60+
{
61+
$store = new ArrayStore;
62+
$taggableStore = $store->tags('bop');
63+
64+
$taggableStore->put('foo', 5, 10);
65+
66+
$value = $taggableStore->increment('foo');
67+
$this->assertSame(6, $value);
68+
69+
$value = $taggableStore->increment('foo');
70+
$this->assertSame(7, $value);
71+
72+
$value = $taggableStore->increment('foo', 3);
73+
$this->assertSame(10, $value);
74+
75+
$value = $taggableStore->increment('foo', -2);
76+
$this->assertSame(8, $value);
77+
78+
$value = $taggableStore->increment('x');
79+
$this->assertSame(1, $value);
80+
81+
$value = $taggableStore->increment('y', 10);
82+
$this->assertSame(10, $value);
83+
}
84+
85+
public function testWithDecrement()
86+
{
87+
$store = new ArrayStore;
88+
$taggableStore = $store->tags('bop');
89+
90+
$taggableStore->put('foo', 50, 10);
91+
92+
$value = $taggableStore->decrement('foo');
93+
$this->assertSame(49, $value);
94+
95+
$value = $taggableStore->decrement('foo');
96+
$this->assertSame(48, $value);
97+
98+
$value = $taggableStore->decrement('foo', 3);
99+
$this->assertSame(45, $value);
100+
101+
$value = $taggableStore->decrement('foo', -2);
102+
$this->assertSame(47, $value);
103+
104+
$value = $taggableStore->decrement('x');
105+
$this->assertSame(-1, $value);
106+
107+
$value = $taggableStore->decrement('y', 10);
108+
$this->assertSame(-10, $value);
109+
}
110+
111+
public function testMany()
112+
{
113+
$store = $this->getTestCacheStoreWithTagValues();
114+
115+
$values = $store->tags(['fruit'])->many(['a', 'e', 'b', 'd', 'c']);
116+
$this->assertSame([
117+
'a' => 'apple',
118+
'e' => null,
119+
'b' => 'banana',
120+
'd' => null,
121+
'c' => 'orange',
122+
], $values);
123+
}
124+
125+
public function testManyWithDefaultValues()
126+
{
127+
$store = $this->getTestCacheStoreWithTagValues();
128+
129+
$values = $store->tags(['fruit'])->many([
130+
'a' => 147,
131+
'e' => 547,
132+
'b' => 'hello world!',
133+
'x' => 'hello world!',
134+
'd',
135+
'c',
136+
]);
137+
$this->assertSame([
138+
'a' => 'apple',
139+
'e' => 547,
140+
'b' => 'banana',
141+
'x' => 'hello world!',
142+
'd' => null,
143+
'c' => 'orange',
144+
], $values);
145+
}
146+
147+
public function testGetMultiple()
148+
{
149+
$store = $this->getTestCacheStoreWithTagValues();
150+
151+
$values = $store->tags(['fruit'])->getMultiple(['a', 'e', 'b', 'd', 'c']);
152+
$this->assertSame([
153+
'a' => 'apple',
154+
'e' => null,
155+
'b' => 'banana',
156+
'd' => null,
157+
'c' => 'orange',
158+
], $values);
159+
160+
$values = $store->tags(['fruit', 'color'])->getMultiple(['a', 'e', 'b', 'd', 'c']);
161+
$this->assertSame([
162+
'a' => 'red',
163+
'e' => 'blue',
164+
'b' => null,
165+
'd' => 'yellow',
166+
'c' => null,
167+
], $values);
168+
}
169+
170+
public function testGetMultipleWithDefaultValue()
171+
{
172+
$store = $this->getTestCacheStoreWithTagValues();
173+
174+
$values = $store->tags(['fruit', 'color'])->getMultiple(['a', 'e', 'b', 'd', 'c'], 547);
175+
$this->assertSame([
176+
'a' => 'red',
177+
'e' => 'blue',
178+
'b' => 547,
179+
'd' => 'yellow',
180+
'c' => 547,
181+
], $values);
182+
}
183+
59184
public function testTagsWithIncrementCanBeFlushed()
60185
{
61186
$store = new ArrayStore;
@@ -161,4 +286,30 @@ public function testRedisCacheTagsCanBeFlushed()
161286

162287
$redis->flush();
163288
}
289+
290+
private function getTestCacheStoreWithTagValues(): ArrayStore
291+
{
292+
$store = new ArrayStore;
293+
294+
$tags = ['fruit'];
295+
$store->tags($tags)->put('a', 'apple', 10);
296+
$store->tags($tags)->put('b', 'banana', 10);
297+
$store->tags($tags)->put('c', 'orange', 10);
298+
299+
$tags = ['fruit', 'color'];
300+
$store->tags($tags)->putMany([
301+
'a' => 'red',
302+
'd' => 'yellow',
303+
'e' => 'blue',
304+
], 10);
305+
306+
$tags = ['sizes', 'shirt'];
307+
$store->tags($tags)->putMany([
308+
'a' => 'small',
309+
'b' => 'medium',
310+
'c' => 'large',
311+
], 10);
312+
313+
return $store;
314+
}
164315
}

0 commit comments

Comments
 (0)