| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation App Framework — WordPress Cache adapter. |
| 4 |
* |
| 5 |
* Backed by the object cache under one group. Without a persistent |
| 6 |
* object cache drop-in this only lives for the request, which is |
| 7 |
* exactly the contract: a miss is always safe. |
| 8 |
* |
| 9 |
* @package OpenStation |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace OpenStation\App\WordPress; |
| 13 |
|
| 14 |
use OpenStation\App\Contracts\Cache as CacheContract; |
| 15 |
|
| 16 |
defined( 'ABSPATH' ) || exit; |
| 17 |
|
| 18 |
/** |
| 19 |
* Object-cache backed cache. |
| 20 |
*/ |
| 21 |
final class Cache implements CacheContract { |
| 22 |
|
| 23 |
const GROUP = 'openstation_apps'; |
| 24 |
|
| 25 |
/** {@inheritDoc} */ |
| 26 |
public function get( $key, $fallback = null ) { |
| 27 |
$found = false; |
| 28 |
$value = wp_cache_get( (string) $key, self::GROUP, false, $found ); |
| 29 |
return $found ? $value : $fallback; |
| 30 |
} |
| 31 |
|
| 32 |
/** {@inheritDoc} */ |
| 33 |
public function set( $key, $value, $ttl = 0 ) { |
| 34 |
wp_cache_set( (string) $key, $value, self::GROUP, max( 0, (int) $ttl ) ); |
| 35 |
} |
| 36 |
|
| 37 |
/** {@inheritDoc} */ |
| 38 |
public function delete( $key ) { |
| 39 |
wp_cache_delete( (string) $key, self::GROUP ); |
| 40 |
} |
| 41 |
} |
| 42 |
|