Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| WPCom_Themes_Cache | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 1 |
| run_cached | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Class WPCom_Themes_Cache. |
| 4 | * Caches WPCom themes. |
| 5 | * |
| 6 | * @package wpcom-themes |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Basic cache implementation for themes. |
| 11 | */ |
| 12 | class WPCom_Themes_Cache { |
| 13 | /** |
| 14 | * The cache group. |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | const CACHE_GROUP = 'wpcom-themes-cache'; |
| 19 | |
| 20 | /** |
| 21 | * Executes the supplied callable and caches the result. |
| 22 | * |
| 23 | * @param string $cache_key The cache key. |
| 24 | * @param callable $lambda Callable that returns theme data. |
| 25 | * @param int $ttl Time to live in seconds. |
| 26 | * |
| 27 | * @return mixed Cached data. |
| 28 | */ |
| 29 | public function run_cached( string $cache_key, callable $lambda, int $ttl = DAY_IN_SECONDS ) { |
| 30 | $data = wp_cache_get( $cache_key, self::CACHE_GROUP ); |
| 31 | |
| 32 | if ( false === $data || defined( 'IGNORE_CACHED_WPCOM_THEMES' ) ) { |
| 33 | $data = $lambda(); |
| 34 | wp_cache_set( $cache_key, $data, self::CACHE_GROUP, $ttl ); |
| 35 | } |
| 36 | |
| 37 | return $data; |
| 38 | } |
| 39 | } |