From c05894dd86e740ce31e5f49d56893b1928135fe1 Mon Sep 17 00:00:00 2001 From: Deleu Date: Thu, 20 Jul 2017 12:30:29 -0300 Subject: [PATCH 1/4] [PSR-16] Implement SimpleCache --- composer.json | 1 + src/Illuminate/Cache/Repository.php | 63 +++++++++++++++++++ src/Illuminate/Contracts/Cache/Repository.php | 3 +- tests/Cache/CacheRepositoryTest.php | 55 ++++++++++++++++ 4 files changed, 121 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b9d8137557dd..8c6aa59747d0 100644 --- a/composer.json +++ b/composer.json @@ -26,6 +26,7 @@ "mtdowling/cron-expression": "~1.0", "nesbot/carbon": "~1.20", "psr/container": "~1.0", + "psr/simple-cache": "^1.0", "ramsey/uuid": "~3.0", "swiftmailer/swiftmailer": "~6.0", "symfony/console": "~3.3", diff --git a/src/Illuminate/Cache/Repository.php b/src/Illuminate/Cache/Repository.php index 2632e1f4ae41..65fe0dc49ab5 100755 --- a/src/Illuminate/Cache/Repository.php +++ b/src/Illuminate/Cache/Repository.php @@ -114,6 +114,25 @@ public function many(array $keys) })->all(); } + /** + * {@inheritdoc} + */ + public function getMultiple($keys, $default = null) + { + if (is_null($default)) + return $this->many($keys); + + $default = collect($default); + + foreach ($keys as $key) { + if (! $default->has($key)) { + $default->put($key, null); + } + } + + return $this->many($default->toArray()); + } + /** * Handle a result for the "many" method. * @@ -176,6 +195,14 @@ public function put($key, $value, $minutes = null) } } + /** + * {@inheritdoc} + */ + public function set($key, $value, $ttl = null) + { + $this->put($key, $value, $ttl); + } + /** * Store multiple items in the cache for a given number of minutes. * @@ -194,6 +221,14 @@ public function putMany(array $values, $minutes) } } + /** + * {@inheritdoc} + */ + public function setMultiple($values, $ttl = null) + { + $this->putMany($values, $ttl); + } + /** * Store an item in the cache if the key does not exist. * @@ -339,6 +374,34 @@ public function forget($key) }); } + /** + * {@inheritdoc} + */ + public function delete($key) + { + return $this->forget($key); + } + + /** + * {@inheritdoc} + */ + public function deleteMultiple($keys) + { + foreach ($keys as $key) { + $this->forget($key); + } + + return true; + } + + /** + * {@inheritdoc} + */ + public function clear() + { + return $this->store->flush(); + } + /** * Begin executing a new tags operation if the store supports it. * diff --git a/src/Illuminate/Contracts/Cache/Repository.php b/src/Illuminate/Contracts/Cache/Repository.php index 3381466f8c51..8ef98078c0e1 100644 --- a/src/Illuminate/Contracts/Cache/Repository.php +++ b/src/Illuminate/Contracts/Cache/Repository.php @@ -3,8 +3,9 @@ namespace Illuminate\Contracts\Cache; use Closure; +use Psr\SimpleCache\CacheInterface; -interface Repository +interface Repository extends CacheInterface { /** * Determine if an item exists in the cache. diff --git a/tests/Cache/CacheRepositoryTest.php b/tests/Cache/CacheRepositoryTest.php index 29e2135abb5e..b61c58206890 100755 --- a/tests/Cache/CacheRepositoryTest.php +++ b/tests/Cache/CacheRepositoryTest.php @@ -113,6 +113,14 @@ public function testPuttingMultipleItemsInCache() $repo->put(['foo' => 'bar', 'bar' => 'baz'], 1); } + public function testSettingMultipleItemsInCache() + { + // Alias of PuttingMultiple + $repo = $this->getRepository(); + $repo->getStore()->shouldReceive('putMany')->once()->with(['foo' => 'bar', 'bar' => 'baz'], 1); + $repo->setMultiple(['foo' => 'bar', 'bar' => 'baz'], 1); + } + public function testPutWithDatetimeInPastOrZeroSecondsDoesntSaveItem() { $repo = $this->getRepository(); @@ -174,6 +182,53 @@ public function testRegisterMacroWithNonStaticCall() $this->assertEquals($repo->{__CLASS__}(), 'Taylor'); } + public function testForgettingCacheKey() + { + $repo = $this->getRepository(); + $repo->getStore()->shouldReceive('forget')->once()->with('a-key')->andReturn(true); + $repo->forget('a-key'); + } + + public function testRemovingCacheKey() + { + // Alias of Forget + $repo = $this->getRepository(); + $repo->getStore()->shouldReceive('forget')->once()->with('a-key')->andReturn(true); + $repo->remove('a-key'); + } + + public function testSettingCache() + { + $repo = $this->getRepository(); + $repo->getStore()->shouldReceive('put')->with($key = 'foo', $value = 'bar', 1); + $repo->set($key, $value, 1); + } + + public function testClearingWholeCache() + { + $repo = $this->getRepository(); + $repo->getStore()->shouldReceive('flush')->andReturn(true); + $repo->clear(); + } + + public function testGettingMultipleValuesFromCache() + { + $keys = ['key1', 'key2', 'key3']; + $default = ['key2' => 5]; + + $repo = $this->getRepository(); + $repo->getStore()->shouldReceive('many')->once()->with(['key1' => null, 'key2' => 5, 'key3' => null])->andReturn(['key1' => 1, 'key2' => 5, 'key3' => null]); + $this->assertEquals(['key1' => 1, 'key2' => 5, 'key3' => null], $repo->getMultiple($keys, $default)); + } + + public function testRemovingMultipleKeys() + { + $repo = $this->getRepository(); + $repo->getStore()->shouldReceive('forget')->once()->with('a-key')->andReturn(true); + $repo->getStore()->shouldReceive('forget')->once()->with('a-second-key')->andReturn(true); + $repo->deleteMultiple(['a-key', 'a-second-key']); + } + protected function getRepository() { $dispatcher = new \Illuminate\Events\Dispatcher(m::mock('Illuminate\Container\Container')); From 7740fd7cad0a37f1ec0d6251db7a8d5ed687d7cc Mon Sep 17 00:00:00 2001 From: Deleu Date: Thu, 20 Jul 2017 12:34:16 -0300 Subject: [PATCH 2/4] StyleCI --- src/Illuminate/Cache/Repository.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Cache/Repository.php b/src/Illuminate/Cache/Repository.php index 65fe0dc49ab5..66e4d3fc2f3e 100755 --- a/src/Illuminate/Cache/Repository.php +++ b/src/Illuminate/Cache/Repository.php @@ -119,8 +119,9 @@ public function many(array $keys) */ public function getMultiple($keys, $default = null) { - if (is_null($default)) + if (is_null($default)) { return $this->many($keys); + } $default = collect($default); From 75c237b042425ac312583c1c4b84758a75a3c586 Mon Sep 17 00:00:00 2001 From: Deleu Date: Thu, 20 Jul 2017 12:51:38 -0300 Subject: [PATCH 3/4] Fix test for getting multiple values from cache --- tests/Cache/CacheRepositoryTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Cache/CacheRepositoryTest.php b/tests/Cache/CacheRepositoryTest.php index b61c58206890..c66bb759f6a7 100755 --- a/tests/Cache/CacheRepositoryTest.php +++ b/tests/Cache/CacheRepositoryTest.php @@ -194,7 +194,7 @@ public function testRemovingCacheKey() // Alias of Forget $repo = $this->getRepository(); $repo->getStore()->shouldReceive('forget')->once()->with('a-key')->andReturn(true); - $repo->remove('a-key'); + $repo->delete('a-key'); } public function testSettingCache() @@ -217,7 +217,7 @@ public function testGettingMultipleValuesFromCache() $default = ['key2' => 5]; $repo = $this->getRepository(); - $repo->getStore()->shouldReceive('many')->once()->with(['key1' => null, 'key2' => 5, 'key3' => null])->andReturn(['key1' => 1, 'key2' => 5, 'key3' => null]); + $repo->getStore()->shouldReceive('many')->once()->with(['key2', 'key1', 'key3'])->andReturn(['key1' => 1, 'key2' => null, 'key3' => null]); $this->assertEquals(['key1' => 1, 'key2' => 5, 'key3' => null], $repo->getMultiple($keys, $default)); } From 43235c584006d268a376a4bd8e4e19772ba35263 Mon Sep 17 00:00:00 2001 From: Deleu Date: Thu, 20 Jul 2017 14:00:05 -0300 Subject: [PATCH 4/4] Add dependency to Contracts; Drop collection usage --- src/Illuminate/Cache/Repository.php | 8 +++----- src/Illuminate/Contracts/composer.json | 3 ++- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Cache/Repository.php b/src/Illuminate/Cache/Repository.php index 66e4d3fc2f3e..30bb4113e741 100755 --- a/src/Illuminate/Cache/Repository.php +++ b/src/Illuminate/Cache/Repository.php @@ -123,15 +123,13 @@ public function getMultiple($keys, $default = null) return $this->many($keys); } - $default = collect($default); - foreach ($keys as $key) { - if (! $default->has($key)) { - $default->put($key, null); + if (! isset($default[$key])) { + $default[$key] = null; } } - return $this->many($default->toArray()); + return $this->many($default); } /** diff --git a/src/Illuminate/Contracts/composer.json b/src/Illuminate/Contracts/composer.json index c34277664037..c97fc256a5ca 100644 --- a/src/Illuminate/Contracts/composer.json +++ b/src/Illuminate/Contracts/composer.json @@ -15,7 +15,8 @@ ], "require": { "php": ">=7.0", - "psr/container": "~1.0" + "psr/container": "~1.0", + "psr/simple-cache": "~1.0" }, "autoload": { "psr-4": {