weblog-content /wp-content/plugins/w3-total-cache/lib/W3/ObjectCache.php

Language PHP Lines 643
MD5 Hash 105b83a13ec46341d6d31389a9aacfe8 Estimated Cost $7,739 (why?)
Repository https://bitbucket.org/openfarmtech/weblog-content View Raw File View Project SPDX
  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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
<?php

/**
 * W3 Object Cache object
 */
class W3_ObjectCache
{
    /**
     * Internal cache array
     * 
     * @var array
     */
    var $cache = array();
    
    /**
     * Array of global groups
     * 
     * @var array
     */
    var $global_groups = array();
    
    /**
     * List of non-persistent groups
     *
     * @var array
     */
    var $nonpersistent_groups = array();
    
    /**
     * Total count of calls
     *
     * @var integer
     */
    var $cache_total = 0;
    
    /**
     * Cache hits count
     * 
     * @var integer
     */
    var $cache_hits = 0;
    
    /**
     * Cache misses count
     *
     * @var integer
     */
    var $cache_misses = 0;
    
    /**
     * Store debug information of w3tc using
     *
     * @var array
     */
    var $debug_info = array();
    
    /**
     * Config
     *
     * @var W3_Config
     */
    var $_config = null;
    
    /**
     * Caching flag
     * 
     * @var boolean
     */
    var $_caching = false;
    
    /**
     * Cache reject reason
     * 
     * @var string
     */
    var $_cache_reject_reason = '';
    
    /**
     * Lifetime
     *
     * @var integer
     */
    var $_lifetime = null;
    
    /**
     * Debug flag
     * 
     * @var boolean
     */
    var $_debug = false;
    
    /**
     * PHP5 style constructor
     */
    function __construct()
    {
        require_once W3TC_LIB_W3_DIR . '/Config.php';
        
        $this->_config = & W3_Config::instance();
        $this->_lifetime = $this->_config->get_integer('objectcache.lifetime');
        $this->_debug = $this->_config->get_boolean('objectcache.debug');
        
        $this->global_groups = $this->_config->get_array('objectcache.groups.global');
        $this->nonpersistent_groups = $this->_config->get_array('objectcache.groups.nonpersistent');
        
        $this->_caching = $this->_can_cache();
        
        $GLOBALS['_wp_using_ext_object_cache'] = $this->_caching;
        
        if ($this->_can_ob()) {
            ob_start(array(
                &$this, 
                'ob_callback'
            ));
        }
    }
    
    /**
     * PHP4 style constructor
     */
    function W3_ObjectCache()
    {
        $this->__construct();
    }
    
    /**
     * Returns onject instance
     *
     * @return W3_ObjectCache
     */
    function &instance()
    {
        static $instances = array();
        
        if (!isset($instances[0])) {
            $class = __CLASS__;
            $instances[0] = & new $class();
        }
        
        return $instances[0];
    }
    
    /**
     * Get from the cache
     * 
     * @param string $id
     * @param string $group
     * @return mixed
     */
    function get($id, $group = 'default')
    {
        $key = $this->_get_cache_key($id, $group);
        
        $caching = true;
        $internal = true;
        $reason = '';
        
        if (isset($this->cache[$key])) {
            $value = $this->cache[$key];
        } else {
            $caching = $this->_can_cache2($id, $group, $reason);
            $internal = false;
            
            if ($caching) {
                $cache = & $this->_get_cache();
                $value = $cache->get($key);
            } else {
                $value = false;
            }
            
            $this->cache[$key] = $value;
        }
        
        if ($value === null) {
            $value = false;
        }
        
        $this->cache_total++;
        
        if ($value !== false) {
            $cached = true;
            $this->cache_hits++;
        } else {
            $cached = false;
            $this->cache_misses++;
        }
        
        /**
         * Add debug info
         */
        if ($this->_debug) {
            if (!$group) {
                $group = 'default';
            }
            
            $this->debug_info[] = array(
                'id' => $id, 
                'group' => $group, 
                'caching' => $caching, 
                'reason' => $reason, 
                'cached' => $cached, 
                'internal' => $internal
            );
        }
        
        return $value;
    }
    
    /**
     * Set to the cache
     * 
     * @param string $id
     * @param mixed $data
     * @param string $group
     * @param integer $expire
     * @return boolean
     */
    function set($id, $data, $group = 'default', $expire = 0)
    {
        $key = $this->_get_cache_key($id, $group);
        
        if (is_object($data)) {
            $data = wp_clone($data);
        }
        
        if (isset($this->cache[$key]) && $this->cache[$key] === $data) {
            return true;
        }
        
        $this->cache[$key] = $data;
        
        $reason = '';
        
        if ($this->_can_cache2($id, $group, $reason)) {
            $cache = & $this->_get_cache();
            
            return $cache->set($key, $data, ($expire ? $expire : $this->_lifetime));
        }
        
        return true;
    }
    
