Skip to content

Commit

Permalink
add CacheItemPoolContract
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed Aug 4, 2023
1 parent 364f7ed commit d3cd932
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function getCategoriesCount()
```


### Simplified interface <span id="simplified-interface"></span>
### Extended interface <span id="extended-interface"></span>


### Using cache tags <span id="using-cache-tags"></span>
Expand Down
6 changes: 6 additions & 0 deletions src/CacheItemContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
use Psr\Cache\CacheItemInterface;

/**
* CacheItemContract extends {@see \Psr\Cache\CacheItemInterface}, adding extra features.
*
* It allows managing cache dependencies.
*
* @see \yii1tech\psr\cache\CacheItemPoolContract
*
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
Expand Down
23 changes: 21 additions & 2 deletions src/CacheItemPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use CApplicationComponent;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Yii;

/**
Expand Down Expand Up @@ -34,7 +33,7 @@
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
class CacheItemPool extends CApplicationComponent implements CacheItemPoolInterface
class CacheItemPool extends CApplicationComponent implements CacheItemPoolContract
{
/**
* @var bool whether to automatically commit all deferred items on object destruction.
Expand Down Expand Up @@ -230,4 +229,24 @@ public function commit(): bool

return $result;
}

/**
* {@inheritdoc}
*/
public function get(string $key, callable $callback)
{
$item = $this->getItem($key);

if ($item->isHit()) {
return $item->get();
}

$value = call_user_func($callback, $item);

$item->set($value);

$this->save($item);

return $value;
}
}
54 changes: 54 additions & 0 deletions src/CacheItemPoolContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace yii1tech\psr\cache;

use Psr\Cache\CacheItemPoolInterface;

/**
* CacheItemPoolContract extends {@see \Psr\Cache\CacheItemPoolInterface}, adding extra features to it.
*
* @see \yii1tech\psr\cache\CacheItemContract
*
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
interface CacheItemPoolContract extends CacheItemPoolInterface
{
/**
* {@inheritdoc}
*
* @return \yii1tech\psr\cache\CacheItemContract the corresponding Cache Item.
*/
public function getItem($key);

/**
* {@inheritdoc}
*
* @return \Traversable|array<string, \yii1tech\psr\cache\CacheItemContract> collection of Cache Items keyed by the cache keys of each item.
*/
public function getItems(array $keys = []);

/**
* Fetches a value from the pool or computes it via given callback if not found.
* Usage example:
*
* ```php
* $value = $pool->get('example-cache-key', function (CacheItemContract $item) {
* $item->expiresAfter(3600);
*
* // ...
*
* return $computedValue; // heavy computations result
* });
* ```
*
* @template T
*
* @param string $key the key of the item to retrieve from the cache.
* @param (callable(CacheItemContract,bool):T)|(callable(CacheItemContract,bool):T) $callback callback, which computes value to be cached.
* @return T cached value or callback result.
*/
public function get(string $key, callable $callback);

//public function invalidateTags(array $tags);
}
32 changes: 32 additions & 0 deletions tests/CacheItemPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use CDummyCache;
use DateInterval;
use ICache;
use yii1tech\psr\cache\CacheItemContract;
use yii1tech\psr\cache\CacheItemPool;

class CacheItemPoolTest extends TestCase
Expand Down Expand Up @@ -232,4 +233,35 @@ public function testSaveWithDependency(): void
$item = $pool->getItem($key);
$this->assertFalse($item->isHit());
}

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

$key = 'test';

$value = $pool->get($key, function (CacheItemContract $item) {
$item->expiresAfter(DateInterval::createFromDateString('1 hour'));

return 'test-value';
});

$this->assertSame('test-value', $value);

$item = $pool->getItem($key);
$this->assertTrue($item->isHit());
$this->assertSame('test-value', $item->get());

$value = $pool->get($key, function (CacheItemContract $item) {
$item->expiresAfter(DateInterval::createFromDateString('1 hour'));

return 'new-value';
});

$this->assertSame('test-value', $value);
$this->assertSame('test-value', $pool->getItem($key)->get());
}
}

0 comments on commit d3cd932

Please sign in to comment.