Skip to content

Commit

Permalink
draft implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed Aug 3, 2023
1 parent 3b8acb2 commit daa7e0c
Show file tree
Hide file tree
Showing 5 changed files with 450 additions and 5 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: mbstring
extensions: mbstring, pdo, sqlite, pdo_sqlite, memcached
tools: composer:v2
coverage: none

- name: Install Memcached.
uses: niden/actions-memcached@v7

- name: Install dependencies
run: |
composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi
Expand Down
106 changes: 105 additions & 1 deletion src/CacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
167 changes: 165 additions & 2 deletions src/CacheItemPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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.
*/
Expand All @@ -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;
}
}
Loading

0 comments on commit daa7e0c

Please sign in to comment.