Skip to content

Commit 067a021

Browse files
deleugpntaylorotwell
authored andcommitted
[5.5] [PSR-16] Implement SimpleCache (#20194)
* [PSR-16] Implement SimpleCache * StyleCI * Fix test for getting multiple values from cache * Add dependency to Contracts; Drop collection usage
1 parent 5f71c09 commit 067a021

File tree

5 files changed

+122
-2
lines changed

5 files changed

+122
-2
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"mtdowling/cron-expression": "~1.0",
2727
"nesbot/carbon": "~1.20",
2828
"psr/container": "~1.0",
29+
"psr/simple-cache": "^1.0",
2930
"ramsey/uuid": "~3.0",
3031
"swiftmailer/swiftmailer": "~6.0",
3132
"symfony/console": "~3.3",

src/Illuminate/Cache/Repository.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,24 @@ public function many(array $keys)
115115
})->all();
116116
}
117117

118+
/**
119+
* {@inheritdoc}
120+
*/
121+
public function getMultiple($keys, $default = null)
122+
{
123+
if (is_null($default)) {
124+
return $this->many($keys);
125+
}
126+
127+
foreach ($keys as $key) {
128+
if (! isset($default[$key])) {
129+
$default[$key] = null;
130+
}
131+
}
132+
133+
return $this->many($default);
134+
}
135+
118136
/**
119137
* Handle a result for the "many" method.
120138
*
@@ -177,6 +195,14 @@ public function put($key, $value, $minutes = null)
177195
}
178196
}
179197

198+
/**
199+
* {@inheritdoc}
200+
*/
201+
public function set($key, $value, $ttl = null)
202+
{
203+
$this->put($key, $value, $ttl);
204+
}
205+
180206
/**
181207
* Store multiple items in the cache for a given number of minutes.
182208
*
@@ -195,6 +221,14 @@ public function putMany(array $values, $minutes)
195221
}
196222
}
197223

224+
/**
225+
* {@inheritdoc}
226+
*/
227+
public function setMultiple($values, $ttl = null)
228+
{
229+
$this->putMany($values, $ttl);
230+
}
231+
198232
/**
199233
* Store an item in the cache if the key does not exist.
200234
*
@@ -340,6 +374,34 @@ public function forget($key)
340374
});
341375
}
342376

377+
/**
378+
* {@inheritdoc}
379+
*/
380+
public function delete($key)
381+
{
382+
return $this->forget($key);
383+
}
384+
385+
/**
386+
* {@inheritdoc}
387+
*/
388+
public function deleteMultiple($keys)
389+
{
390+
foreach ($keys as $key) {
391+
$this->forget($key);
392+
}
393+
394+
return true;
395+
}
396+
397+
/**
398+
* {@inheritdoc}
399+
*/
400+
public function clear()
401+
{
402+
return $this->store->flush();
403+
}
404+
343405
/**
344406
* Begin executing a new tags operation if the store supports it.
345407
*

src/Illuminate/Contracts/Cache/Repository.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
namespace Illuminate\Contracts\Cache;
44

55
use Closure;
6+
use Psr\SimpleCache\CacheInterface;
67

7-
interface Repository
8+
interface Repository extends CacheInterface
89
{
910
/**
1011
* Determine if an item exists in the cache.

src/Illuminate/Contracts/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
],
1616
"require": {
1717
"php": ">=7.0",
18-
"psr/container": "~1.0"
18+
"psr/container": "~1.0",
19+
"psr/simple-cache": "~1.0"
1920
},
2021
"autoload": {
2122
"psr-4": {

tests/Cache/CacheRepositoryTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ public function testPuttingMultipleItemsInCache()
113113
$repo->put(['foo' => 'bar', 'bar' => 'baz'], 1);
114114
}
115115

116+
public function testSettingMultipleItemsInCache()
117+
{
118+
// Alias of PuttingMultiple
119+
$repo = $this->getRepository();
120+
$repo->getStore()->shouldReceive('putMany')->once()->with(['foo' => 'bar', 'bar' => 'baz'], 1);
121+
$repo->setMultiple(['foo' => 'bar', 'bar' => 'baz'], 1);
122+
}
123+
116124
public function testPutWithDatetimeInPastOrZeroSecondsDoesntSaveItem()
117125
{
118126
$repo = $this->getRepository();
@@ -174,6 +182,53 @@ public function testRegisterMacroWithNonStaticCall()
174182
$this->assertEquals($repo->{__CLASS__}(), 'Taylor');
175183
}
176184

185+
public function testForgettingCacheKey()
186+
{
187+
$repo = $this->getRepository();
188+
$repo->getStore()->shouldReceive('forget')->once()->with('a-key')->andReturn(true);
189+
$repo->forget('a-key');
190+
}
191+
192+
public function testRemovingCacheKey()
193+
{
194+
// Alias of Forget
195+
$repo = $this->getRepository();
196+
$repo->getStore()->shouldReceive('forget')->once()->with('a-key')->andReturn(true);
197+
$repo->delete('a-key');
198+
}
199+
200+
public function testSettingCache()
201+
{
202+
$repo = $this->getRepository();
203+
$repo->getStore()->shouldReceive('put')->with($key = 'foo', $value = 'bar', 1);
204+
$repo->set($key, $value, 1);
205+
}
206+
207+
public function testClearingWholeCache()
208+
{
209+
$repo = $this->getRepository();
210+
$repo->getStore()->shouldReceive('flush')->andReturn(true);
211+
$repo->clear();
212+
}
213+
214+
public function testGettingMultipleValuesFromCache()
215+
{
216+
$keys = ['key1', 'key2', 'key3'];
217+
$default = ['key2' => 5];
218+
219+
$repo = $this->getRepository();
220+
$repo->getStore()->shouldReceive('many')->once()->with(['key2', 'key1', 'key3'])->andReturn(['key1' => 1, 'key2' => null, 'key3' => null]);
221+
$this->assertEquals(['key1' => 1, 'key2' => 5, 'key3' => null], $repo->getMultiple($keys, $default));
222+
}
223+
224+
public function testRemovingMultipleKeys()
225+
{
226+
$repo = $this->getRepository();
227+
$repo->getStore()->shouldReceive('forget')->once()->with('a-key')->andReturn(true);
228+
$repo->getStore()->shouldReceive('forget')->once()->with('a-second-key')->andReturn(true);
229+
$repo->deleteMultiple(['a-key', 'a-second-key']);
230+
}
231+
177232
protected function getRepository()
178233
{
179234
$dispatcher = new \Illuminate\Events\Dispatcher(m::mock('Illuminate\Container\Container'));

0 commit comments

Comments
 (0)