-
-
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
364f7ed
commit d3cd932
Showing
5 changed files
with
114 additions
and
3 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 |
---|---|---|
|
@@ -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 | ||
*/ | ||
|
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
|
||
use CApplicationComponent; | ||
use Psr\Cache\CacheItemInterface; | ||
use Psr\Cache\CacheItemPoolInterface; | ||
use Yii; | ||
|
||
/** | ||
|
@@ -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. | ||
|
@@ -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; | ||
} | ||
} |
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 |
---|---|---|
@@ -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); | ||
} |
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