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