| @@ -3,12 +3,13 @@ | ||
| 3 | 3 | * @package Polylang |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | - * An extremely simple non persistent cache system | |
| 8 | - * not as fast as using directly an array but more readable | |
| 7 | + * An extremely simple non persistent cache system. | |
| 9 | 8 | * |
| 10 | 9 | * @since 1.7 |
| 10 | + * | |
| 11 | + * @template TCacheData | |
| 11 | 12 | */ |
| 12 | 13 | class PLL_Cache { |
| 13 | 14 | /** |
| 14 | 15 | * Current site id. |
| @@ -20,13 +21,15 @@ | ||
| 20 | 21 | /** |
| 21 | 22 | * The cache container. |
| 22 | 23 | * |
| 23 | 24 | * @var array |
| 25 | + * | |
| 26 | + * @phpstan-var array<int, array<non-empty-string, TCacheData>> | |
| 24 | 27 | */ |
| 25 | - protected $cache; | |
| 28 | + protected $cache = array(); | |
| 26 | 29 | |
| 27 | 30 | /** |
| 28 | - * Constructor | |
| 31 | + * Constructor. | |
| 29 | 32 | * |
| 30 | 33 | * @since 1.7 |
| 31 | 34 | */ |
| 32 | 35 | public function __construct() { |
| @@ -34,39 +37,49 @@ | ||
| 34 | 37 | add_action( 'switch_blog', array( $this, 'switch_blog' ) ); |
| 35 | 38 | } |
| 36 | 39 | |
| 37 | 40 | /** |
| 38 | - * Called when switching blog | |
| 41 | + * Called when switching blog. | |
| 39 | 42 | * |
| 40 | 43 | * @since 1.7 |
| 41 | 44 | * |
| 42 | - * @param int $new_blog | |
| 45 | + * @param int $new_blog_id New blog ID. | |
| 43 | 46 | * @return void |
| 44 | 47 | */ |
| 45 | - public function switch_blog( $new_blog ) { | |
| 46 | - $this->blog_id = $new_blog; | |
| 48 | + public function switch_blog( $new_blog_id ) { | |
| 49 | + $this->blog_id = $new_blog_id; | |
| 47 | 50 | } |
| 48 | 51 | |
| 49 | 52 | /** |
| 50 | - * Add a value in cache | |
| 53 | + * Adds a value in cache. | |
| 51 | 54 | * |
| 52 | 55 | * @since 1.7 |
| 56 | + * @since 3.6 Returns the cached value. | |
| 53 | 57 | * |
| 54 | - * @param string $key | |
| 55 | - * @param mixed $data | |
| 56 | - * @return void | |
| 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 | |
| 57 | 65 | */ |
| 58 | 66 | public function set( $key, $data ) { |
| 59 | 67 | $this->cache[ $this->blog_id ][ $key ] = $data; |
| 68 | + | |
| 69 | + return $data; | |
| 60 | 70 | } |
| 61 | 71 | |
| 62 | 72 | /** |
| 63 | - * Get value from cache | |
| 73 | + * Returns value from cache. | |
| 64 | 74 | * |
| 65 | 75 | * @since 1.7 |
| 66 | 76 | * |
| 67 | - * @param string $key | |
| 68 | - * @return mixed $data | |
| 77 | + * @param string $key Cache key. | |
| 78 | + * @return mixed | |
| 79 | + * | |
| 80 | + * @phpstan-param non-empty-string $key | |
| 81 | + * @phpstan-return TCacheData|false | |
| 69 | 82 | */ |
| 70 | 83 | public function get( $key ) { |
| 71 | 84 | return isset( $this->cache[ $this->blog_id ][ $key ] ) ? $this->cache[ $this->blog_id ][ $key ] : false; |
| 72 | 85 | } |
| @@ -71,20 +84,38 @@ | ||
| 71 | 84 | return isset( $this->cache[ $this->blog_id ][ $key ] ) ? $this->cache[ $this->blog_id ][ $key ] : false; |
| 72 | 85 | } |
| 73 | 86 | |
| 74 | 87 | /** |
| 75 | - * Clean the cache (for this blog only) | |
| 88 | + * Cleans the cache (for this blog only). | |
| 76 | 89 | * |
| 77 | 90 | * @since 1.7 |
| 78 | 91 | * |
| 79 | - * @param string $key | |
| 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. | |
| 80 | 94 | * @return void |
| 81 | 95 | */ |
| 82 | 96 | public function clean( $key = '' ) { |
| 83 | - if ( empty( $key ) ) { | |
| 97 | + if ( '' === $key ) { | |
| 84 | 98 | unset( $this->cache[ $this->blog_id ] ); |
| 85 | - } | |
| 86 | - else { | |
| 99 | + } else { | |
| 87 | 100 | unset( $this->cache[ $this->blog_id ][ $key ] ); |
| 88 | 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 ); | |
| 89 | 120 | } |
| 90 | 121 | } |