Skip to content

Commit

Permalink
fix getMultiple and increment / decrement on tagged cache
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Mar 31, 2021
1 parent f09f067 commit 0d21194
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/Illuminate/Cache/RetrievesMultipleKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ public function many(array $keys)
{
$return = [];

foreach ($keys as $key) {
$return[$key] = $this->get($key);
$keys = collect($keys)->mapWithKeys(function ($value, $key) {
return [is_string($key) ? $key : $value => is_string($key) ? $value : null];
})->all();

foreach ($keys as $key => $default) {
$return[$key] = $this->get($key, $default);
}

return $return;
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Cache/TaggedCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function putMany(array $values, $ttl = null)
*/
public function increment($key, $value = 1)
{
$this->store->increment($this->itemKey($key), $value);
return $this->store->increment($this->itemKey($key), $value);
}

/**
Expand All @@ -68,7 +68,7 @@ public function increment($key, $value = 1)
*/
public function decrement($key, $value = 1)
{
$this->store->decrement($this->itemKey($key), $value);
return $this->store->decrement($this->itemKey($key), $value);
}

/**
Expand Down
151 changes: 151 additions & 0 deletions tests/Cache/CacheTaggedCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,131 @@ public function testTagsWithStringArgument()
$this->assertSame('bar', $store->tags('bop')->get('foo'));
}

public function testWithIncrement()
{
$store = new ArrayStore;
$taggableStore = $store->tags('bop');

$taggableStore->put('foo', 5, 10);

$value = $taggableStore->increment('foo');
$this->assertSame(6, $value);

$value = $taggableStore->increment('foo');
$this->assertSame(7, $value);

$value = $taggableStore->increment('foo', 3);
$this->assertSame(10, $value);

$value = $taggableStore->increment('foo', -2);
$this->assertSame(8, $value);

$value = $taggableStore->increment('x');
$this->assertSame(1, $value);

$value = $taggableStore->increment('y', 10);
$this->assertSame(10, $value);
}

public function testWithDecrement()
{
$store = new ArrayStore;
$taggableStore = $store->tags('bop');

$taggableStore->put('foo', 50, 10);

$value = $taggableStore->decrement('foo');
$this->assertSame(49, $value);

$value = $taggableStore->decrement('foo');
$this->assertSame(48, $value);

$value = $taggableStore->decrement('foo', 3);
$this->assertSame(45, $value);

$value = $taggableStore->decrement('foo', -2);
$this->assertSame(47, $value);

$value = $taggableStore->decrement('x');
$this->assertSame(-1, $value);

$value = $taggableStore->decrement('y', 10);
$this->assertSame(-10, $value);
}

public function testMany()
{
$store = $this->getTestCacheStoreWithTagValues();

$values = $store->tags(['fruit'])->many(['a', 'e', 'b', 'd', 'c']);
$this->assertSame([
'a' => 'apple',
'e' => null,
'b' => 'banana',
'd' => null,
'c' => 'orange',
], $values);
}

public function testManyWithDefaultValues()
{
$store = $this->getTestCacheStoreWithTagValues();

$values = $store->tags(['fruit'])->many([
'a' => 147,
'e' => 547,
'b' => 'hello world!',
'x' => 'hello world!',
'd',
'c',
]);
$this->assertSame([
'a' => 'apple',
'e' => 547,
'b' => 'banana',
'x' => 'hello world!',
'd' => null,
'c' => 'orange',
], $values);
}

public function testGetMultiple()
{
$store = $this->getTestCacheStoreWithTagValues();

$values = $store->tags(['fruit'])->getMultiple(['a', 'e', 'b', 'd', 'c']);
$this->assertSame([
'a' => 'apple',
'e' => null,
'b' => 'banana',
'd' => null,
'c' => 'orange',
], $values);

$values = $store->tags(['fruit', 'color'])->getMultiple(['a', 'e', 'b', 'd', 'c']);
$this->assertSame([
'a' => 'red',
'e' => 'blue',
'b' => null,
'd' => 'yellow',
'c' => null,
], $values);
}

public function testGetMultipleWithDefaultValue()
{
$store = $this->getTestCacheStoreWithTagValues();

$values = $store->tags(['fruit', 'color'])->getMultiple(['a', 'e', 'b', 'd', 'c'], 547);
$this->assertSame([
'a' => 'red',
'e' => 'blue',
'b' => 547,
'd' => 'yellow',
'c' => 547,
], $values);
}

public function testTagsWithIncrementCanBeFlushed()
{
$store = new ArrayStore;
Expand Down Expand Up @@ -161,4 +286,30 @@ public function testRedisCacheTagsCanBeFlushed()

$redis->flush();
}

private function getTestCacheStoreWithTagValues(): ArrayStore
{
$store = new ArrayStore;

$tags = ['fruit'];
$store->tags($tags)->put('a', 'apple', 10);
$store->tags($tags)->put('b', 'banana', 10);
$store->tags($tags)->put('c', 'orange', 10);

$tags = ['fruit', 'color'];
$store->tags($tags)->putMany([
'a' => 'red',
'd' => 'yellow',
'e' => 'blue',
], 10);

$tags = ['sizes', 'shirt'];
$store->tags($tags)->putMany([
'a' => 'small',
'b' => 'medium',
'c' => 'large',
], 10);

return $store;
}
}

0 comments on commit 0d21194

Please sign in to comment.