diff --git a/src/CacheItem.php b/src/CacheItem.php index d1b6abd..75a6fce 100644 --- a/src/CacheItem.php +++ b/src/CacheItem.php @@ -3,13 +3,12 @@ namespace yii1tech\psr\cache; use CComponent; -use Psr\Cache\CacheItemInterface; /** * @author Paul Klimov * @since 1.0 */ -class CacheItem extends CComponent implements CacheItemInterface +class CacheItem extends CComponent implements CacheItemContract { /** * @var string cache item key (ID). @@ -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. * @@ -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} */ @@ -115,4 +138,12 @@ public function expiresAfter($time): self return $this; } + + /** + * {@inheritdoc} + */ + public function depends(?\ICacheDependency $dependency): self + { + return $this->setDependency($dependency); + } } \ No newline at end of file diff --git a/src/CacheItemContract.php b/src/CacheItemContract.php new file mode 100644 index 0000000..76bb956 --- /dev/null +++ b/src/CacheItemContract.php @@ -0,0 +1,20 @@ + + * @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); +} \ No newline at end of file diff --git a/src/CacheItemPool.php b/src/CacheItemPool.php index a6096b4..d98e72d 100644 --- a/src/CacheItemPool.php +++ b/src/CacheItemPool.php @@ -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() ); } diff --git a/tests/CacheItemPoolTest.php b/tests/CacheItemPoolTest.php index 2abbdf4..ec6d574 100644 --- a/tests/CacheItemPoolTest.php +++ b/tests/CacheItemPoolTest.php @@ -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()); + } } \ No newline at end of file