Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] [PSR-16] Implement SimpleCache #20194

Merged
merged 4 commits into from
Jul 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
62 changes: 62 additions & 0 deletions src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@ public function many(array $keys)
})->all();
}

/**
* {@inheritdoc}
*/
public function getMultiple($keys, $default = null)
{
if (is_null($default)) {
return $this->many($keys);
}

foreach ($keys as $key) {
if (! isset($default[$key])) {
$default[$key] = null;
}
}

return $this->many($default);
}

/**
* Handle a result for the "many" method.
*
Expand Down Expand Up @@ -176,6 +194,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.
*
Expand All @@ -194,6 +220,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.
*
Expand Down Expand Up @@ -339,6 +373,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.
*
Expand Down
3 changes: 2 additions & 1 deletion src/Illuminate/Contracts/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Illuminate\Contracts\Cache;

use Closure;
use Psr\SimpleCache\CacheInterface;

interface Repository
interface Repository extends CacheInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that this should be \Illuminate\Cache\Repository that implements Psr\SimpleCache\CacheInterface

Copy link
Contributor Author

@deleugpn deleugpn Jul 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@lucasmichot lucasmichot Jul 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah true, it' weird. I had a look at \Illuminate\Contracts\Container\Container too, which extends \Psr\Container\ContainerInterface

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevertheless @deleugpn make sure to add this "psr/simple-cache": "~1.0" in Illuminate/Contracts/composer.json too 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

{
/**
* Determine if an item exists in the cache.
Expand Down
3 changes: 2 additions & 1 deletion src/Illuminate/Contracts/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
],
"require": {
"php": ">=7.0",
"psr/container": "~1.0"
"psr/container": "~1.0",
"psr/simple-cache": "~1.0"
},
"autoload": {
"psr-4": {
Expand Down
55 changes: 55 additions & 0 deletions tests/Cache/CacheRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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->delete('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(['key2', 'key1', 'key3'])->andReturn(['key1' => 1, 'key2' => null, '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'));
Expand Down