Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ea260f
commit 53dfd8c
Showing
16 changed files
with
693 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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
Empty file.
Empty file.
This file contains 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,149 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ACache library. | ||
* | ||
* (c) Martin Rademacher <mano@radebatz.net> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Radebatz\ACache\Decorators\Psr; | ||
|
||
use DateInterval; | ||
use DateTime; | ||
use DateTimeInterface; | ||
use Psr\Cache\CacheItemInterface; | ||
|
||
/** | ||
* ACache Psr CacheItem implementation. | ||
*/ | ||
class CacheItem implements CacheItemInterface | ||
{ | ||
protected $key; | ||
protected $value; | ||
/** @var $cache CacheItemPool */ | ||
protected $cacheItemPool; | ||
/** @var $expiresAt DateTimeInterface */ | ||
protected $expiresAt; | ||
|
||
/** | ||
* Create a new cache item. | ||
*/ | ||
public function __construct($key, $value, CacheItemPool $cacheItemPool, $ttl = null) | ||
{ | ||
if (strpbrk($key, CacheItemPool::BAD_KEY_CHARS)) { | ||
throw new InvalidArgumentException(sprintf('Invalid key: %s', $key)); | ||
} | ||
|
||
$this->key = $key; | ||
$this->value = $value; | ||
$this->cacheItemPool = $cacheItemPool; | ||
$this->setExpiresAt($ttl); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getKey() | ||
{ | ||
return $this->key; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function get() | ||
{ | ||
return $this->isHit() ? $this->value : null; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function set($value, $ttl = null) | ||
{ | ||
$this->value = $value; | ||
$this->setExpiresAt($ttl); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function isHit() | ||
{ | ||
return $this->cacheItemPool->getCache()->contains($this->key); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function expiresAt($expiration) | ||
{ | ||
if (null === $expiration || ($expiration instanceof DateTimeInterface) || ($expiration instanceof DateTime)) { | ||
$this->setExpiresAt($expiration); | ||
|
||
return $this; | ||
} | ||
|
||
throw new InvalidArgumentException('Invalid expiration date'); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function expiresAfter($time) | ||
{ | ||
if (is_int($time)) { | ||
$this->setExpiresAt($time); | ||
|
||
return $this; | ||
} elseif ($time instanceof DateInterval) { | ||
$now = new DateTime(); | ||
$this->setExpiresAt($now->add($time)); | ||
|
||
return $this; | ||
} | ||
|
||
throw new InvalidArgumentException('Invalid expiration time'); | ||
} | ||
|
||
/** | ||
* Get the value irrespective of whether it is in cache or not. | ||
* | ||
* @return mixed The value. | ||
*/ | ||
public function getValue() | ||
{ | ||
return $this->value; | ||
} | ||
|
||
/** | ||
* Get the expires at value. | ||
* | ||
* @return DateTimeInterface The expires at date/time. | ||
*/ | ||
public function getExpiresAt() | ||
{ | ||
return $this->expiresAt; | ||
} | ||
|
||
/** | ||
* Set expires at value from given ttl. | ||
* | ||
* @param int|DateTimeInterface $ttl | ||
*/ | ||
protected function setExpiresAt($ttl) | ||
{ | ||
if (is_int($ttl)) { | ||
$this->expiresAt = new DateTime('@'.(time() + $ttl)); | ||
} elseif (($ttl instanceof DateTimeInterface) || ($ttl instanceof DateTime)) { | ||
$this->expiresAt = $ttl; | ||
} else { | ||
$this->expiresAt = null; | ||
} | ||
} | ||
} |
This file contains 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,167 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ACache library. | ||
* | ||
* (c) Martin Rademacher <mano@radebatz.net> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Radebatz\ACache\Decorators\Psr; | ||
|
||
use Psr\Cache\CacheItemInterface; | ||
use Psr\Cache\CacheItemPoolInterface; | ||
use Radebatz\ACache\CacheInterface; | ||
|
||
/** | ||
* ACache Psr CacheItemPool implementation. | ||
*/ | ||
class CacheItemPool implements CacheItemPoolInterface | ||
{ | ||
const BAD_KEY_CHARS = '{}()/\@:'; | ||
|
||
/** @var $cache CacheInterface */ | ||
protected $cache; | ||
protected $deferred; | ||
|
||
/** | ||
* Create a new cache item pool. | ||
* | ||
* @param $cache CacheInterface The underlying cache instance. | ||
*/ | ||
public function __construct(CacheInterface $cache) | ||
{ | ||
$this->cache = $cache; | ||
$this->deferred = array(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function hasItem($key) | ||
{ | ||
if (strpbrk($key, CacheItemPool::BAD_KEY_CHARS)) { | ||
throw new InvalidArgumentException(sprintf('Invalid key: %s', $key)); | ||
} | ||
|
||
if (array_key_exists($key, $this->deferred)) { | ||
return true; | ||
} | ||
|
||
return $this->cache->contains($key); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getItem($key) | ||
{ | ||
if (strpbrk($key, CacheItemPool::BAD_KEY_CHARS)) { | ||
throw new InvalidArgumentException(sprintf('Invalid key: %s', $key)); | ||
} | ||
|
||
if (array_key_exists($key, $this->deferred)) { | ||
return $this->deferred[$key]; | ||
} | ||
|
||
$ttl = null; | ||
$value = null; | ||
if ($this->cache->contains($key)) { | ||
$ttl = $this->cache->getTimeToLive($key); | ||
$value = $this->cache->fetch($key); | ||
} | ||
|
||
return new CacheItem($key, $value, $this, 0 === $ttl ? null : $ttl); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getItems(array $keys = array()) | ||
{ | ||
$items = array(); | ||
foreach ($keys as $key) { | ||
$items[] = $this->getItem($key); | ||
} | ||
|
||
return $items; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function clear() | ||
{ | ||
$this->cache->flush(); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function deleteItem($key) | ||
{ | ||
$this->cache->delete($key); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function deleteItems(array $keys) | ||
{ | ||
foreach ($keys as $key) { | ||
$this->cache->delete($key); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function save(CacheItemInterface $item) | ||
{ | ||
$expiresAt = $item->getExpiresAt(); | ||
$this->cache->save($item->getKey(), $item->getValue(), null === $expiresAt ? 0 : $expiresAt->getTimestamp() - time()); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function saveDeferred(CacheItemInterface $item) | ||
{ | ||
// TODO: improve $this->deferred[$item->getKey()] = $item; | ||
$this->save($item); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function commit() | ||
{ | ||
foreach ($this->deferred as $item) { | ||
$this->save($item); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Get the underlying cache instance. | ||
* | ||
* @return CacheInterface The cache. | ||
*/ | ||
public function getCache() | ||
{ | ||
return $this->cache; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Radebatz/ACache/Decorators/Psr/InvalidArgumentException.php
This file contains 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,16 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ACache library. | ||
* | ||
* (c) Martin Rademacher <mano@radebatz.net> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Radebatz\ACache\Decorators\Psr; | ||
|
||
class InvalidArgumentException extends \Exception implements \Psr\Cache\InvalidArgumentException | ||
{ | ||
} |
Empty file.
Empty file.
Empty file.
Empty file.
Oops, something went wrong.