changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
6:490d5cc509ed | 7:cf61fcea0001 |
---|---|
1 <?php |
1 <?php |
2 /** |
2 /** |
3 * Object Cache API |
3 * Object Cache API |
4 * |
4 * |
5 * @link https://codex.wordpress.org/Function_Reference/WP_Cache |
5 * @link https://codex.wordpress.org/Class_Reference/WP_Object_Cache |
6 * |
6 * |
7 * @package WordPress |
7 * @package WordPress |
8 * @subpackage Cache |
8 * @subpackage Cache |
9 */ |
9 */ |
10 |
10 |
11 /** |
11 /** |
12 * Adds data to the cache, if the cache key doesn't already exist. |
12 * Adds data to the cache, if the cache key doesn't already exist. |
13 * |
13 * |
14 * @since 2.0.0 |
14 * @since 2.0.0 |
15 * @uses $wp_object_cache Object Cache Class |
15 * |
16 * @see WP_Object_Cache::add() |
16 * @see WP_Object_Cache::add() |
17 * |
17 * @global WP_Object_Cache $wp_object_cache Object cache global instance. |
18 * @param int|string $key The cache key to use for retrieval later |
18 * |
19 * @param mixed $data The data to add to the cache store |
19 * @param int|string $key The cache key to use for retrieval later. |
20 * @param string $group The group to add the cache to |
20 * @param mixed $data The data to add to the cache. |
21 * @param int $expire When the cache data should be expired |
21 * @param string $group Optional. The group to add the cache to. Enables the same key |
22 * @return bool False if cache key and group already exist, true on success |
22 * to be used across groups. Default empty. |
23 * @param int $expire Optional. When the cache data should expire, in seconds. |
|
24 * Default 0 (no expiration). |
|
25 * @return bool False if cache key and group already exist, true on success. |
|
23 */ |
26 */ |
24 function wp_cache_add( $key, $data, $group = '', $expire = 0 ) { |
27 function wp_cache_add( $key, $data, $group = '', $expire = 0 ) { |
25 global $wp_object_cache; |
28 global $wp_object_cache; |
26 |
29 |
27 return $wp_object_cache->add( $key, $data, $group, (int) $expire ); |
30 return $wp_object_cache->add( $key, $data, $group, (int) $expire ); |
29 |
32 |
30 /** |
33 /** |
31 * Closes the cache. |
34 * Closes the cache. |
32 * |
35 * |
33 * This function has ceased to do anything since WordPress 2.5. The |
36 * This function has ceased to do anything since WordPress 2.5. The |
34 * functionality was removed along with the rest of the persistent cache. This |
37 * functionality was removed along with the rest of the persistent cache. |
35 * does not mean that plugins can't implement this function when they need to |
38 * |
36 * make sure that the cache is cleaned up after WordPress no longer needs it. |
39 * This does not mean that plugins can't implement this function when they need |
37 * |
40 * to make sure that the cache is cleaned up after WordPress no longer needs it. |
38 * @since 2.0.0 |
41 * |
39 * |
42 * @since 2.0.0 |
40 * @return bool Always returns True |
43 * |
44 * @return true Always returns true. |
|
41 */ |
45 */ |
42 function wp_cache_close() { |
46 function wp_cache_close() { |
43 return true; |
47 return true; |
44 } |
48 } |
45 |
49 |
46 /** |
50 /** |
47 * Decrement numeric cache item's value |
51 * Decrements numeric cache item's value. |
48 * |
52 * |
49 * @since 3.3.0 |
53 * @since 3.3.0 |
50 * @uses $wp_object_cache Object Cache Class |
54 * |
51 * @see WP_Object_Cache::decr() |
55 * @see WP_Object_Cache::decr() |
52 * |
56 * @global WP_Object_Cache $wp_object_cache Object cache global instance. |
53 * @param int|string $key The cache key to increment |
57 * |
54 * @param int $offset The amount by which to decrement the item's value. Default is 1. |
58 * @param int|string $key The cache key to decrement. |
55 * @param string $group The group the key is in. |
59 * @param int $offset Optional. The amount by which to decrement the item's value. Default 1. |
60 * @param string $group Optional. The group the key is in. Default empty. |
|
56 * @return false|int False on failure, the item's new value on success. |
61 * @return false|int False on failure, the item's new value on success. |
57 */ |
62 */ |
58 function wp_cache_decr( $key, $offset = 1, $group = '' ) { |
63 function wp_cache_decr( $key, $offset = 1, $group = '' ) { |
59 global $wp_object_cache; |
64 global $wp_object_cache; |
60 |
65 |
63 |
68 |
64 /** |
69 /** |
65 * Removes the cache contents matching key and group. |
70 * Removes the cache contents matching key and group. |
66 * |
71 * |
67 * @since 2.0.0 |
72 * @since 2.0.0 |
68 * @uses $wp_object_cache Object Cache Class |
73 * |
69 * @see WP_Object_Cache::delete() |
74 * @see WP_Object_Cache::delete() |
70 * |
75 * @global WP_Object_Cache $wp_object_cache Object cache global instance. |
71 * @param int|string $key What the contents in the cache are called |
76 * |
72 * @param string $group Where the cache contents are grouped |
77 * @param int|string $key What the contents in the cache are called. |
73 * @return bool True on successful removal, false on failure |
78 * @param string $group Optional. Where the cache contents are grouped. Default empty. |
74 */ |
79 * @return bool True on successful removal, false on failure. |
75 function wp_cache_delete($key, $group = '') { |
80 */ |
81 function wp_cache_delete( $key, $group = '' ) { |
|
76 global $wp_object_cache; |
82 global $wp_object_cache; |
77 |
83 |
78 return $wp_object_cache->delete($key, $group); |
84 return $wp_object_cache->delete($key, $group); |
79 } |
85 } |
80 |
86 |
81 /** |
87 /** |
82 * Removes all cache items. |
88 * Removes all cache items. |
83 * |
89 * |
84 * @since 2.0.0 |
90 * @since 2.0.0 |
85 * @uses $wp_object_cache Object Cache Class |
91 * |
86 * @see WP_Object_Cache::flush() |
92 * @see WP_Object_Cache::flush() |
93 * @global WP_Object_Cache $wp_object_cache Object cache global instance. |
|
87 * |
94 * |
88 * @return bool False on failure, true on success |
95 * @return bool False on failure, true on success |
89 */ |
96 */ |
90 function wp_cache_flush() { |
97 function wp_cache_flush() { |
91 global $wp_object_cache; |
98 global $wp_object_cache; |
95 |
102 |
96 /** |
103 /** |
97 * Retrieves the cache contents from the cache by key and group. |
104 * Retrieves the cache contents from the cache by key and group. |
98 * |
105 * |
99 * @since 2.0.0 |
106 * @since 2.0.0 |
100 * @uses $wp_object_cache Object Cache Class |
107 * |
101 * @see WP_Object_Cache::get() |
108 * @see WP_Object_Cache::get() |
102 * |
109 * @global WP_Object_Cache $wp_object_cache Object cache global instance. |
103 * @param int|string $key What the contents in the cache are called |
110 * |
104 * @param string $group Where the cache contents are grouped |
111 * @param int|string $key The key under which the cache contents are stored. |
105 * @param bool $force Whether to force an update of the local cache from the persistent cache (default is false) |
112 * @param string $group Optional. Where the cache contents are grouped. Default empty. |
106 * @param bool &$found Whether key was found in the cache. Disambiguates a return of false, a storable value. |
113 * @param bool $force Optional. Whether to force an update of the local cache from the persistent |
114 * cache. Default false. |
|
115 * @param bool $found Optional. Whether the key was found in the cache (passed by reference). |
|
116 * Disambiguates a return of false, a storable value. Default null. |
|
107 * @return bool|mixed False on failure to retrieve contents or the cache |
117 * @return bool|mixed False on failure to retrieve contents or the cache |
108 * contents on success |
118 * contents on success |
109 */ |
119 */ |
110 function wp_cache_get( $key, $group = '', $force = false, &$found = null ) { |
120 function wp_cache_get( $key, $group = '', $force = false, &$found = null ) { |
111 global $wp_object_cache; |
121 global $wp_object_cache; |
112 |
122 |
113 return $wp_object_cache->get( $key, $group, $force, $found ); |
123 return $wp_object_cache->get( $key, $group, $force, $found ); |
115 |
125 |
116 /** |
126 /** |
117 * Increment numeric cache item's value |
127 * Increment numeric cache item's value |
118 * |
128 * |
119 * @since 3.3.0 |
129 * @since 3.3.0 |
120 * @uses $wp_object_cache Object Cache Class |
130 * |
121 * @see WP_Object_Cache::incr() |
131 * @see WP_Object_Cache::incr() |
122 * |
132 * @global WP_Object_Cache $wp_object_cache Object cache global instance. |
123 * @param int|string $key The cache key to increment |
133 * |
124 * @param int $offset The amount by which to increment the item's value. Default is 1. |
134 * @param int|string $key The key for the cache contents that should be incremented. |
125 * @param string $group The group the key is in. |
135 * @param int $offset Optional. The amount by which to increment the item's value. Default 1. |
136 * @param string $group Optional. The group the key is in. Default empty. |
|
126 * @return false|int False on failure, the item's new value on success. |
137 * @return false|int False on failure, the item's new value on success. |
127 */ |
138 */ |
128 function wp_cache_incr( $key, $offset = 1, $group = '' ) { |
139 function wp_cache_incr( $key, $offset = 1, $group = '' ) { |
129 global $wp_object_cache; |
140 global $wp_object_cache; |
130 |
141 |
133 |
144 |
134 /** |
145 /** |
135 * Sets up Object Cache Global and assigns it. |
146 * Sets up Object Cache Global and assigns it. |
136 * |
147 * |
137 * @since 2.0.0 |
148 * @since 2.0.0 |
138 * @global WP_Object_Cache $wp_object_cache WordPress Object Cache |
149 * |
150 * @global WP_Object_Cache $wp_object_cache |
|
139 */ |
151 */ |
140 function wp_cache_init() { |
152 function wp_cache_init() { |
141 $GLOBALS['wp_object_cache'] = new WP_Object_Cache(); |
153 $GLOBALS['wp_object_cache'] = new WP_Object_Cache(); |
142 } |
154 } |
143 |
155 |
144 /** |
156 /** |
145 * Replaces the contents of the cache with new data. |
157 * Replaces the contents of the cache with new data. |
146 * |
158 * |
147 * @since 2.0.0 |
159 * @since 2.0.0 |
148 * @uses $wp_object_cache Object Cache Class |
160 * |
149 * @see WP_Object_Cache::replace() |
161 * @see WP_Object_Cache::replace() |
150 * |
162 * @global WP_Object_Cache $wp_object_cache Object cache global instance. |
151 * @param int|string $key What to call the contents in the cache |
163 * |
152 * @param mixed $data The contents to store in the cache |
164 * @param int|string $key The key for the cache data that should be replaced. |
153 * @param string $group Where to group the cache contents |
165 * @param mixed $data The new data to store in the cache. |
154 * @param int $expire When to expire the cache contents |
166 * @param string $group Optional. The group for the cache data that should be replaced. |
155 * @return bool False if not exists, true if contents were replaced |
167 * Default empty. |
168 * @param int $expire Optional. When to expire the cache contents, in seconds. |
|
169 * Default 0 (no expiration). |
|
170 * @return bool False if original value does not exist, true if contents were replaced |
|
156 */ |
171 */ |
157 function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) { |
172 function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) { |
158 global $wp_object_cache; |
173 global $wp_object_cache; |
159 |
174 |
160 return $wp_object_cache->replace( $key, $data, $group, (int) $expire ); |
175 return $wp_object_cache->replace( $key, $data, $group, (int) $expire ); |
161 } |
176 } |
162 |
177 |
163 /** |
178 /** |
164 * Saves the data to the cache. |
179 * Saves the data to the cache. |
165 * |
180 * |
166 * @since 2.0.0 |
181 * Differs from wp_cache_add() and wp_cache_replace() in that it will always write data. |
167 * |
182 * |
168 * @uses $wp_object_cache Object Cache Class |
183 * @since 2.0.0 |
184 * |
|
169 * @see WP_Object_Cache::set() |
185 * @see WP_Object_Cache::set() |
170 * |
186 * @global WP_Object_Cache $wp_object_cache Object cache global instance. |
171 * @param int|string $key What to call the contents in the cache |
187 * |
172 * @param mixed $data The contents to store in the cache |
188 * @param int|string $key The cache key to use for retrieval later. |
173 * @param string $group Where to group the cache contents |
189 * @param mixed $data The contents to store in the cache. |
174 * @param int $expire When to expire the cache contents |
190 * @param string $group Optional. Where to group the cache contents. Enables the same key |
191 * to be used across groups. Default empty. |
|
192 * @param int $expire Optional. When to expire the cache contents, in seconds. |
|
193 * Default 0 (no expiration). |
|
175 * @return bool False on failure, true on success |
194 * @return bool False on failure, true on success |
176 */ |
195 */ |
177 function wp_cache_set( $key, $data, $group = '', $expire = 0 ) { |
196 function wp_cache_set( $key, $data, $group = '', $expire = 0 ) { |
178 global $wp_object_cache; |
197 global $wp_object_cache; |
179 |
198 |
180 return $wp_object_cache->set( $key, $data, $group, (int) $expire ); |
199 return $wp_object_cache->set( $key, $data, $group, (int) $expire ); |
181 } |
200 } |
182 |
201 |
183 /** |
202 /** |
184 * Switch the interal blog id. |
203 * Switches the internal blog ID. |
185 * |
204 * |
186 * This changes the blog id used to create keys in blog specific groups. |
205 * This changes the blog id used to create keys in blog specific groups. |
187 * |
206 * |
188 * @since 3.5.0 |
207 * @since 3.5.0 |
189 * |
208 * |
190 * @param int $blog_id Blog ID |
209 * @see WP_Object_Cache::switch_to_blog() |
210 * @global WP_Object_Cache $wp_object_cache Object cache global instance. |
|
211 * |
|
212 * @param int $blog_id Site ID. |
|
191 */ |
213 */ |
192 function wp_cache_switch_to_blog( $blog_id ) { |
214 function wp_cache_switch_to_blog( $blog_id ) { |
193 global $wp_object_cache; |
215 global $wp_object_cache; |
194 |
216 |
195 return $wp_object_cache->switch_to_blog( $blog_id ); |
217 $wp_object_cache->switch_to_blog( $blog_id ); |
196 } |
218 } |
197 |
219 |
198 /** |
220 /** |
199 * Adds a group or set of groups to the list of global groups. |
221 * Adds a group or set of groups to the list of global groups. |
200 * |
222 * |
201 * @since 2.6.0 |
223 * @since 2.6.0 |
202 * |
224 * |
203 * @param string|array $groups A group or an array of groups to add |
225 * @see WP_Object_Cache::add_global_groups() |
226 * @global WP_Object_Cache $wp_object_cache Object cache global instance. |
|
227 * |
|
228 * @param string|array $groups A group or an array of groups to add. |
|
204 */ |
229 */ |
205 function wp_cache_add_global_groups( $groups ) { |
230 function wp_cache_add_global_groups( $groups ) { |
206 global $wp_object_cache; |
231 global $wp_object_cache; |
207 |
232 |
208 return $wp_object_cache->add_global_groups( $groups ); |
233 $wp_object_cache->add_global_groups( $groups ); |
209 } |
234 } |
210 |
235 |
211 /** |
236 /** |
212 * Adds a group or set of groups to the list of non-persistent groups. |
237 * Adds a group or set of groups to the list of non-persistent groups. |
213 * |
238 * |
214 * @since 2.6.0 |
239 * @since 2.6.0 |
215 * |
240 * |
216 * @param string|array $groups A group or an array of groups to add |
241 * @param string|array $groups A group or an array of groups to add. |
217 */ |
242 */ |
218 function wp_cache_add_non_persistent_groups( $groups ) { |
243 function wp_cache_add_non_persistent_groups( $groups ) { |
219 // Default cache doesn't persist so nothing to do here. |
244 // Default cache doesn't persist so nothing to do here. |
220 } |
245 } |
221 |
246 |
222 /** |
247 /** |
223 * Reset internal cache keys and structures. If the cache backend uses global |
248 * Reset internal cache keys and structures. |
224 * blog or site IDs as part of its cache keys, this function instructs the |
249 * |
225 * backend to reset those keys and perform any cleanup since blog or site IDs |
250 * If the cache back end uses global blog or site IDs as part of its cache keys, |
226 * have changed since cache init. |
251 * this function instructs the back end to reset those keys and perform any cleanup |
252 * since blog or site IDs have changed since cache init. |
|
227 * |
253 * |
228 * This function is deprecated. Use wp_cache_switch_to_blog() instead of this |
254 * This function is deprecated. Use wp_cache_switch_to_blog() instead of this |
229 * function when preparing the cache for a blog switch. For clearing the cache |
255 * function when preparing the cache for a blog switch. For clearing the cache |
230 * during unit tests, consider using wp_cache_init(). wp_cache_init() is not |
256 * during unit tests, consider using wp_cache_init(). wp_cache_init() is not |
231 * recommended outside of unit tests as the performance penality for using it is |
257 * recommended outside of unit tests as the performance penalty for using it is |
232 * high. |
258 * high. |
233 * |
259 * |
234 * @since 2.6.0 |
260 * @since 2.6.0 |
235 * @deprecated 3.5.0 |
261 * @deprecated 3.5.0 WP_Object_Cache::reset() |
262 * @see WP_Object_Cache::reset() |
|
263 * |
|
264 * @global WP_Object_Cache $wp_object_cache Object cache global instance. |
|
236 */ |
265 */ |
237 function wp_cache_reset() { |
266 function wp_cache_reset() { |
238 _deprecated_function( __FUNCTION__, '3.5' ); |
267 _deprecated_function( __FUNCTION__, '3.5.0', 'WP_Object_Cache::reset()' ); |
239 |
268 |
240 global $wp_object_cache; |
269 global $wp_object_cache; |
241 |
270 |
242 return $wp_object_cache->reset(); |
271 $wp_object_cache->reset(); |
243 } |
272 } |
244 |
273 |
245 /** |
274 /** |
246 * WordPress Object Cache |
275 * Core class that implements an object cache. |
247 * |
276 * |
248 * The WordPress Object Cache is used to save on trips to the database. The |
277 * The WordPress Object Cache is used to save on trips to the database. The |
249 * Object Cache stores all of the cache data to memory and makes the cache |
278 * Object Cache stores all of the cache data to memory and makes the cache |
250 * contents available by using a key, which is used to name and later retrieve |
279 * contents available by using a key, which is used to name and later retrieve |
251 * the cache contents. |
280 * the cache contents. |
252 * |
281 * |
253 * The Object Cache can be replaced by other caching mechanisms by placing files |
282 * The Object Cache can be replaced by other caching mechanisms by placing files |
254 * in the wp-content folder which is looked at in wp-settings. If that file |
283 * in the wp-content folder which is looked at in wp-settings. If that file |
255 * exists, then this file will not be included. |
284 * exists, then this file will not be included. |
256 * |
285 * |
257 * @package WordPress |
|
258 * @subpackage Cache |
|
259 * @since 2.0.0 |
286 * @since 2.0.0 |
260 */ |
287 */ |
261 class WP_Object_Cache { |
288 class WP_Object_Cache { |
262 |
289 |
263 /** |
290 /** |
264 * Holds the cached objects |
291 * Holds the cached objects. |
265 * |
292 * |
293 * @since 2.0.0 |
|
266 * @var array |
294 * @var array |
267 * @access private |
|
268 * @since 2.0.0 |
|
269 */ |
295 */ |
270 private $cache = array(); |
296 private $cache = array(); |
271 |
297 |
272 /** |
298 /** |
273 * The amount of times the cache data was already stored in the cache. |
299 * The amount of times the cache data was already stored in the cache. |
274 * |
300 * |
275 * @since 2.5.0 |
301 * @since 2.5.0 |
276 * @access private |
|
277 * @var int |
302 * @var int |
278 */ |
303 */ |
279 private $cache_hits = 0; |
304 public $cache_hits = 0; |
280 |
305 |
281 /** |
306 /** |
282 * Amount of times the cache did not have the request in cache |
307 * Amount of times the cache did not have the request in cache. |
283 * |
308 * |
309 * @since 2.0.0 |
|
284 * @var int |
310 * @var int |
285 * @access public |
|
286 * @since 2.0.0 |
|
287 */ |
311 */ |
288 public $cache_misses = 0; |
312 public $cache_misses = 0; |
289 |
313 |
290 /** |
314 /** |
291 * List of global groups |
315 * List of global cache groups. |
292 * |
316 * |
317 * @since 3.0.0 |
|
293 * @var array |
318 * @var array |
294 * @access protected |
|
295 * @since 3.0.0 |
|
296 */ |
319 */ |
297 protected $global_groups = array(); |
320 protected $global_groups = array(); |
298 |
321 |
299 /** |
322 /** |
300 * The blog prefix to prepend to keys in non-global groups. |
323 * The blog prefix to prepend to keys in non-global groups. |
301 * |
324 * |
325 * @since 3.5.0 |
|
302 * @var int |
326 * @var int |
303 * @access private |
327 */ |
328 private $blog_prefix; |
|
329 |
|
330 /** |
|
331 * Holds the value of is_multisite(). |
|
332 * |
|
304 * @since 3.5.0 |
333 * @since 3.5.0 |
305 */ |
|
306 private $blog_prefix; |
|
307 |
|
308 /** |
|
309 * Holds the value of `is_multisite()` |
|
310 * |
|
311 * @var bool |
334 * @var bool |
312 * @access private |
|
313 * @since 3.5.0 |
|
314 */ |
335 */ |
315 private $multisite; |
336 private $multisite; |
316 |
337 |
317 /** |
338 /** |
318 * Make private properties readable for backwards compatibility. |
339 * Makes private properties readable for backward compatibility. |
319 * |
340 * |
320 * @since 4.0.0 |
341 * @since 4.0.0 |
321 * @access public |
|
322 * |
342 * |
323 * @param string $name Property to get. |
343 * @param string $name Property to get. |
324 * @return mixed Property. |
344 * @return mixed Property. |
325 */ |
345 */ |
326 public function __get( $name ) { |
346 public function __get( $name ) { |
327 return $this->$name; |
347 return $this->$name; |
328 } |
348 } |
329 |
349 |
330 /** |
350 /** |
331 * Make private properties settable for backwards compatibility. |
351 * Makes private properties settable for backward compatibility. |
332 * |
352 * |
333 * @since 4.0.0 |
353 * @since 4.0.0 |
334 * @access public |
|
335 * |
354 * |
336 * @param string $name Property to set. |
355 * @param string $name Property to set. |
337 * @param mixed $value Property value. |
356 * @param mixed $value Property value. |
338 * @return mixed Newly-set property. |
357 * @return mixed Newly-set property. |
339 */ |
358 */ |
340 public function __set( $name, $value ) { |
359 public function __set( $name, $value ) { |
341 return $this->$name = $value; |
360 return $this->$name = $value; |
342 } |
361 } |
343 |
362 |
344 /** |
363 /** |
345 * Make private properties checkable for backwards compatibility. |
364 * Makes private properties checkable for backward compatibility. |
346 * |
365 * |
347 * @since 4.0.0 |
366 * @since 4.0.0 |
348 * @access public |
|
349 * |
367 * |
350 * @param string $name Property to check if set. |
368 * @param string $name Property to check if set. |
351 * @return bool Whether the property is set. |
369 * @return bool Whether the property is set. |
352 */ |
370 */ |
353 public function __isset( $name ) { |
371 public function __isset( $name ) { |
354 return isset( $this->$name ); |
372 return isset( $this->$name ); |
355 } |
373 } |
356 |
374 |
357 /** |
375 /** |
358 * Make private properties un-settable for backwards compatibility. |
376 * Makes private properties un-settable for backward compatibility. |
359 * |
377 * |
360 * @since 4.0.0 |
378 * @since 4.0.0 |
361 * @access public |
|
362 * |
379 * |
363 * @param string $name Property to unset. |
380 * @param string $name Property to unset. |
364 */ |
381 */ |
365 public function __unset( $name ) { |
382 public function __unset( $name ) { |
366 unset( $this->$name ); |
383 unset( $this->$name ); |
367 } |
384 } |
368 |
385 |
369 /** |
386 /** |
370 * Adds data to the cache if it doesn't already exist. |
387 * Adds data to the cache if it doesn't already exist. |
371 * |
388 * |
372 * @uses WP_Object_Cache::_exists Checks to see if the cache already has data. |
389 * @since 2.0.0 |
373 * @uses WP_Object_Cache::set Sets the data after the checking the cache |
390 * |
374 * contents existence. |
391 * @uses WP_Object_Cache::_exists() Checks to see if the cache already has data. |
375 * |
392 * @uses WP_Object_Cache::set() Sets the data after the checking the cache |
376 * @since 2.0.0 |
393 * contents existence. |
377 * |
394 * |
378 * @param int|string $key What to call the contents in the cache |
395 * @param int|string $key What to call the contents in the cache. |
379 * @param mixed $data The contents to store in the cache |
396 * @param mixed $data The contents to store in the cache. |
380 * @param string $group Where to group the cache contents |
397 * @param string $group Optional. Where to group the cache contents. Default 'default'. |
381 * @param int $expire When to expire the cache contents |
398 * @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration). |
382 * @return bool False if cache key and group already exist, true on success |
399 * @return bool False if cache key and group already exist, true on success |
383 */ |
400 */ |
384 public function add( $key, $data, $group = 'default', $expire = 0 ) { |
401 public function add( $key, $data, $group = 'default', $expire = 0 ) { |
385 if ( wp_suspend_cache_addition() ) |
402 if ( wp_suspend_cache_addition() ) |
386 return false; |
403 return false; |
397 |
414 |
398 return $this->set( $key, $data, $group, (int) $expire ); |
415 return $this->set( $key, $data, $group, (int) $expire ); |
399 } |
416 } |
400 |
417 |
401 /** |
418 /** |
402 * Sets the list of global groups. |
419 * Sets the list of global cache groups. |
403 * |
420 * |
404 * @since 3.0.0 |
421 * @since 3.0.0 |
405 * |
422 * |
406 * @param array $groups List of groups that are global. |
423 * @param array $groups List of groups that are global. |
407 */ |
424 */ |
411 $groups = array_fill_keys( $groups, true ); |
428 $groups = array_fill_keys( $groups, true ); |
412 $this->global_groups = array_merge( $this->global_groups, $groups ); |
429 $this->global_groups = array_merge( $this->global_groups, $groups ); |
413 } |
430 } |
414 |
431 |
415 /** |
432 /** |
416 * Decrement numeric cache item's value |
433 * Decrements numeric cache item's value. |
417 * |
434 * |
418 * @since 3.3.0 |
435 * @since 3.3.0 |
419 * |
436 * |
420 * @param int|string $key The cache key to increment |
437 * @param int|string $key The cache key to decrement. |
421 * @param int $offset The amount by which to decrement the item's value. Default is 1. |
438 * @param int $offset Optional. The amount by which to decrement the item's value. Default 1. |
422 * @param string $group The group the key is in. |
439 * @param string $group Optional. The group the key is in. Default 'default'. |
423 * @return false|int False on failure, the item's new value on success. |
440 * @return false|int False on failure, the item's new value on success. |
424 */ |
441 */ |
425 public function decr( $key, $offset = 1, $group = 'default' ) { |
442 public function decr( $key, $offset = 1, $group = 'default' ) { |
426 if ( empty( $group ) ) |
443 if ( empty( $group ) ) |
427 $group = 'default'; |
444 $group = 'default'; |
444 |
461 |
445 return $this->cache[ $group ][ $key ]; |
462 return $this->cache[ $group ][ $key ]; |
446 } |
463 } |
447 |
464 |
448 /** |
465 /** |
449 * Remove the contents of the cache key in the group |
466 * Removes the contents of the cache key in the group. |
450 * |
467 * |
451 * If the cache key does not exist in the group, then nothing will happen. |
468 * If the cache key does not exist in the group, then nothing will happen. |
452 * |
469 * |
453 * @since 2.0.0 |
470 * @since 2.0.0 |
454 * |
471 * |
455 * @param int|string $key What the contents in the cache are called |
472 * @param int|string $key What the contents in the cache are called. |
456 * @param string $group Where the cache contents are grouped |
473 * @param string $group Optional. Where the cache contents are grouped. Default 'default'. |
457 * @param bool $deprecated Deprecated. |
474 * @param bool $deprecated Optional. Unused. Default false. |
458 * |
475 * @return bool False if the contents weren't deleted and true on success. |
459 * @return bool False if the contents weren't deleted and true on success |
|
460 */ |
476 */ |
461 public function delete( $key, $group = 'default', $deprecated = false ) { |
477 public function delete( $key, $group = 'default', $deprecated = false ) { |
462 if ( empty( $group ) ) |
478 if ( empty( $group ) ) |
463 $group = 'default'; |
479 $group = 'default'; |
464 |
480 |
471 unset( $this->cache[$group][$key] ); |
487 unset( $this->cache[$group][$key] ); |
472 return true; |
488 return true; |
473 } |
489 } |
474 |
490 |
475 /** |
491 /** |
476 * Clears the object cache of all data |
492 * Clears the object cache of all data. |
477 * |
493 * |
478 * @since 2.0.0 |
494 * @since 2.0.0 |
479 * |
495 * |
480 * @return bool Always returns true |
496 * @return true Always returns true. |
481 */ |
497 */ |
482 public function flush() { |
498 public function flush() { |
483 $this->cache = array (); |
499 $this->cache = array(); |
484 |
500 |
485 return true; |
501 return true; |
486 } |
502 } |
487 |
503 |
488 /** |
504 /** |
489 * Retrieves the cache contents, if it exists |
505 * Retrieves the cache contents, if it exists. |
490 * |
506 * |
491 * The contents will be first attempted to be retrieved by searching by the |
507 * The contents will be first attempted to be retrieved by searching by the |
492 * key in the cache group. If the cache is hit (success) then the contents |
508 * key in the cache group. If the cache is hit (success) then the contents |
493 * are returned. |
509 * are returned. |
494 * |
510 * |
495 * On failure, the number of cache misses will be incremented. |
511 * On failure, the number of cache misses will be incremented. |
496 * |
512 * |
497 * @since 2.0.0 |
513 * @since 2.0.0 |
498 * |
514 * |
499 * @param int|string $key What the contents in the cache are called |
515 * @param int|string $key What the contents in the cache are called. |
500 * @param string $group Where the cache contents are grouped |
516 * @param string $group Optional. Where the cache contents are grouped. Default 'default'. |
501 * @param string $force Whether to force a refetch rather than relying on the local cache (default is false) |
517 * @param string $force Optional. Unused. Whether to force a refetch rather than relying on the local |
502 * @return bool|mixed False on failure to retrieve contents or the cache |
518 * cache. Default false. |
503 * contents on success |
519 * @param bool $found Optional. Whether the key was found in the cache (passed by reference). |
520 * Disambiguates a return of false, a storable value. Default null. |
|
521 * @return false|mixed False on failure to retrieve contents or the cache contents on success. |
|
504 */ |
522 */ |
505 public function get( $key, $group = 'default', $force = false, &$found = null ) { |
523 public function get( $key, $group = 'default', $force = false, &$found = null ) { |
506 if ( empty( $group ) ) |
524 if ( empty( $group ) ) |
507 $group = 'default'; |
525 $group = 'default'; |
508 |
526 |
522 $this->cache_misses += 1; |
540 $this->cache_misses += 1; |
523 return false; |
541 return false; |
524 } |
542 } |
525 |
543 |
526 /** |
544 /** |
527 * Increment numeric cache item's value |
545 * Increments numeric cache item's value. |
528 * |
546 * |
529 * @since 3.3.0 |
547 * @since 3.3.0 |
530 * |
548 * |
531 * @param int|string $key The cache key to increment |
549 * @param int|string $key The cache key to increment |
532 * @param int $offset The amount by which to increment the item's value. Default is 1. |
550 * @param int $offset Optional. The amount by which to increment the item's value. Default 1. |
533 * @param string $group The group the key is in. |
551 * @param string $group Optional. The group the key is in. Default 'default'. |
534 * @return false|int False on failure, the item's new value on success. |
552 * @return false|int False on failure, the item's new value on success. |
535 */ |
553 */ |
536 public function incr( $key, $offset = 1, $group = 'default' ) { |
554 public function incr( $key, $offset = 1, $group = 'default' ) { |
537 if ( empty( $group ) ) |
555 if ( empty( $group ) ) |
538 $group = 'default'; |
556 $group = 'default'; |
555 |
573 |
556 return $this->cache[ $group ][ $key ]; |
574 return $this->cache[ $group ][ $key ]; |
557 } |
575 } |
558 |
576 |
559 /** |
577 /** |
560 * Replace the contents in the cache, if contents already exist |
578 * Replaces the contents in the cache, if contents already exist. |
561 * |
579 * |
562 * @since 2.0.0 |
580 * @since 2.0.0 |
581 * |
|
563 * @see WP_Object_Cache::set() |
582 * @see WP_Object_Cache::set() |
564 * |
583 * |
565 * @param int|string $key What to call the contents in the cache |
584 * @param int|string $key What to call the contents in the cache. |
566 * @param mixed $data The contents to store in the cache |
585 * @param mixed $data The contents to store in the cache. |
567 * @param string $group Where to group the cache contents |
586 * @param string $group Optional. Where to group the cache contents. Default 'default'. |
568 * @param int $expire When to expire the cache contents |
587 * @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration). |
569 * @return bool False if not exists, true if contents were replaced |
588 * @return bool False if not exists, true if contents were replaced. |
570 */ |
589 */ |
571 public function replace( $key, $data, $group = 'default', $expire = 0 ) { |
590 public function replace( $key, $data, $group = 'default', $expire = 0 ) { |
572 if ( empty( $group ) ) |
591 if ( empty( $group ) ) |
573 $group = 'default'; |
592 $group = 'default'; |
574 |
593 |
581 |
600 |
582 return $this->set( $key, $data, $group, (int) $expire ); |
601 return $this->set( $key, $data, $group, (int) $expire ); |
583 } |
602 } |
584 |
603 |
585 /** |
604 /** |
586 * Reset keys |
605 * Resets cache keys. |
587 * |
606 * |
588 * @since 3.0.0 |
607 * @since 3.0.0 |
589 * @deprecated 3.5.0 |
608 * |
609 * @deprecated 3.5.0 Use switch_to_blog() |
|
610 * @see switch_to_blog() |
|
590 */ |
611 */ |
591 public function reset() { |
612 public function reset() { |
592 _deprecated_function( __FUNCTION__, '3.5', 'switch_to_blog()' ); |
613 _deprecated_function( __FUNCTION__, '3.5.0', 'switch_to_blog()' ); |
593 |
614 |
594 // Clear out non-global caches since the blog ID has changed. |
615 // Clear out non-global caches since the blog ID has changed. |
595 foreach ( array_keys( $this->cache ) as $group ) { |
616 foreach ( array_keys( $this->cache ) as $group ) { |
596 if ( ! isset( $this->global_groups[ $group ] ) ) |
617 if ( ! isset( $this->global_groups[ $group ] ) ) |
597 unset( $this->cache[ $group ] ); |
618 unset( $this->cache[ $group ] ); |
598 } |
619 } |
599 } |
620 } |
600 |
621 |
601 /** |
622 /** |
602 * Sets the data contents into the cache |
623 * Sets the data contents into the cache. |
603 * |
624 * |
604 * The cache contents is grouped by the $group parameter followed by the |
625 * The cache contents is grouped by the $group parameter followed by the |
605 * $key. This allows for duplicate ids in unique groups. Therefore, naming of |
626 * $key. This allows for duplicate ids in unique groups. Therefore, naming of |
606 * the group should be used with care and should follow normal function |
627 * the group should be used with care and should follow normal function |
607 * naming guidelines outside of core WordPress usage. |
628 * naming guidelines outside of core WordPress usage. |
610 * expire for each time a page is accessed and PHP finishes. The method is |
631 * expire for each time a page is accessed and PHP finishes. The method is |
611 * more for cache plugins which use files. |
632 * more for cache plugins which use files. |
612 * |
633 * |
613 * @since 2.0.0 |
634 * @since 2.0.0 |
614 * |
635 * |
615 * @param int|string $key What to call the contents in the cache |
636 * @param int|string $key What to call the contents in the cache. |
616 * @param mixed $data The contents to store in the cache |
637 * @param mixed $data The contents to store in the cache. |
617 * @param string $group Where to group the cache contents |
638 * @param string $group Optional. Where to group the cache contents. Default 'default'. |
618 * @param int $expire Not Used |
639 * @param int $expire Not Used. |
619 * @return bool Always returns true |
640 * @return true Always returns true. |
620 */ |
641 */ |
621 public function set( $key, $data, $group = 'default', $expire = 0 ) { |
642 public function set( $key, $data, $group = 'default', $expire = 0 ) { |
622 if ( empty( $group ) ) |
643 if ( empty( $group ) ) |
623 $group = 'default'; |
644 $group = 'default'; |
624 |
645 |
645 echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />"; |
666 echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />"; |
646 echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />"; |
667 echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />"; |
647 echo "</p>"; |
668 echo "</p>"; |
648 echo '<ul>'; |
669 echo '<ul>'; |
649 foreach ($this->cache as $group => $cache) { |
670 foreach ($this->cache as $group => $cache) { |
650 echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / 1024, 2 ) . 'k )</li>'; |
671 echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>'; |
651 } |
672 } |
652 echo '</ul>'; |
673 echo '</ul>'; |
653 } |
674 } |
654 |
675 |
655 /** |
676 /** |
656 * Switch the interal blog id. |
677 * Switches the internal blog ID. |
657 * |
678 * |
658 * This changes the blog id used to create keys in blog specific groups. |
679 * This changes the blog ID used to create keys in blog specific groups. |
659 * |
680 * |
660 * @since 3.5.0 |
681 * @since 3.5.0 |
661 * |
682 * |
662 * @param int $blog_id Blog ID |
683 * @param int $blog_id Blog ID. |
663 */ |
684 */ |
664 public function switch_to_blog( $blog_id ) { |
685 public function switch_to_blog( $blog_id ) { |
665 $blog_id = (int) $blog_id; |
686 $blog_id = (int) $blog_id; |
666 $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; |
687 $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; |
667 } |
688 } |
668 |
689 |
669 /** |
690 /** |
670 * Utility function to determine whether a key exists in the cache. |
691 * Serves as a utility function to determine whether a key exists in the cache. |
671 * |
692 * |
672 * @since 3.4.0 |
693 * @since 3.4.0 |
673 * |
694 * |
674 * @access protected |
695 * @param int|string $key Cache key to check for existence. |
675 * @param string $key |
696 * @param string $group Cache group for the key existence check. |
676 * @param string $group |
697 * @return bool Whether the key exists in the cache for the given group. |
677 * @return bool |
|
678 */ |
698 */ |
679 protected function _exists( $key, $group ) { |
699 protected function _exists( $key, $group ) { |
680 return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) ); |
700 return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) ); |
681 } |
701 } |
682 |
702 |
683 /** |
703 /** |
684 * Sets up object properties; PHP 5 style constructor |
704 * Sets up object properties; PHP 5 style constructor. |
685 * |
705 * |
686 * @since 2.0.8 |
706 * @since 2.0.8 |
687 */ |
707 */ |
688 public function __construct() { |
708 public function __construct() { |
689 global $blog_id; |
|
690 |
|
691 $this->multisite = is_multisite(); |
709 $this->multisite = is_multisite(); |
692 $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; |
710 $this->blog_prefix = $this->multisite ? get_current_blog_id() . ':' : ''; |
693 |
711 |
694 |
712 |
695 /** |
713 /** |
696 * @todo This should be moved to the PHP4 style constructor, PHP5 |
714 * @todo This should be moved to the PHP4 style constructor, PHP5 |
697 * already calls __destruct() |
715 * already calls __destruct() |
698 */ |
716 */ |
699 register_shutdown_function( array( $this, '__destruct' ) ); |
717 register_shutdown_function( array( $this, '__destruct' ) ); |
700 } |
718 } |
701 |
719 |
702 /** |
720 /** |
703 * Will save the object cache before object is completely destroyed. |
721 * Saves the object cache before object is completely destroyed. |
704 * |
722 * |
705 * Called upon object destruction, which should be when PHP ends. |
723 * Called upon object destruction, which should be when PHP ends. |
706 * |
724 * |
707 * @since 2.0.8 |
725 * @since 2.0.8 |
708 * |
726 * |
709 * @return bool True value. Won't be used by PHP |
727 * @return true Always returns true. |
710 */ |
728 */ |
711 public function __destruct() { |
729 public function __destruct() { |
712 return true; |
730 return true; |
713 } |
731 } |
714 } |
732 } |