    /**
     * Delete from the cache
     * 
     * @param string $id
     * @param string $group
     * @return boolean
     */
    function delete($id, $group = 'default')
    {
        $key = $this->_get_cache_key($id, $group);
        
        unset($this->cache[$key]);
        
        $reason = '';
        
        if ($this->_can_cache2($id, $group, $reason)) {
            $cache = & $this->_get_cache();
            
            return $cache->delete($key);
        }
        
        return true;
    }
    
    /**
     * Add to the cache
     * 
     * @param string $id
     * @param mixed $data
     * @param string $group
     * @param integer $expire
     * @return boolean
     */
    function add($id, $data, $group = 'default', $expire = 0)
    {
        if ($this->get($id, $group) !== false) {
            return false;
        }
        
        return $this->set($id, $data, $group, $expire);
    }
    
    /**
     * Replace in the cache
     * 
     * @param string $id
     * @param mixed $data
     * @param string $group
     * @param integer $expire
     * @return boolean
     */
    function replace($id, $data, $group = 'default', $expire = 0)
    {
        if ($this->get($id, $group) === false) {
            return false;
        }
        
        return $this->set($id, $data, $group, $expire);
    }
    
    /**
     * Flush cache
     * 
     * @return boolean
     */
    function flush()
    {
        $this->cache = array();
        
        $cache = & $this->_get_cache();
        
        return $cache->flush();
    }
    
    /**
     * Add global groups
     * 
     * @param array $groups
     * @return void
     */
    function add_global_groups($groups)
    {
        if (!is_array($groups)) {
            $groups = (array) $groups;
        }
        
        $this->global_groups = array_merge($this->global_groups, $groups);
        $this->global_groups = array_unique($this->global_groups);
    }
    
    /**
     * Add non-persistent groups
     * 
     * @param array $groups
     * @return void
     */
    function add_nonpersistent_groups($groups)
    {
        if (!is_array($groups)) {
            $groups = (array) $groups;
        }
        
        $this->nonpersistent_groups = array_merge($this->nonpersistent_groups, $groups);
        $this->nonpersistent_groups = array_unique($this->nonpersistent_groups);
    }
    
    /**
     * Output buffering callback
     *
     * @param string $buffer
     * @return string
     */
    function ob_callback(&$buffer)
    {
        if ($buffer != '' && w3_is_xml($buffer)) {
            $buffer .= "\r\n\r\n" . $this->_get_debug_info();
        }
        
        return $buffer;
    }
    
    /**
     * Returns cache key
     * 
     * @param string $id
     * @param string $group
     * @return string
     */
    function _get_cache_key($id, $group = 'default')
    {
        if (!$group) {
            $group = 'default';
        }
        
        $host = w3_get_host();
        
        if (!in_array($group, $this->global_groups)) {
            $prefix = w3_get_blogname();
        } else {
            $prefix = '';
        }
        
        $key = sprintf('w3tc_%s_object_%s', md5($host), md5($prefix . $group . $id));
        
        /**
         * Allow to modify cache key by W3TC plugins
         */
        $key = w3tc_do_action('w3tc_objectcache_cache_key', $key);
        
        return $key;
    }
    
    /**
     * Returns cache object
     *
     * @return W3_Cache_Base
     */
    function &_get_cache()
    {
        static $cache = array();
        
        if (!isset($cache[0])) {
            $engine = $this->_config->get_string('objectcache.engine');
            
            switch ($engine) {
                case 'memcached':
                    $engineConfig = array(
                        'servers' => $this->_config->get_array('objectcache.memcached.servers'), 
                        'persistant' => $this->_config->get_boolean('objectcache.memcached.persistant')
                    );
                    break;
                
                case 'file':
                    $engineConfig = array(
                        'cache_dir' => W3TC_CACHE_FILE_OBJECTCACHE_DIR, 
                        'locking' => $this->_config->get_boolean('objectcache.file.locking')
                    );
                    break;
                
                default:
                    $engineConfig = array();
            }
            
            require_once W3TC_LIB_W3_DIR . '/Cache.php';
            $cache[0] = & W3_Cache::instance($engine, $engineConfig);
        }
        
        return $cache[0];
    }
    
    /**
     * Check if caching allowed on init
     * 
     * @return boolean
     */
    function _can_cache()
    {
        /**
         * Skip if disabled
         */
        if (!$this->_config->get_boolean('objectcache.enabled')) {
            $this->_cache_reject_reason = 'object caching is disabled';
            
            return false;
        }
        
        /**
         * Check for DONOTCACHCEOBJECT constant
         */
        if (defined('DONOTCACHCEOBJECT') && DONOTCACHCEOBJECT) {
            $this->_cache_reject_reason = 'DONOTCACHCEOBJECT constant is defined';
            
            return false;
        }
        
        /**
         * Skip if admin
         */
        if ($this->_config->get_boolean('objectcache.reject.admin') && defined('WP_ADMIN')) {
            $this->_cache_reject_reason = 'wp-admin';
            
            return false;
        }
        
        /**
         * Check request URI
         */
        if (!$this->_check_request_uri()) {
            $this->_cache_reject_reason = 'request URI is rejected';
            
            return false;
        }
        
        return true;
    }
    
