Skip to content

Commit

Permalink
add cache dependency support
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed Aug 3, 2023
1 parent 804d03d commit beba7c0
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
35 changes: 33 additions & 2 deletions src/CacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
namespace yii1tech\psr\cache;

use CComponent;
use Psr\Cache\CacheItemInterface;

/**
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
class CacheItem extends CComponent implements CacheItemInterface
class CacheItem extends CComponent implements CacheItemContract
{
/**
* @var string cache item key (ID).
Expand All @@ -26,6 +25,11 @@ class CacheItem extends CComponent implements CacheItemInterface
*/
private $_expire;

/**
* @var \ICacheDependency|null dependency of the cache item.
*/
private $_dependency;

/**
* Sets the key for the current cache item.
*
Expand All @@ -47,6 +51,25 @@ public function getExpire()
return $this->_expire;
}

/**
* @return \ICacheDependency|null dependency of the cache item.
*/
public function getDependency(): ?\ICacheDependency
{
return $this->_dependency;
}

/**
* @param \ICacheDependency|null $dependency dependency of the cache item.
* @return static self reference.
*/
public function setDependency(?\ICacheDependency $dependency): self
{
$this->_dependency = $dependency;

return $this;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -115,4 +138,12 @@ public function expiresAfter($time): self

return $this;
}

/**
* {@inheritdoc}
*/
public function depends(?\ICacheDependency $dependency): self
{
return $this->setDependency($dependency);
}
}
20 changes: 20 additions & 0 deletions src/CacheItemContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace yii1tech\psr\cache;

use Psr\Cache\CacheItemInterface;

/**
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
interface CacheItemContract extends CacheItemInterface
{
/**
* Sets dependency of the cached item. If the dependency changes, the item is labelled invalid.
*
* @param \ICacheDependency|null $dependency dependency of the cached item.
* @return static self reference.
*/
public function depends(?\ICacheDependency $dependency);
}
4 changes: 2 additions & 2 deletions src/CacheItemPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ public function save(CacheItemInterface $item): bool
return $this->getCache()->set(
$item->getKey(),
$item->get(),
$item->getExpire()
// @todo dependency
$item->getExpire(),
$item->getDependency()
);
}

Expand Down
32 changes: 32 additions & 0 deletions tests/CacheItemPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,36 @@ public function testAutoCommit(): void

$this->assertSame($value, $cache->get($key));
}

/**
* @depends testSave
*/
public function testSaveWithDependency(): void
{
$pool = new CacheItemPool();

$GLOBALS['test_state'] = 1;

$key = 'test';
$value = 'test-value';

$item = $pool->getItem($key);
$item->set($value);
$item->expiresAfter(DateInterval::createFromDateString('1 hour'));
$item->depends(new \CExpressionDependency('$GLOBALS["test_state"] == 1'));

$this->assertTrue($pool->save($item));

$this->assertTrue($pool->hasItem($key));

$item = $pool->getItem($key);
$this->assertTrue($item->isHit());

$GLOBALS['test_state'] = 2;

$this->assertFalse($pool->hasItem($key));

$item = $pool->getItem($key);
$this->assertFalse($item->isHit());
}
}

0 comments on commit beba7c0

Please sign in to comment.