diff --git a/src/Illuminate/Cache/RetrievesMultipleKeys.php b/src/Illuminate/Cache/RetrievesMultipleKeys.php index 5dd41edb5e7f..7db7a0aa50af 100644 --- a/src/Illuminate/Cache/RetrievesMultipleKeys.php +++ b/src/Illuminate/Cache/RetrievesMultipleKeys.php @@ -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; diff --git a/src/Illuminate/Cache/TaggedCache.php b/src/Illuminate/Cache/TaggedCache.php index 01e483b6ea66..ae5ad8e3e07d 100644 --- a/src/Illuminate/Cache/TaggedCache.php +++ b/src/Illuminate/Cache/TaggedCache.php @@ -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); } /** @@ -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); } /** diff --git a/tests/Cache/CacheTaggedCacheTest.php b/tests/Cache/CacheTaggedCacheTest.php index 2fee3e8e84de..fca40fcf6cef 100644 --- a/tests/Cache/CacheTaggedCacheTest.php +++ b/tests/Cache/CacheTaggedCacheTest.php @@ -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; @@ -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; + } }