-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Cache] Implement PSR-16 SimpleCache v1.0 #20694
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
[Cache] Implement PSR-16 SimpleCache v1.0
- Loading branch information
commit 848a33ed3e4d06c50ee9f39d71a9aabf044d39bb
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/Symfony/Component/Cache/Adapter/SimpleCacheAdapter.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Cache\Adapter; | ||
|
||
use Psr\SimpleCache\CacheInterface; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class SimpleCacheAdapter extends AbstractAdapter | ||
{ | ||
private $pool; | ||
private $miss; | ||
|
||
public function __construct(CacheInterface $pool, $namespace = '', $defaultLifetime = 0) | ||
{ | ||
parent::__construct($namespace, $defaultLifetime); | ||
|
||
$this->pool = $pool; | ||
$this->miss = new \stdClass(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function doFetch(array $ids) | ||
{ | ||
foreach ($this->pool->getMultiple($ids, $this->miss) as $key => $value) { | ||
if ($this->miss !== $value) { | ||
yield $key => $value; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function doHave($id) | ||
{ | ||
return $this->pool->has($id); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function doClear($namespace) | ||
{ | ||
return $this->pool->clear(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function doDelete(array $ids) | ||
{ | ||
return $this->pool->deleteMultiple($ids); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function doSave(array $values, $lifetime) | ||
{ | ||
return $this->pool->setMultiple($values, 0 === $lifetime ? null : $lifetime); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Cache\Simple; | ||
|
||
use Psr\Cache\CacheItemPoolInterface; | ||
use Psr\Cache\CacheException as Psr6CacheException; | ||
use Psr\SimpleCache\CacheInterface; | ||
use Psr\SimpleCache\CacheException as SimpleCacheException; | ||
use Symfony\Component\Cache\CacheItem; | ||
use Symfony\Component\Cache\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class Psr6Cache implements CacheInterface | ||
{ | ||
private $pool; | ||
private $createCacheItem; | ||
|
||
public function __construct(CacheItemPoolInterface $pool) | ||
{ | ||
$this->pool = $pool; | ||
|
||
if ($pool instanceof Adapter\AdapterInterface) { | ||
$this->createCacheItem = \Closure::bind( | ||
function ($key, $value, $allowInt = false) { | ||
if ($allowInt && is_int($key)) { | ||
$key = (string) $key; | ||
} else { | ||
CacheItem::validateKey($key); | ||
} | ||
$item = new CacheItem(); | ||
$item->key = $key; | ||
$item->value = $value; | ||
|
||
return $item; | ||
}, | ||
null, | ||
CacheItem::class | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function get($key, $default = null) | ||
{ | ||
try { | ||
$item = $this->pool->getItem($key); | ||
} catch (SimpleCacheException $e) { | ||
throw $e; | ||
} catch (Psr6CacheException $e) { | ||
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
|
||
return $item->isHit() ? $item->get() : $default; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function set($key, $value, $ttl = null) | ||
{ | ||
try { | ||
if (null !== $f = $this->createCacheItem) { | ||
$item = $f($key, $value); | ||
} else { | ||
$item = $this->pool->getItem($key)->set($value); | ||
} | ||
} catch (SimpleCacheException $e) { | ||
throw $e; | ||
} catch (Psr6CacheException $e) { | ||
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
if (null !== $ttl) { | ||
$item->expiresAfter($ttl); | ||
} | ||
|
||
return $this->pool->save($item); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function delete($key) | ||
{ | ||
try { | ||
return $this->pool->deleteItem($key); | ||
} catch (SimpleCacheException $e) { | ||
throw $e; | ||
} catch (Psr6CacheException $e) { | ||
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function clear() | ||
{ | ||
return $this->pool->clear(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getMultiple($keys, $default = null) | ||
{ | ||
if ($keys instanceof \Traversable) { | ||
$keys = iterator_to_array($keys, false); | ||
} elseif (!is_array($keys)) { | ||
throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', is_object($keys) ? get_class($keys) : gettype($keys))); | ||
} | ||
|
||
try { | ||
$items = $this->pool->getItems($keys); | ||
} catch (SimpleCacheException $e) { | ||
throw $e; | ||
} catch (Psr6CacheException $e) { | ||
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
$values = array(); | ||
|
||
foreach ($items as $key => $item) { | ||
$values[$key] = $item->isHit() ? $item->get() : $default; | ||
} | ||
|
||
return $values; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setMultiple($values, $ttl = null) | ||
{ | ||
$valuesIsArray = is_array($values); | ||
if (!$valuesIsArray && !$values instanceof \Traversable) { | ||
throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given', is_object($values) ? get_class($values) : gettype($values))); | ||
} | ||
$items = array(); | ||
|
||
try { | ||
if (null !== $f = $this->createCacheItem) { | ||
$valuesIsArray = false; | ||
foreach ($values as $key => $value) { | ||
$items[$key] = $f($key, $value, true); | ||
} | ||
} elseif ($valuesIsArray) { | ||
$items = array(); | ||
foreach ($values as $key => $value) { | ||
$items[] = (string) $key; | ||
} | ||
$items = $this->pool->getItems($items); | ||
} else { | ||
foreach ($values as $key => $value) { | ||
if (is_int($key)) { | ||
$key = (string) $key; | ||
} | ||
$items[$key] = $this->pool->getItem($key)->set($value); | ||
} | ||
} | ||
} catch (SimpleCacheException $e) { | ||
throw $e; | ||
} catch (Psr6CacheException $e) { | ||
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
$ok = true; | ||
|
||
foreach ($items as $key => $item) { | ||
if ($valuesIsArray) { | ||
$item->set($values[$key]); | ||
} | ||
if (null !== $ttl) { | ||
$item->expiresAfter($ttl); | ||
} | ||
$ok = $this->pool->saveDeferred($item) && $ok; | ||
} | ||
|
||
return $this->pool->commit() && $ok; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function deleteMultiple($keys) | ||
{ | ||
if ($keys instanceof \Traversable) { | ||
$keys = iterator_to_array($keys, false); | ||
} elseif (!is_array($keys)) { | ||
throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', is_object($keys) ? get_class($keys) : gettype($keys))); | ||
} | ||
|
||
try { | ||
return $this->pool->deleteItems($keys); | ||
} catch (SimpleCacheException $e) { | ||
throw $e; | ||
} catch (Psr6CacheException $e) { | ||
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function has($key) | ||
{ | ||
try { | ||
return $this->pool->hasItem($key); | ||
} catch (SimpleCacheException $e) { | ||
throw $e; | ||
} catch (Psr6CacheException $e) { | ||
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Symfony/Component/Cache/Tests/Adapter/SimpleCacheAdapterTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Cache\Tests\Adapter; | ||
|
||
use Symfony\Component\Cache\Adapter\FilesystemAdapter; | ||
use Symfony\Component\Cache\Adapter\SimpleCacheAdapter; | ||
use Symfony\Component\Cache\Simple\Psr6Cache; | ||
|
||
/** | ||
* @group time-sensitive | ||
*/ | ||
class SimpleCacheAdapterTest extends AdapterTestCase | ||
{ | ||
public function createCachePool($defaultLifetime = 0) | ||
{ | ||
return new SimpleCacheAdapter(new Psr6Cache(new FilesystemAdapter()), '', $defaultLifetime); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this call would be performed only when the iteration starts rather than when
doFetch
is called. Is it an issue ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ids have already been validated by the AbstractAdapter. Since this is the only situation that is allowed to throw, it's OK this way to me.