Skip to content

Commit 3afda8a

Browse files
authored
Added support for simple cache (#134)
* Added support for SimpleCache * Added tests * Bugfix with cloning * Style fix * Minor * Updated integration tests version * Bugfix * Allow same keys * cs
1 parent 9bf1aae commit 3afda8a

File tree

4 files changed

+154
-5
lines changed

4 files changed

+154
-5
lines changed

AbstractCachePool.php

Lines changed: 142 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
use Psr\Cache\CacheItemInterface;
1818
use Psr\Log\LoggerAwareInterface;
1919
use Psr\Log\LoggerInterface;
20+
use Psr\SimpleCache\CacheInterface;
2021

2122
/**
2223
* @author Aaron Scherer <[email protected]>
2324
* @author Tobias Nyholm <[email protected]>
2425
*/
25-
abstract class AbstractCachePool implements PhpCachePool, LoggerAwareInterface
26+
abstract class AbstractCachePool implements PhpCachePool, LoggerAwareInterface, CacheInterface
2627
{
2728
const SEPARATOR_TAG = '!';
2829

@@ -281,7 +282,10 @@ protected function validateKey($key)
281282
));
282283
$this->handleException($e, __FUNCTION__);
283284
}
284-
285+
if (!isset($key[0])) {
286+
$e = new InvalidArgumentException('Cache key cannot be an empty string');
287+
$this->handleException($e, __FUNCTION__);
288+
}
285289
if (preg_match('|[\{\}\(\)/\\\@\:]|', $key)) {
286290
$e = new InvalidArgumentException(sprintf(
287291
'Invalid key: "%s". The key contains one or more characters reserved for future extension: {}()/\@:',
@@ -415,4 +419,140 @@ protected function getTagKey($tag)
415419
{
416420
return 'tag'.self::SEPARATOR_TAG.$tag;
417421
}
422+
423+
/**
424+
* {@inheritdoc}
425+
*/
426+
public function get($key, $default = null)
427+
{
428+
$item = $this->getItem($key);
429+
if (!$item->isHit()) {
430+
return $default;
431+
}
432+
433+
return $item->get();
434+
}
435+
436+
/**
437+
* {@inheritdoc}
438+
*/
439+
public function set($key, $value, $ttl = null)
440+
{
441+
$item = $this->getItem($key);
442+
$item->set($value);
443+
$item->expiresAfter($ttl);
444+
445+
return $this->save($item);
446+
}
447+
448+
/**
449+
* {@inheritdoc}
450+
*/
451+
public function delete($key)
452+
{
453+
return $this->deleteItem($key);
454+
}
455+
456+
/**
457+
* {@inheritdoc}
458+
*/
459+
public function getMultiple($keys, $default = null)
460+
{
461+
if (!is_array($keys)) {
462+
if (!$keys instanceof \Traversable) {
463+
throw new InvalidArgumentException('$keys is neither an array nor Traversable');
464+
}
465+
466+
// Since we need to throw an exception if *any* key is invalid, it doesn't
467+
// make sense to wrap iterators or something like that.
468+
$keys = iterator_to_array($keys, false);
469+
}
470+
471+
$items = $this->getItems($keys);
472+
473+
return $this->generateValues($default, $items);
474+
}
475+
476+
/**
477+
* @param $default
478+
* @param $items
479+
*
480+
* @return \Generator
481+
*/
482+
private function generateValues($default, $items)
483+
{
484+
foreach ($items as $key => $item) {
485+
/** @type $item CacheItemInterface */
486+
if (!$item->isHit()) {
487+
yield $key => $default;
488+
} else {
489+
yield $key => $item->get();
490+
}
491+
}
492+
}
493+
494+
/**
495+
* {@inheritdoc}
496+
*/
497+
public function setMultiple($values, $ttl = null)
498+
{
499+
if (!is_array($values)) {
500+
if (!$values instanceof \Traversable) {
501+
throw new InvalidArgumentException('$values is neither an array nor Traversable');
502+
}
503+
}
504+
505+
$keys = [];
506+
$arrayValues = [];
507+
foreach ($values as $key => $value) {
508+
if (is_int($key)) {
509+
$key = (string) $key;
510+
}
511+
$this->validateKey($key);
512+
$keys[] = $key;
513+
$arrayValues[$key] = $value;
514+
}
515+
516+
$items = $this->getItems($keys);
517+
$itemSuccess = true;
518+
foreach ($items as $key => $item) {
519+
$item->set($arrayValues[$key]);
520+
521+
try {
522+
$item->expiresAfter($ttl);
523+
} catch (InvalidArgumentException $e) {
524+
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
525+
}
526+
527+
$itemSuccess = $itemSuccess && $this->saveDeferred($item);
528+
}
529+
530+
return $itemSuccess && $this->commit();
531+
}
532+
533+
/**
534+
* {@inheritdoc}
535+
*/
536+
public function deleteMultiple($keys)
537+
{
538+
if (!is_array($keys)) {
539+
if (!$keys instanceof \Traversable) {
540+
throw new InvalidArgumentException('$keys is neither an array nor Traversable');
541+
}
542+
543+
// Since we need to throw an exception if *any* key is invalid, it doesn't
544+
// make sense to wrap iterators or something like that.
545+
$keys = iterator_to_array($keys, false);
546+
}
547+
548+
return $this->deleteItems($keys);
549+
}
550+
551+
/**
552+
* {@inheritdoc}
553+
*/
554+
public function has($key)
555+
{
556+
return $this->hasItem($key);
557+
}
418558
}

CacheItem.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Cache\Adapter\Common;
1313

1414
use Cache\Adapter\Common\Exception\InvalidArgumentException;
15+
use Cache\TagInterop\TaggableCacheItemInterface;
1516

1617
/**
1718
* @author Aaron Scherer <[email protected]>
@@ -140,8 +141,10 @@ public function expiresAt($expiration)
140141
{
141142
if ($expiration instanceof \DateTimeInterface) {
142143
$this->expirationTimestamp = $expiration->getTimestamp();
143-
} else {
144+
} elseif (is_int($expiration) || null === $expiration) {
144145
$this->expirationTimestamp = $expiration;
146+
} else {
147+
throw new InvalidArgumentException('Cache item ttl/expiresAt must be of type integer or \DateTimeInterface.');
145148
}
146149

147150
return $this;
@@ -160,6 +163,8 @@ public function expiresAfter($time)
160163
$this->expirationTimestamp = $date->getTimestamp();
161164
} elseif (is_int($time)) {
162165
$this->expirationTimestamp = time() + $time;
166+
} else {
167+
throw new InvalidArgumentException('Cache item ttl/expiresAfter must be of type integer or \DateInterval.');
163168
}
164169

165170
return $this;

Exception/InvalidArgumentException.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Cache\Adapter\Common\Exception;
1313

14-
class InvalidArgumentException extends CacheException implements \Psr\Cache\InvalidArgumentException
14+
use Psr\Cache\InvalidArgumentException as CacheInvalidArgumentException;
15+
use Psr\SimpleCache\InvalidArgumentException as SimpleCacheInvalidArgumentException;
16+
17+
class InvalidArgumentException extends CacheException implements CacheInvalidArgumentException, SimpleCacheInvalidArgumentException
1518
{
1619
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
"require": {
2626
"php": "^5.5 || ^7.0",
2727
"psr/cache": "^1.0",
28+
"psr/simple-cache": "^1.0",
2829
"psr/log": "^1.0",
2930
"cache/tag-interop": "^1.0"
3031
},
3132
"require-dev": {
3233
"phpunit/phpunit": "^4.0 || ^5.1",
33-
"cache/integration-tests": "^0.14"
34+
"cache/integration-tests": "^0.16"
3435
},
3536
"autoload": {
3637
"psr-4": {

0 commit comments

Comments
 (0)