PluginProbe
Polylang / 3.7.1
Polylang v3.7.1
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
← All changes | include/cache.php +66 -20 2.8.33.7.1 View file →
@@ -3,19 +3,34 @@
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 - protected $blog_id, $cache;
14 + /**
15 + * Current site id.
16 + *
17 + * @var int
18 + */
19 + protected $blog_id;
14 20
15 21 /**
16 - * Constructor
22 + * The cache container.
17 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 + *
18 33 * @since 1.7
19 34 */
20 35 public function __construct() {
21 36 $this->blog_id = get_current_blog_id();
@@ -22,54 +37,85 @@
22 37 add_action( 'switch_blog', array( $this, 'switch_blog' ) );
23 38 }
24 39
25 40 /**
26 - * Called when switching blog
41 + * Called when switching blog.
27 42 *
28 43 * @since 1.7
29 44 *
30 - * @param int $new_blog
45 + * @param int $new_blog_id New blog ID.
46 + * @return void
31 47 */
32 - public function switch_blog( $new_blog ) {
33 - $this->blog_id = $new_blog;
48 + public function switch_blog( $new_blog_id ) {
49 + $this->blog_id = $new_blog_id;
34 50 }
35 51
36 52 /**
37 - * Add a value in cache
53 + * Adds a value in cache.
38 54 *
39 55 * @since 1.7
56 + * @since 3.6 Returns the cached value.
40 57 *
41 - * @param string $key
42 - * @param mixed $data
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
43 65 */
44 66 public function set( $key, $data ) {
45 67 $this->cache[ $this->blog_id ][ $key ] = $data;
68 +
69 + return $data;
46 70 }
47 71
48 72 /**
49 - * Get value from cache
73 + * Returns value from cache.
50 74 *
51 75 * @since 1.7
52 76 *
53 - * @param string $key
54 - * @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
55 82 */
56 83 public function get( $key ) {
57 - return isset( $this->cache[ $this->blog_id ][ $key ] ) ? $this->cache[ $this->blog_id ][ $key ] : false;
84 + return $this->cache[ $this->blog_id ][ $key ] ?? false;
58 85 }
59 86
60 87 /**
61 - * Clean the cache (for this blog only)
88 + * Cleans the cache (for this blog only).
62 89 *
63 90 * @since 1.7
64 91 *
65 - * @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.
94 + * @return void
66 95 */
67 96 public function clean( $key = '' ) {
68 - if ( empty( $key ) ) {
97 + if ( '' === $key ) {
69 98 unset( $this->cache[ $this->blog_id ] );
70 - }
71 - else {
99 + } else {
72 100 unset( $this->cache[ $this->blog_id ][ $key ] );
73 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 );
74 120 }
75 121 }