Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 24 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| DependencyWrapper | |
0.00% |
0 / 24 |
|
0.00% |
0 / 6 |
210 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| isExpired | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| initialiseDeps | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| getValue | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| storeToCache | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getValueFromCache | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | use Wikimedia\ObjectCache\BagOStuff; |
| 8 | |
| 9 | /** |
| 10 | * Store an arbitrary value whilst representing several CacheDependency objects as one. |
| 11 | * |
| 12 | * You should typically only use DependencyWrapper::getValueFromCache(), |
| 13 | * rather than instantiating one of these objects directly. |
| 14 | * |
| 15 | * @ingroup Language |
| 16 | */ |
| 17 | class DependencyWrapper { |
| 18 | /** @var mixed */ |
| 19 | private $value; |
| 20 | /** @var CacheDependency[] */ |
| 21 | private $deps; |
| 22 | |
| 23 | /** |
| 24 | * @param mixed $value The user-supplied value |
| 25 | * @param CacheDependency|CacheDependency[] $deps A dependency or dependency |
| 26 | * array. All dependencies must be objects implementing CacheDependency. |
| 27 | */ |
| 28 | public function __construct( $value = false, $deps = [] ) { |
| 29 | $this->value = $value; |
| 30 | |
| 31 | if ( !is_array( $deps ) ) { |
| 32 | $deps = [ $deps ]; |
| 33 | } |
| 34 | |
| 35 | $this->deps = $deps; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Returns true if any of the dependencies have expired |
| 40 | * |
| 41 | * @return bool |
| 42 | */ |
| 43 | public function isExpired() { |
| 44 | foreach ( $this->deps as $dep ) { |
| 45 | if ( $dep->isExpired() ) { |
| 46 | return true; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Initialise dependency values in preparation for storing. This must be |
| 55 | * called before serialization. |
| 56 | */ |
| 57 | public function initialiseDeps() { |
| 58 | foreach ( $this->deps as $dep ) { |
| 59 | $dep->loadDependencyValues(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get the user-defined value |
| 65 | * @return bool|mixed |
| 66 | */ |
| 67 | public function getValue() { |
| 68 | return $this->value; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Store the wrapper to a cache |
| 73 | * |
| 74 | * @param BagOStuff $cache |
| 75 | * @param string $key |
| 76 | * @param int $expiry |
| 77 | */ |
| 78 | public function storeToCache( $cache, $key, $expiry = 0 ) { |
| 79 | $this->initialiseDeps(); |
| 80 | $cache->set( $key, $this, $expiry ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Attempt to get a value from the cache. If the value is expired or missing, |
| 85 | * it will be generated with the callback function (if present), and the newly |
| 86 | * calculated value will be stored to the cache in a wrapper. |
| 87 | * |
| 88 | * @param BagOStuff $cache |
| 89 | * @param string $key The cache key |
| 90 | * @param int $expiry The expiry timestamp or interval in seconds |
| 91 | * @param callable|false $callback The callback for generating the value, or false |
| 92 | * @param array $callbackParams The function parameters for the callback |
| 93 | * @param array $deps The dependencies to store on a cache miss. Note: these |
| 94 | * are not the dependencies used on a cache hit! Cache hits use the stored |
| 95 | * dependency array. |
| 96 | * |
| 97 | * @return mixed The value, or null if it was not present in the cache and no |
| 98 | * callback was defined. |
| 99 | */ |
| 100 | public static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false, |
| 101 | $callbackParams = [], $deps = [] |
| 102 | ) { |
| 103 | $obj = $cache->get( $key ); |
| 104 | |
| 105 | if ( is_object( $obj ) && $obj instanceof DependencyWrapper && !$obj->isExpired() ) { |
| 106 | $value = $obj->value; |
| 107 | } elseif ( $callback ) { |
| 108 | $value = $callback( ...$callbackParams ); |
| 109 | # Cache the newly-generated value |
| 110 | $wrapper = new DependencyWrapper( $value, $deps ); |
| 111 | $wrapper->storeToCache( $cache, $key, $expiry ); |
| 112 | } else { |
| 113 | $value = null; |
| 114 | } |
| 115 | |
| 116 | return $value; |
| 117 | } |
| 118 | } |