    /**
     * Check if caching allowed runtime
     *
     * @param string $id
     * @param string $group
     * @return boolean
     */
    function _can_cache2($id, $group, &$cache_reject_reason)
    {
        /**
         * Skip if disabled
         */
        if (!$this->_caching) {
            $cache_reject_reason = $this->_cache_reject_reason;
            
            return false;
        }
        
        /**
         * Check for DONOTCACHCEOBJECT constant
         */
        if (defined('DONOTCACHCEOBJECT') && DONOTCACHCEOBJECT) {
            $cache_reject_reason = 'DONOTCACHCEOBJECT constant is defined';
            
            return false;
        }
        
        /**
         * Skip if key in non-persistent group
         */
        if (!$group) {
            $group = 'default';
        }
        
        if (in_array($group, $this->nonpersistent_groups)) {
            $cache_reject_reason = 'non-persistent group';
            
            return false;
        }
        
        return true;
    }
    
    /**
     * Check request URI
     *
     * @return boolean
     */
    function _check_request_uri()
    {
        $auto_reject_uri = array(
            'wp-login', 
            'wp-register'
        );
        
        foreach ($auto_reject_uri as $uri) {
            if (strstr($_SERVER['REQUEST_URI'], $uri) !== false) {
                return false;
            }
        }
        
        $reject_uri = $this->_config->get_array('objectcache.reject.uri');
        
        foreach ($reject_uri as $expr) {
            $expr = trim($expr);
            if ($expr != '' && preg_match('~' . $expr . '~i', $_SERVER['REQUEST_URI'])) {
                return false;
            }
        }
        
        return true;
    }
    
    /**
     * Check if we can start OB
     * 
     * @return boolean
     */
    function _can_ob()
    {
        /**
         * Object cache should be enabled
         */
        if (!$this->_config->get_boolean('objectcache.enabled')) {
            return false;
        }
        
        /**
         * Debug should be enabled
         */
        if (!$this->_config->get_boolean('objectcache.debug')) {
            return false;
        }
        
        /**
         * Skip if admin
         */
        if (defined('WP_ADMIN')) {
            return false;
        }
        
        /**
         * Skip if doint AJAX
         */
        if (defined('DOING_AJAX')) {
            return false;
        }
        
        /**
         * Skip if doing cron
         */
        if (defined('DOING_CRON')) {
            return false;
        }
        
        /**
         * Skip if APP request
         */
        if (defined('APP_REQUEST')) {
            return false;
        }
        
        /**
         * Skip if XMLRPC request
         */
        if (defined('XMLRPC_REQUEST')) {
            return false;
        }
        
        /**
         * Check for WPMU's and WP's 3.0 short init
         */
        if (defined('SHORTINIT') && SHORTINIT) {
            return false;
        }
        
        return true;
    }
    
    /**
     * Returns debug info
     * 
     * @return string
     */
    function _get_debug_info()
    {
        $debug_info = "<!-- W3 Total Cache: Object Cache debug info:\r\n";
        $debug_info .= sprintf("%s%s\r\n", str_pad('Engine: ', 20), w3_get_engine_name($this->_config->get_string('objectcache.engine')));
        $debug_info .= sprintf("%s%d\r\n", str_pad('Total calls: ', 20), $this->cache_total);
        $debug_info .= sprintf("%s%d\r\n", str_pad('Cache hits: ', 20), $this->cache_hits);
        $debug_info .= sprintf("%s%d\r\n", str_pad('Cache misses: ', 20), $this->cache_misses);
        
        $debug_info .= "W3TC Object Cache info:\r\n";
        $debug_info .= sprintf("%s | %s | %s | %s | %s\r\n", str_pad('#', 5, ' ', STR_PAD_LEFT), str_pad('Caching (Reject reason)', 30, ' ', STR_PAD_BOTH), str_pad('Status', 15, ' ', STR_PAD_BOTH), str_pad('Source', 15, ' ', STR_PAD_BOTH), 'ID:Group');
        
        foreach ($this->debug_info as $index => $debug) {
            $debug_info .= sprintf("%s | %s | %s | %s | %s\r\n", str_pad($index + 1, 5, ' ', STR_PAD_LEFT), str_pad(($debug['caching'] ? 'enabled' : sprintf('disabled (%s)', $debug['reason'])), 30, ' ', STR_PAD_BOTH), str_pad(($debug['cached'] ? 'cached' : 'not cached'), 15, ' ', STR_PAD_BOTH), str_pad(($debug['internal'] ? 'internal' : 'persistent'), 15, ' ', STR_PAD_BOTH), sprintf('%s:%s', $debug['id'], $debug['group']));
        }
        
        $debug_info .= '-->';
        
        return $debug_info;
    }
}
Back to Top