author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
16 | 1 |
<?php |
2 |
/** |
|
3 |
* Object Cache API functions missing from 3rd party object caches. |
|
4 |
* |
|
19 | 5 |
* @link https://developer.wordpress.org/reference/classes/wp_object_cache/ |
16 | 6 |
* |
7 |
* @package WordPress |
|
8 |
* @subpackage Cache |
|
9 |
*/ |
|
10 |
||
19 | 11 |
if ( ! function_exists( 'wp_cache_add_multiple' ) ) : |
12 |
/** |
|
13 |
* Adds multiple values to the cache in one call, if the cache keys don't already exist. |
|
14 |
* |
|
15 |
* Compat function to mimic wp_cache_add_multiple(). |
|
16 |
* |
|
17 |
* @ignore |
|
18 |
* @since 6.0.0 |
|
19 |
* |
|
20 |
* @see wp_cache_add_multiple() |
|
21 |
* |
|
22 |
* @param array $data Array of keys and values to be added. |
|
23 |
* @param string $group Optional. Where the cache contents are grouped. Default empty. |
|
24 |
* @param int $expire Optional. When to expire the cache contents, in seconds. |
|
25 |
* Default 0 (no expiration). |
|
26 |
* @return bool[] Array of return values, grouped by key. Each value is either |
|
27 |
* true on success, or false if cache key and group already exist. |
|
28 |
*/ |
|
29 |
function wp_cache_add_multiple( array $data, $group = '', $expire = 0 ) { |
|
30 |
$values = array(); |
|
31 |
||
32 |
foreach ( $data as $key => $value ) { |
|
33 |
$values[ $key ] = wp_cache_add( $key, $value, $group, $expire ); |
|
34 |
} |
|
35 |
||
36 |
return $values; |
|
37 |
} |
|
38 |
endif; |
|
39 |
||
40 |
if ( ! function_exists( 'wp_cache_set_multiple' ) ) : |
|
41 |
/** |
|
42 |
* Sets multiple values to the cache in one call. |
|
43 |
* |
|
44 |
* Differs from wp_cache_add_multiple() in that it will always write data. |
|
45 |
* |
|
46 |
* Compat function to mimic wp_cache_set_multiple(). |
|
47 |
* |
|
48 |
* @ignore |
|
49 |
* @since 6.0.0 |
|
50 |
* |
|
51 |
* @see wp_cache_set_multiple() |
|
52 |
* |
|
53 |
* @param array $data Array of keys and values to be set. |
|
54 |
* @param string $group Optional. Where the cache contents are grouped. Default empty. |
|
55 |
* @param int $expire Optional. When to expire the cache contents, in seconds. |
|
56 |
* Default 0 (no expiration). |
|
57 |
* @return bool[] Array of return values, grouped by key. Each value is either |
|
58 |
* true on success, or false on failure. |
|
59 |
*/ |
|
60 |
function wp_cache_set_multiple( array $data, $group = '', $expire = 0 ) { |
|
61 |
$values = array(); |
|
62 |
||
63 |
foreach ( $data as $key => $value ) { |
|
64 |
$values[ $key ] = wp_cache_set( $key, $value, $group, $expire ); |
|
65 |
} |
|
66 |
||
67 |
return $values; |
|
68 |
} |
|
69 |
endif; |
|
70 |
||
16 | 71 |
if ( ! function_exists( 'wp_cache_get_multiple' ) ) : |
72 |
/** |
|
73 |
* Retrieves multiple values from the cache in one call. |
|
74 |
* |
|
75 |
* Compat function to mimic wp_cache_get_multiple(). |
|
76 |
* |
|
77 |
* @ignore |
|
78 |
* @since 5.5.0 |
|
79 |
* |
|
80 |
* @see wp_cache_get_multiple() |
|
81 |
* |
|
82 |
* @param array $keys Array of keys under which the cache contents are stored. |
|
83 |
* @param string $group Optional. Where the cache contents are grouped. Default empty. |
|
84 |
* @param bool $force Optional. Whether to force an update of the local cache |
|
85 |
* from the persistent cache. Default false. |
|
19 | 86 |
* @return array Array of return values, grouped by key. Each value is either |
87 |
* the cache contents on success, or false on failure. |
|
16 | 88 |
*/ |
89 |
function wp_cache_get_multiple( $keys, $group = '', $force = false ) { |
|
90 |
$values = array(); |
|
91 |
||
92 |
foreach ( $keys as $key ) { |
|
93 |
$values[ $key ] = wp_cache_get( $key, $group, $force ); |
|
94 |
} |
|
95 |
||
96 |
return $values; |
|
97 |
} |
|
98 |
endif; |
|
19 | 99 |
|
100 |
if ( ! function_exists( 'wp_cache_delete_multiple' ) ) : |
|
101 |
/** |
|
102 |
* Deletes multiple values from the cache in one call. |
|
103 |
* |
|
104 |
* Compat function to mimic wp_cache_delete_multiple(). |
|
105 |
* |
|
106 |
* @ignore |
|
107 |
* @since 6.0.0 |
|
108 |
* |
|
109 |
* @see wp_cache_delete_multiple() |
|
110 |
* |
|
111 |
* @param array $keys Array of keys under which the cache to deleted. |
|
112 |
* @param string $group Optional. Where the cache contents are grouped. Default empty. |
|
113 |
* @return bool[] Array of return values, grouped by key. Each value is either |
|
114 |
* true on success, or false if the contents were not deleted. |
|
115 |
*/ |
|
116 |
function wp_cache_delete_multiple( array $keys, $group = '' ) { |
|
117 |
$values = array(); |
|
118 |
||
119 |
foreach ( $keys as $key ) { |
|
120 |
$values[ $key ] = wp_cache_delete( $key, $group ); |
|
121 |
} |
|
122 |
||
123 |
return $values; |
|
124 |
} |
|
125 |
endif; |
|
126 |
||
127 |
if ( ! function_exists( 'wp_cache_flush_runtime' ) ) : |
|
128 |
/** |
|
129 |
* Removes all cache items from the in-memory runtime cache. |
|
130 |
* |
|
131 |
* Compat function to mimic wp_cache_flush_runtime(). |
|
132 |
* |
|
133 |
* @ignore |
|
134 |
* @since 6.0.0 |
|
135 |
* |
|
136 |
* @see wp_cache_flush_runtime() |
|
137 |
* |
|
138 |
* @return bool True on success, false on failure. |
|
139 |
*/ |
|
140 |
function wp_cache_flush_runtime() { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
141 |
if ( ! wp_cache_supports( 'flush_runtime' ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
142 |
_doing_it_wrong( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
143 |
__FUNCTION__, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
144 |
__( 'Your object cache implementation does not support flushing the in-memory runtime cache.' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
145 |
'6.1.0' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
146 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
147 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
148 |
return false; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
149 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
150 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
151 |
return wp_cache_flush(); |
19 | 152 |
} |
153 |
endif; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
154 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
155 |
if ( ! function_exists( 'wp_cache_flush_group' ) ) : |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
156 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
157 |
* Removes all cache items in a group, if the object cache implementation supports it. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
158 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
159 |
* Before calling this function, always check for group flushing support using the |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
160 |
* `wp_cache_supports( 'flush_group' )` function. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
161 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
162 |
* @since 6.1.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
163 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
164 |
* @see WP_Object_Cache::flush_group() |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
165 |
* @global WP_Object_Cache $wp_object_cache Object cache global instance. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
166 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
167 |
* @param string $group Name of group to remove from cache. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
168 |
* @return bool True if group was flushed, false otherwise. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
169 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
170 |
function wp_cache_flush_group( $group ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
171 |
global $wp_object_cache; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
172 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
173 |
if ( ! wp_cache_supports( 'flush_group' ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
174 |
_doing_it_wrong( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
175 |
__FUNCTION__, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
176 |
__( 'Your object cache implementation does not support flushing individual groups.' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
177 |
'6.1.0' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
178 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
179 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
180 |
return false; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
181 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
182 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
183 |
return $wp_object_cache->flush_group( $group ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
184 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
185 |
endif; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
186 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
187 |
if ( ! function_exists( 'wp_cache_supports' ) ) : |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
188 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
189 |
* Determines whether the object cache implementation supports a particular feature. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
190 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
191 |
* @since 6.1.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
192 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
193 |
* @param string $feature Name of the feature to check for. Possible values include: |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
194 |
* 'add_multiple', 'set_multiple', 'get_multiple', 'delete_multiple', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
195 |
* 'flush_runtime', 'flush_group'. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
196 |
* @return bool True if the feature is supported, false otherwise. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
197 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
198 |
function wp_cache_supports( $feature ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
199 |
return false; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
200 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
201 |
endif; |