/
object.lib.php
492 lines (412 loc) · 9.44 KB
/
object.lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<?php
/**
* LiteSpeed Object Cache Library
*
* @since 1.8
*/
if ( ! defined( 'WPINC' ) ) {
die ;
}
require_once dirname( __FILE__ ) . '/object.class.php' ;
/**
* Sets up Object Cache Global and assigns it.
*
* @since 1.8
*/
function wp_cache_init()
{
$GLOBALS['wp_object_cache'] = WP_Object_Cache::get_instance();
}
/**
* Retrieves the cache contents from the cache by key and group.
*
* @since 1.8
*/
function wp_cache_get( $key, $group = '', $force = false, &$found = null )
{
global $wp_object_cache ;
return $wp_object_cache->get( $key, $group, $force, $found ) ;
}
/**
* Saves the data to the cache.
*
* @since 1.8
*/
function wp_cache_set( $key, $data, $group = '', $expire = 0 )
{
global $wp_object_cache ;
return $wp_object_cache->set( $key, $data, $group, $expire ) ;
}
/**
* Adds data to the cache, if the cache key doesn't already exist.
*
* @since 1.8
*/
function wp_cache_add( $key, $data, $group = '', $expire = 0 )
{
global $wp_object_cache ;
return $wp_object_cache->add( $key, $data, $group, $expire ) ;
}
/**
* Replaces the contents of the cache with new data.
*
* @since 1.8
*/
function wp_cache_replace( $key, $data, $group = '', $expire = 0 )
{
global $wp_object_cache ;
return $wp_object_cache->replace( $key, $data, $group, $expire ) ;
}
/**
* Increment numeric cache item's value
*
* @since 1.8
*/
function wp_cache_incr( $key, $offset = 1, $group = '' )
{
global $wp_object_cache ;
return $wp_object_cache->incr_desr( $key, $offset, $group ) ;
}
/**
* Decrements numeric cache item's value.
*
* @since 1.8
*/
function wp_cache_decr( $key, $offset = 1, $group = '' )
{
global $wp_object_cache ;
return $wp_object_cache->incr_desr( $key, $offset, $group, false ) ;
}
/**
* Removes the cache contents matching key and group.
*
* @since 1.8
*/
function wp_cache_delete( $key, $group = '' )
{
global $wp_object_cache ;
return $wp_object_cache->delete( $key, $group ) ;
}
/**
* Removes all cache items.
*
* @since 1.8
*/
function wp_cache_flush()
{
global $wp_object_cache ;
return $wp_object_cache->flush() ;
}
/**
* Adds a group or set of groups to the list of global groups.
*
* @since 1.8
*/
function wp_cache_add_global_groups( $groups )
{
global $wp_object_cache ;
$wp_object_cache->add_global_groups( $groups ) ;
}
/**
* Adds a group or set of groups to the list of non-persistent groups.
*
* @since 1.8
*/
function wp_cache_add_non_persistent_groups( $groups )
{
global $wp_object_cache ;
$wp_object_cache->add_non_persistent_groups( $groups ) ;
}
/**
* Switches the internal blog ID.
*
* This changes the blog id used to create keys in blog specific groups.
*
* @since 1.8
*
* @see WP_Object_Cache::switch_to_blog()
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
*
* @param int $blog_id Site ID.
*/
function wp_cache_switch_to_blog( $blog_id ) {
global $wp_object_cache ;
$wp_object_cache->switch_to_blog( $blog_id ) ;
}
/**
* Closes the cache.
*
* @since 1.8
*/
function wp_cache_close()
{
return true ;
}
class WP_Object_Cache
{
private static $_instance ;
private $_object_cache ;
private $_cache = array() ;
private $_cache_404 = array() ;
private $cache_total = 0 ;
private $count_hit_incall = 0 ;
private $count_hit = 0 ;
private $count_miss_incall = 0 ;
private $count_miss = 0 ;
private $count_set = 0 ;
private $blog_prefix ;
/**
* Init
*
* @since 1.8
* @access private
*/
private function __construct()
{
$this->_object_cache = LiteSpeed_Cache_Object::get_instance() ;
$this->multisite = is_multisite() ;
$this->blog_prefix = $this->multisite ? get_current_blog_id() . ':' : '' ;
/**
* Fix multiple instance using same oc issue
* @since 1.8.2
*/
! defined( 'LSOC_PREFIX' ) && define( 'LSOC_PREFIX', substr( md5( __FILE__ ), -5 ) ) ;
}
/**
* Output debug info
*
* @since 1.8
* @access public
*/
public function debug()
{
$log = ' [total] ' . $this->cache_total
. ' [hit_incall] ' . $this->count_hit_incall
. ' [hit] ' . $this->count_hit
. ' [miss_incall] ' . $this->count_miss_incall
. ' [miss] ' . $this->count_miss
. ' [set] ' . $this->count_set ;
return $log ;
}
/**
* Get from cache
*
* @since 1.8
* @access public
*/
public function get( $key, $group = 'default', $force = false, &$found = null )
{
$final_key = $this->_key( $key, $group ) ;
// error_log('');
// error_log("oc: get \t\t\t[key] " . $final_key . ( $force ? "\t\t\t [forced] " : '' ) );
$found = false ;
$found_in_oc = false ;
$cache_val = false ;
if ( array_key_exists( $final_key, $this->_cache ) && ! $force ) {
$found = true ;
$cache_val = $this->_cache[ $final_key ] ;
$this->count_hit_incall ++ ;
}
elseif ( ! array_key_exists( $final_key, $this->_cache_404 ) && ! $this->_object_cache->is_non_persistent( $group ) ) {
$v = $this->_object_cache->get( $final_key ) ;
$v = @unserialize( $v ) ;
// To be compatible with false val
if ( is_array( $v ) && array_key_exists( 'data', $v ) ) {
$this->count_hit ++ ;
$found = true ;
$found_in_oc = true ;
$cache_val = $v[ 'data' ] ;
}
else { // Can't find key, cache it to 404
// error_log("oc: add404\t\t\t[key] " . $final_key ) ;
$this->_cache_404[ $final_key ] = 1 ;
$this->count_miss ++ ;
}
}
else {
$this->count_miss_incall ++ ;
}
if ( is_object( $cache_val ) ) {
$cache_val = clone $cache_val ;
}
if ( $found_in_oc ) {
$this->_cache[ $final_key ] = $cache_val ;
}
$this->cache_total ++ ;
return $cache_val ;
}
/**
* Set to cache
*
* @since 1.8
* @access public
*/
public function set( $key, $data, $group = 'default', $expire = 0 )
{
$final_key = $this->_key( $key, $group ) ;
if ( is_object( $data ) ) {
$data = clone $data ;
}
// error_log("oc: set \t\t\t[key] " . $final_key ) ;
$this->_cache[ $final_key ] = $data ;
if( array_key_exists( $final_key, $this->_cache_404 ) ) {
// error_log("oc: unset404\t\t\t[key] " . $final_key ) ;
unset( $this->_cache_404[ $final_key ] ) ;
}
if ( ! $this->_object_cache->is_non_persistent( $group ) ) {
$this->_object_cache->set( $final_key, serialize( array( 'data' => $data ) ), $expire ) ;
$this->count_set ++ ;
}
return true ;
}
/**
* Adds data to the cache if it doesn't already exist.
*
* @since 1.8
* @access public
*/
public function add( $key, $data, $group = 'default', $expire = 0 )
{
if ( wp_suspend_cache_addition() ) {
return false ;
}
$final_key = $this->_key( $key, $group ) ;
if ( array_key_exists( $final_key, $this->_cache ) ) {
return false ;
}
return $this->set( $key, $data, $group, $expire ) ;
}
/**
* Replace cache if the cache key exists.
*
* @since 1.8
* @access public
*/
public function replace( $key, $data, $group = 'default', $expire = 0 )
{
$final_key = $this->_key( $key, $group ) ;
if ( ! array_key_exists( $final_key, $this->_cache ) ) {
return false ;
}
return $this->set( $key, $data, $group, $expire ) ;
}
/**
* Increments numeric cache item's value.
*
* @since 1.8
* @access public
*/
public function incr_desr( $key, $offset = 1, $group = 'default', $incr = true )
{
$cache_val = $this->get( $key, $group ) ;
if ( $cache_val === false ) {
return false ;
}
if ( ! is_numeric( $cache_val ) ) {
$cache_val = 0 ;
}
$offset = (int) $offset ;
if ( $incr ) {
$cache_val += $offset ;
}
else {
$cache_val -= $offset ;
}
if ( $cache_val < 0 ) {
$cache_val = 0 ;
}
$this->set( $key, $cache_val, $group ) ;
return $cache_val ;
}
/**
* Delete cache
*
* @since 1.8
* @access public
*/
public function delete( $key, $group = 'default' )
{
$final_key = $this->_key( $key, $group ) ;
if ( ! array_key_exists( $final_key, $this->_cache ) ) {
return false ;
}
// error_log("oc: delete \t\t\t[key] " . $final_key ) ;
unset( $this->_cache[ $final_key ] ) ;
if ( $this->_object_cache->is_non_persistent( $group ) ) {
return false ;
}
return $this->_object_cache->delete( $final_key ) ;
}
/**
* Clear all cached data
*
* @since 1.8
* @access public
*/
public function flush()
{
$this->_cache = array() ;
$this->_cache_404 = array() ;
// error_log("oc: flush " ) ;
$this->_object_cache->flush() ;
return true ;
}
/**
* Add global groups
*
* @since 1.8
* @access public
*/
public function add_global_groups( $groups )
{
$this->_object_cache->add_global_groups( $groups ) ;
}
/**
* Add non persistent groups
*
* @since 1.8
* @access public
*/
public function add_non_persistent_groups( $groups )
{
$this->_object_cache->add_non_persistent_groups( $groups ) ;
}
/**
* Get the final key
*
* @since 1.8
* @access private
*/
private function _key( $key, $group = 'default' )
{
$prefix = $this->_object_cache->is_global( $group ) ? '' : $this->blog_prefix ;
return LSOC_PREFIX . $prefix . $group . '.' . $key ;
}
/**
* Switches the internal blog ID.
*
* This changes the blog ID used to create keys in blog specific groups.
*
* @since 1.8
*
* @param int $blog_id Blog ID.
*/
public function switch_to_blog( $blog_id ) {
$blog_id = (int) $blog_id ;
$this->blog_prefix = $this->multisite ? $blog_id . ':' : '' ;
}
/**
* Get the current instance object.
*
* @since 1.8
* @access public
* @return Current class instance.
*/
public static function get_instance()
{
if ( ! isset( self::$_instance ) ) {
self::$_instance = new self() ;
}
return self::$_instance ;
}
}