| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation App Framework — standalone Cache adapter. |
| 4 |
* |
| 5 |
* In-process only: lives for the request, honours TTLs, forgets |
| 6 |
* everything when the process ends. |
| 7 |
* |
| 8 |
* @package OpenStation |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace OpenStation\App\Standalone; |
| 12 |
|
| 13 |
use OpenStation\App\Contracts\Cache as CacheContract; |
| 14 |
|
| 15 |
// Direct access, unless a standalone host is booting on bare PHP. |
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
defined( 'OPENSTATION_STANDALONE' ) || exit; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Per-process cache. |
| 22 |
*/ |
| 23 |
final class Cache implements CacheContract { |
| 24 |
|
| 25 |
/** |
| 26 |
* `key => array( expires_at|0, value )`. |
| 27 |
* |
| 28 |
* @var array<string,array{0:int,1:mixed}> |
| 29 |
*/ |
| 30 |
private $items = array(); |
| 31 |
|
| 32 |
/** {@inheritDoc} */ |
| 33 |
public function get( $key, $fallback = null ) { |
| 34 |
if ( ! isset( $this->items[ $key ] ) ) { |
| 35 |
return $fallback; |
| 36 |
} |
| 37 |
list( $expires_at, $value ) = $this->items[ $key ]; |
| 38 |
if ( 0 !== $expires_at && $expires_at < time() ) { |
| 39 |
unset( $this->items[ $key ] ); |
| 40 |
return $fallback; |
| 41 |
} |
| 42 |
return $value; |
| 43 |
} |
| 44 |
|
| 45 |
/** {@inheritDoc} */ |
| 46 |
public function set( $key, $value, $ttl = 0 ) { |
| 47 |
$ttl = (int) $ttl; |
| 48 |
$this->items[ $key ] = array( $ttl > 0 ? time() + $ttl : 0, $value ); |
| 49 |
} |
| 50 |
|
| 51 |
/** {@inheritDoc} */ |
| 52 |
public function delete( $key ) { |
| 53 |
unset( $this->items[ $key ] ); |
| 54 |
} |
| 55 |
} |
| 56 |
|