-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
3b8acb2
commit daa7e0c
Showing
5 changed files
with
450 additions
and
5 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,116 @@ | |
namespace yii1tech\psr\cache; | ||
|
||
use CComponent; | ||
use Psr\Cache\CacheItemInterface; | ||
|
||
/** | ||
* @author Paul Klimov <[email protected]> | ||
* @since 1.0 | ||
*/ | ||
class CacheItem extends CComponent | ||
class CacheItem extends CComponent implements CacheItemInterface | ||
{ | ||
/** | ||
* @var string cache item key (ID). | ||
*/ | ||
private $_key; | ||
|
||
/** | ||
* @var mixed cache item value. | ||
*/ | ||
private $_value; | ||
|
||
/** | ||
* @var int|null cache item expire. | ||
*/ | ||
private $_expire; | ||
|
||
/** | ||
* Sets the key for the current cache item. | ||
* | ||
* @param string $key the key string for this cache item. | ||
* @return static self reference. | ||
*/ | ||
public function setKey(string $key): self | ||
{ | ||
$this->_key = $key; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return int|null cache item expiration in seconds. | ||
*/ | ||
public function getExpire() | ||
{ | ||
return $this->_expire; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getKey(): string | ||
{ | ||
return $this->_key; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function get(): mixed | ||
{ | ||
if ($this->_value === false) { | ||
return null; | ||
} | ||
|
||
return $this->_value; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isHit(): bool | ||
{ | ||
return $this->_value !== false; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function set($value): static | ||
{ | ||
$this->_value = $value; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function expiresAt($expiration): static | ||
{ | ||
if ($expiration === null) { | ||
$this->_expire = null; | ||
} else { | ||
$this->_expire = $expiration->getTimestamp() - time(); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function expiresAfter($time): static | ||
{ | ||
if ($time === null) { | ||
$this->_expire = null; | ||
} elseif ($time instanceof \DateInterval) { | ||
$timestamp = (new \DateTime())->add($time)->getTimestamp(); | ||
$this->_expire = $timestamp - time(); | ||
} else { | ||
$this->_expire = (int) $time; | ||
} | ||
|
||
return $this; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -3,20 +3,45 @@ | |
namespace yii1tech\psr\cache; | ||
|
||
use CApplicationComponent; | ||
use Psr\Cache\CacheItemInterface; | ||
use Psr\Cache\CacheItemPoolInterface; | ||
use Yii; | ||
|
||
/** | ||
* @author Paul Klimov <[email protected]> | ||
* @since 1.0 | ||
*/ | ||
class CacheItemPool extends CApplicationComponent | ||
class CacheItemPool extends CApplicationComponent implements CacheItemPoolInterface | ||
{ | ||
/** | ||
* @var \ICache|array|string | ||
* @var bool whether to automatically commit all deferred items on object destruction. | ||
*/ | ||
public $autocommit = true; | ||
|
||
/** | ||
* @var \ICache|array|string wrapped Yii cache component. | ||
*/ | ||
private $_cache = 'cache'; | ||
|
||
/** | ||
* @var \Psr\Cache\CacheItemInterface[] deferred cache items. | ||
*/ | ||
private $_deferredItems = []; | ||
|
||
/** | ||
* Destructor. | ||
* Commits deferred cache items, if {@see $autocommit} is enabled. | ||
*/ | ||
public function __destruct() | ||
{ | ||
if ($this->autocommit) { | ||
$this->commit(); | ||
} | ||
} | ||
|
||
/** | ||
* Returns wrapped Yii cache component instance. | ||
* | ||
* @return \ICache Yii cache component instance. | ||
*/ | ||
public function getCache() | ||
|
@@ -33,6 +58,8 @@ public function getCache() | |
} | ||
|
||
/** | ||
* Sets the Yii cache component to be used for cache items storage. | ||
* | ||
* @param \ICache|array|string $cache cache component instance, application component ID or array configuration. | ||
* @return static self reference. | ||
*/ | ||
|
@@ -43,5 +70,141 @@ public function setCache($cache): self | |
return $this; | ||
} | ||
|
||
/** | ||
* @return \Psr\Cache\CacheItemInterface[] deferred cache items. | ||
*/ | ||
public function getDeferredItems(): array | ||
{ | ||
return $this->_deferredItems; | ||
} | ||
|
||
/** | ||
* Instantiates cache item. | ||
* | ||
* @param string $key cache item key. | ||
* @param mixed $value cache item value. | ||
* @return \Psr\Cache\CacheItemInterface cache item instance. | ||
*/ | ||
protected function createCacheItem($key, $value): CacheItemInterface | ||
{ | ||
$item = new CacheItem(); | ||
$item->setKey($key); | ||
$item->set($value); | ||
|
||
return $item; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getItem($key): CacheItemInterface | ||
{ | ||
if (isset($this->_deferredItems[$key])) { | ||
return $this->_deferredItems[$key]; | ||
} | ||
|
||
$value = $this->getCache()->get($key); | ||
|
||
return $this->createCacheItem($key, $value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getItems(array $keys = []): iterable | ||
{ | ||
$items = []; | ||
foreach ($this->getCache()->mget($keys) as $key => $value) { | ||
$items[$key] = $this->createCacheItem($key, $value); | ||
} | ||
|
||
return $items; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasItem($key): bool | ||
{ | ||
return $this->getCache()->get($key) !== false; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function clear(): bool | ||
{ | ||
return $this->getCache()->flush(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function deleteItem($key): bool | ||
{ | ||
return $this->getCache()->delete($key); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function deleteItems(array $keys): bool | ||
{ | ||
$cache = $this->getCache(); | ||
|
||
$result = true; | ||
|
||
foreach ($keys as $key) { | ||
if (!$cache->delete($key)) { | ||
$result = false; | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function save(CacheItemInterface $item): bool | ||
{ | ||
if (!$item instanceof CacheItem) { | ||
return false; | ||
} | ||
|
||
return $this->getCache()->set( | ||
$item->getKey(), | ||
$item->get(), | ||
$item->getExpire() | ||
// @todo dependency | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function saveDeferred(CacheItemInterface $item): bool | ||
{ | ||
$this->_deferredItems[$item->getKey()] = $item; | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function commit(): bool | ||
{ | ||
$result = true; | ||
|
||
foreach ($this->_deferredItems as $key => $item) { | ||
if ($this->save($item)) { | ||
unset($this->_deferredItems[$key]); | ||
} else { | ||
$result = false; | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
} |
Oops, something went wrong.