| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* An extremely simple non persistent cache system. |
| 8 |
* |
| 9 |
* @since 1.7 |
| 10 |
* |
| 11 |
* @template TCacheData |
| 12 |
*/ |
| 13 |
class PLL_Cache { |
| 14 |
/** |
| 15 |
* Current site id. |
| 16 |
* |
| 17 |
* @var int |
| 18 |
*/ |
| 19 |
protected $blog_id; |
| 20 |
|
| 21 |
/** |
| 22 |
* The cache container. |
| 23 |
* |
| 24 |
* @var array |
| 25 |
* |
| 26 |
* @phpstan-var array<int, array<non-empty-string, TCacheData>> |
| 27 |
*/ |
| 28 |
protected $cache = array(); |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor. |
| 32 |
* |
| 33 |
* @since 1.7 |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
$this->blog_id = get_current_blog_id(); |
| 37 |
add_action( 'switch_blog', array( $this, 'switch_blog' ) ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Called when switching blog. |
| 42 |
* |
| 43 |
* @since 1.7 |
| 44 |
* |
| 45 |
* @param int $new_blog_id New blog ID. |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public function switch_blog( $new_blog_id ) { |
| 49 |
$this->blog_id = $new_blog_id; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Adds a value in cache. |
| 54 |
* |
| 55 |
* @since 1.7 |
| 56 |
* @since 3.6 Returns the cached value. |
| 57 |
* |
| 58 |
* @param string $key Cache key. |
| 59 |
* @param mixed $data The value to add to the cache. |
| 60 |
* @return mixed |
| 61 |
* |
| 62 |
* @phpstan-param non-empty-string $key |
| 63 |
* @phpstan-param TCacheData $data |
| 64 |
* @phpstan-return TCacheData |
| 65 |
*/ |
| 66 |
public function set( $key, $data ) { |
| 67 |
$this->cache[ $this->blog_id ][ $key ] = $data; |
| 68 |
|
| 69 |
return $data; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Returns value from cache. |
| 74 |
* |
| 75 |
* @since 1.7 |
| 76 |
* |
| 77 |
* @param string $key Cache key. |
| 78 |
* @return mixed |
| 79 |
* |
| 80 |
* @phpstan-param non-empty-string $key |
| 81 |
* @phpstan-return TCacheData|false |
| 82 |
*/ |
| 83 |
public function get( $key ) { |
| 84 |
return $this->cache[ $this->blog_id ][ $key ] ?? false; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Cleans the cache (for this blog only). |
| 89 |
* |
| 90 |
* @since 1.7 |
| 91 |
* |
| 92 |
* @param string $key Optional. Cache key. An empty string to clean the whole cache for the current blog. |
| 93 |
* Default is an empty string. |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
public function clean( $key = '' ) { |
| 97 |
if ( '' === $key ) { |
| 98 |
unset( $this->cache[ $this->blog_id ] ); |
| 99 |
} else { |
| 100 |
unset( $this->cache[ $this->blog_id ][ $key ] ); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Generates and returns a "unique" cache key, depending on `$data` and prefixed by `$prefix`. |
| 106 |
* |
| 107 |
* @since 3.6 |
| 108 |
* |
| 109 |
* @param string $prefix String to prefix the cache key. |
| 110 |
* @param string|array|object $data Data. |
| 111 |
* @return string |
| 112 |
* |
| 113 |
* @phpstan-param non-empty-string $prefix |
| 114 |
* @phpstan-return non-empty-string |
| 115 |
*/ |
| 116 |
public function get_unique_key( string $prefix, $data ): string { |
| 117 |
/** @var scalar */ |
| 118 |
$serialized = maybe_serialize( $data ); |
| 119 |
return $prefix . md5( (string) $serialized ); |
| 120 |
} |
| 121 |
} |
| 122 |
|