PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 1.2.6
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v1.2.6
trunk 1.0 1.0.1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1 2.1.1 2.1.2 2.2 2.2.1 All 69 releases
powered-cache / includes / dropins / apcu-object-cache.php

apcu-object-cache.php in Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score 1.2.6, at includes/dropins/apcu-object-cache.php

362 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * APCu drop-in, based on https://wordpress.org/plugins/apcu/
5 */
6
7 if ( ! function_exists( 'apcu_add' ) ) {
8 return;
9 }
10
11 if ( ! defined( 'WP_CACHE_KEY_SALT' ) ) {
12 define( 'WP_CACHE_KEY_SALT', DB_NAME );
13 }
14
15
16 function wp_cache_add( $key, $data, $group = '', $expire = 0 ) {
17 global $wp_object_cache;
18
19 return $wp_object_cache->add( $key, $data, $group, (int) $expire );
20 }
21
22 function wp_cache_close() {
23 return true;
24 }
25
26 function wp_cache_decr( $key, $offset = 1, $group = '' ) {
27 global $wp_object_cache;
28
29 return $wp_object_cache->decr( $key, $offset, $group );
30 }
31
32 function wp_cache_delete( $key, $group = '' ) {
33 global $wp_object_cache;
34
35 return $wp_object_cache->delete( $key, $group );
36 }
37
38 function wp_cache_flush() {
39 global $wp_object_cache;
40
41 return $wp_object_cache->flush();
42 }
43
44 function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
45 global $wp_object_cache;
46
47 return $wp_object_cache->get( $key, $group, $force, $found );
48 }
49
50 function wp_cache_incr( $key, $offset = 1, $group = '' ) {
51 global $wp_object_cache;
52
53 return $wp_object_cache->incr( $key, $offset, $group );
54 }
55
56 function wp_cache_init() {
57 $GLOBALS['wp_object_cache'] = new APCu_Object_Cache();
58 }
59
60 function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
61 global $wp_object_cache;
62
63 return $wp_object_cache->replace( $key, $data, $group, (int) $expire );
64 }
65
66 function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
67 global $wp_object_cache;
68
69 return $wp_object_cache->set( $key, $data, $group, (int) $expire );
70 }
71
72 function wp_cache_switch_to_blog( $blog_id ) {
73 global $wp_object_cache;
74
75 $wp_object_cache->switch_to_blog( $blog_id );
76 }
77
78 function wp_cache_add_global_groups( $groups ) {
79 global $wp_object_cache;
80
81 $wp_object_cache->add_global_groups( $groups );
82 }
83
84 function wp_cache_add_non_persistent_groups( $groups ) {
85 global $wp_object_cache;
86
87 $wp_object_cache->wp_cache_add_non_persistent_groups( $groups );
88 }
89
90 function wp_cache_reset() {
91 global $wp_object_cache;
92
93 $wp_object_cache->reset();
94 }
95
96
97 class APCu_Object_Cache {
98
99 private $prefix = '';
100 private $local_cache = array();
101 private $global_groups = array();
102 private $non_persistent_groups = array();
103 private $multisite = false;
104 private $blog_prefix = '';
105
106
107 /**
108 * The amount of times the cache data was already stored in the cache.
109 *
110 * @access public
111 * @var int
112 */
113 public $cache_hits = 0;
114
115 /**
116 * Amount of times the cache did not have the request in cache
117 *
118 * @access public
119 * @var int
120 */
121 public $cache_misses = 0;
122
123 public function __construct() {
124 global $table_prefix;
125
126 $this->multisite = is_multisite();
127 $this->blog_prefix = $this->multisite ? get_current_blog_id() . ':' : '';
128 $this->prefix = DB_HOST . '.' . DB_NAME . '.' . $table_prefix;
129 }
130
131 private function get_group( $group ) {
132 return empty( $group ) ? 'default' : $group;
133 }
134
135 private function get_key( $group, $key ) {
136
137 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
138 $prefix = $this->prefix . '.' . $group . '.' . $this->blog_prefix . ':' . $key;
139 } else {
140 $prefix = $this->prefix . '.' . $group . '.' . $key;
141 }
142
143 return preg_replace( '/\s+/', '', WP_CACHE_KEY_SALT . "$prefix" );
144 }
145
146 public function add( $key, $data, $group = 'default', $expire = 0 ) {
147 $group = $this->get_group( $group );
148 $key = $this->get_key( $group, $key );
149
150 if ( function_exists( 'wp_suspend_cache_addition' ) && wp_suspend_cache_addition() ) {
151 return false;
152 }
153 if ( isset( $this->local_cache[ $group ][ $key ] ) ) {
154 return false;
155 }
156 // FIXME: Somehow apcu_add does not return false if key already exists
157 if ( ! isset( $this->non_persistent_groups[ $group ] ) && apcu_exists( $key ) ) {
158 return false;
159 }
160
161 if ( is_object( $data ) ) {
162 $this->local_cache[ $group ][ $key ] = clone $data;
163 } else {
164 $this->local_cache[ $group ][ $key ] = $data;
165 }
166
167 if ( ! isset( $this->non_persistent_groups[ $group ] ) ) {
168 return apcu_add( $key, $data, (int) $expire );
169 }
170
171 return true;
172 }
173
174 public function add_global_groups( $groups ) {
175 if ( is_array( $groups ) ) {
176 foreach ( $groups as $group ) {
177 $this->global_groups[ $group ] = true;
178 }
179 } else {
180 $this->global_groups[ $groups ] = true;
181 }
182 }
183
184 public function wp_cache_add_non_persistent_groups( $groups ) {
185 if ( is_array( $groups ) ) {
186 foreach ( $groups as $group ) {
187 $this->non_persistent_groups[ $group ] = true;
188 }
189 } else {
190 $this->non_persistent_groups[ $groups ] = true;
191 }
192 }
193
194 public function decr( $key, $offset = 1, $group = 'default' ) {
195 if ( $offset < 0 ) {
196 return $this->incr( $key, abs( $offset ), $group );
197 }
198
199 $group = $this->get_group( $group );
200 $key = $this->get_key( $group, $key );
201
202 if ( isset( $this->local_cache[ $group ][ $key ] ) && $this->local_cache[ $group ][ $key ] - $offset >= 0 ) {
203 $this->local_cache[ $group ][ $key ] -= $offset;
204 } else {
205 $this->local_cache[ $group ][ $key ] = 0;
206 }
207
208 if ( isset( $this->non_persistent_groups[ $group ] ) ) {
209 return $this->local_cache[ $group ][ $key ];
210 } else {
211 $value = apcu_dec( $key, $offset );
212 if ( $value < 0 ) {
213 apcu_store( $key, 0 );
214
215 return 0;
216 }
217
218 return $value;
219 }
220 }
221
222 public function delete( $key, $group = 'default', $force = false ) {
223 $group = $this->get_group( $group );
224 $key = $this->get_key( $group, $key );
225
226 unset( $this->local_cache[ $group ][ $key ] );
227 if ( ! isset( $this->non_persistent_groups[ $group ] ) ) {
228 return apcu_delete( $key );
229 }
230
231 return true;
232 }
233
234 public function flush() {
235 $this->local_cache = array();
236 // TODO: only clear our own entries
237 apcu_clear_cache();
238
239 return true;
240 }
241
242 public function get( $key, $group = 'default', $force = false, &$found = null ) {
243 $group = $this->get_group( $group );
244 $key = $this->get_key( $group, $key );
245
246 if ( ! $force && isset( $this->local_cache[ $group ][ $key ] ) ) {
247 $found = true;
248 $this->cache_hits += 1;
249 if ( is_object( $this->local_cache[ $group ][ $key ] ) ) {
250 return clone $this->local_cache[ $group ][ $key ];
251 } else {
252 return $this->local_cache[ $group ][ $key ];
253 }
254 } elseif ( isset( $this->non_persistent_groups[ $group ] ) ) {
255 $found = false;
256 $this->cache_misses += 1;
257
258 return false;
259 } else {
260 $value = apcu_fetch( $key, $found );
261 if ( $found ) {
262 if ( $force ) {
263 $this->local_cache[ $group ][ $key ] = $value;
264 }
265 $this->cache_hits += 1;
266
267 return $value;
268 } else {
269 $this->cache_misses += 1;
270
271 return false;
272 }
273 }
274 }
275
276 public function incr( $key, $offset = 1, $group = 'default' ) {
277 if ( $offset < 0 ) {
278 return $this->decr( $key, abs( $offset ), $group );
279 }
280
281 $group = $this->get_group( $group );
282 $key = $this->get_key( $group, $key );
283
284 if ( isset( $this->local_cache[ $group ][ $key ] ) && $this->local_cache[ $group ][ $key ] + $offset >= 0 ) {
285 $this->local_cache[ $group ][ $key ] += $offset;
286 } else {
287 $this->local_cache[ $group ][ $key ] = 0;
288 }
289
290 if ( isset( $this->non_persistent_groups[ $group ] ) ) {
291 return $this->local_cache[ $group ][ $key ];
292 } else {
293 $value = apcu_inc( $key, $offset );
294 if ( $value < 0 ) {
295 apcu_store( $key, 0 );
296
297 return 0;
298 }
299
300 return $value;
301 }
302 }
303
304 public function replace( $key, $data, $group = 'default', $expire = 0 ) {
305 $group = $this->get_group( $group );
306 $key = $this->get_key( $group, $key );
307
308 if ( isset( $this->non_persistent_groups[ $group ] ) ) {
309 if ( ! isset( $this->local_cache[ $group ][ $key ] ) ) {
310 return false;
311 }
312 } else {
313 if ( ! isset( $this->local_cache[ $group ][ $key ] ) && ! apcu_exists( $key ) ) {
314 return false;
315 }
316 apcu_store( $key, $data, (int) $expire );
317 }
318
319 if ( is_object( $data ) ) {
320 $this->local_cache[ $group ][ $key ] = clone $data;
321 } else {
322 $this->local_cache[ $group ][ $key ] = $data;
323 }
324
325 return true;
326 }
327
328 public function reset() {
329 // This function is deprecated as of WordPress 3.5
330 // Be safe and flush the cache if this function is still used
331 $this->flush();
332 }
333
334 public function set( $key, $data, $group = 'default', $expire = 0 ) {
335 $group = $this->get_group( $group );
336 $key = $this->get_key( $group, $key );
337
338 if ( is_object( $data ) ) {
339 $this->local_cache[ $group ][ $key ] = clone $data;
340 } else {
341 $this->local_cache[ $group ][ $key ] = $data;
342 }
343
344 if ( ! isset( $this->non_persistent_groups[ $group ] ) ) {
345 return apcu_store( $key, $data, (int) $expire );
346 }
347
348 return true;
349 }
350
351 public function stats() {
352 // Only implemented because the default cache class provides this.
353 // This method is never called.
354 echo '';
355 }
356
357 public function switch_to_blog( $blog_id ) {
358 $this->blog_prefix = $this->multisite ? $blog_id . ':' : '';
359 }
360
361 }
362