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