-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathobject-cache.cls.php
More file actions
855 lines (749 loc) · 20.8 KB
/
Copy pathobject-cache.cls.php
File metadata and controls
855 lines (749 loc) · 20.8 KB
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
<?php
/**
* The object cache class.
*
* @since 1.8
* @package LiteSpeed
*/
namespace LiteSpeed;
defined( 'WPINC' ) || exit();
require_once dirname( __DIR__ ) . '/autoload.php';
/**
* Object cache handler using Redis or Memcached.
*
* NOTE: this class may be included without initialized core.
*
* @since 1.8
*/
class Object_Cache extends Root {
const LOG_TAG = '[Object_Cache]';
/**
* Debug option key.
*
* @var string
*/
const O_DEBUG = 'debug';
/**
* Object cache enable key.
*
* @var string
*/
const O_OBJECT = 'object';
/**
* Object kind (Redis/Memcached).
*
* @var string
*/
const O_OBJECT_KIND = 'object-kind';
/**
* Object host.
*
* @var string
*/
const O_OBJECT_HOST = 'object-host';
/**
* Object port.
*
* @var string
*/
const O_OBJECT_PORT = 'object-port';
/**
* Object life/TTL.
*
* @var string
*/
const O_OBJECT_LIFE = 'object-life';
/**
* Persistent connection flag.
*
* @var string
*/
const O_OBJECT_PERSISTENT = 'object-persistent';
/**
* Admin cache flag.
*
* @var string
*/
const O_OBJECT_ADMIN = 'object-admin';
/**
* DB index for Redis.
*
* @var string
*/
const O_OBJECT_DB_ID = 'object-db_id';
/**
* Username for auth.
*
* @var string
*/
const O_OBJECT_USER = 'object-user';
/**
* Password for auth.
*
* @var string
*/
const O_OBJECT_PSWD = 'object-pswd';
/**
* Global groups list.
*
* @var string
*/
const O_OBJECT_GLOBAL_GROUPS = 'object-global_groups';
/**
* Non-persistent groups list.
*
* @var string
*/
const O_OBJECT_NON_PERSISTENT_GROUPS = 'object-non_persistent_groups';
/**
* Connection instance.
*
* @var \Redis|\Memcached|null
*/
private $_conn;
/**
* Debug config.
*
* @var bool
*/
private $_cfg_debug;
/**
* Whether OC is enabled.
*
* @var bool
*/
private $_cfg_enabled;
/**
* True => Redis, false => Memcached.
*
* @var bool
*/
private $_cfg_method;
/**
* Host name.
*
* @var string
*/
private $_cfg_host;
/**
* Port number.
*
* @var int|string
*/
private $_cfg_port;
/**
* TTL in seconds.
*
* @var int
*/
private $_cfg_life;
/**
* Use persistent connection.
*
* @var bool
*/
private $_cfg_persistent;
/**
* Cache admin pages.
*
* @var bool
*/
private $_cfg_admin;
/**
* Redis DB index.
*
* @var int
*/
private $_cfg_db;
/**
* Auth username.
*
* @var string
*/
private $_cfg_user;
/**
* Auth password.
*
* @var string
*/
private $_cfg_pswd;
/**
* Default TTL in seconds.
*
* @var int
*/
private $_default_life = 360;
/**
* 'Redis' or 'Memcached'.
*
* @var string
*/
private $_oc_driver = 'Memcached'; // Redis or Memcached.
/**
* Global groups.
*
* @var array
*/
private $_global_groups = [];
/**
* Non-persistent groups.
*
* @var array
*/
private $_non_persistent_groups = [];
/**
* Init.
*
* NOTE: this class may be included without initialized core.
*
* @since 1.8
*
* @param array|false $cfg Optional configuration to bootstrap without core.
*/
public function __construct( $cfg = false ) {
if ( $cfg ) {
if ( ! is_array( $cfg[ Base::O_OBJECT_GLOBAL_GROUPS ] ) ) {
$cfg[ Base::O_OBJECT_GLOBAL_GROUPS ] = explode( "\n", $cfg[ Base::O_OBJECT_GLOBAL_GROUPS ] );
}
if ( ! is_array( $cfg[ Base::O_OBJECT_NON_PERSISTENT_GROUPS ] ) ) {
$cfg[ Base::O_OBJECT_NON_PERSISTENT_GROUPS ] = explode( "\n", $cfg[ Base::O_OBJECT_NON_PERSISTENT_GROUPS ] );
}
$this->_cfg_debug = $cfg[ Base::O_DEBUG ] ? $cfg[ Base::O_DEBUG ] : false;
$this->_cfg_method = $cfg[ Base::O_OBJECT_KIND ] ? true : false;
$this->_cfg_host = $cfg[ Base::O_OBJECT_HOST ];
$this->_cfg_port = $cfg[ Base::O_OBJECT_PORT ];
$this->_cfg_life = $cfg[ Base::O_OBJECT_LIFE ];
$this->_cfg_persistent = $cfg[ Base::O_OBJECT_PERSISTENT ];
$this->_cfg_admin = $cfg[ Base::O_OBJECT_ADMIN ];
$this->_cfg_db = $cfg[ Base::O_OBJECT_DB_ID ];
$this->_cfg_user = $cfg[ Base::O_OBJECT_USER ];
$this->_cfg_pswd = $cfg[ Base::O_OBJECT_PSWD ];
$this->_global_groups = $cfg[ Base::O_OBJECT_GLOBAL_GROUPS ];
$this->_non_persistent_groups = $cfg[ Base::O_OBJECT_NON_PERSISTENT_GROUPS ];
if ( $this->_cfg_method ) {
$this->_oc_driver = 'Redis';
}
$this->_cfg_enabled = $cfg[ Base::O_OBJECT ] && class_exists( $this->_oc_driver ) && $this->_cfg_host;
} elseif ( defined( 'LITESPEED_CONF_LOADED' ) ) { // If OC is OFF, will hit here to init OC after conf initialized
$this->_cfg_debug = $this->conf( Base::O_DEBUG ) ? $this->conf( Base::O_DEBUG ) : false;
$this->_cfg_method = $this->conf( Base::O_OBJECT_KIND ) ? true : false;
$this->_cfg_host = $this->conf( Base::O_OBJECT_HOST );
$this->_cfg_port = $this->conf( Base::O_OBJECT_PORT );
$this->_cfg_life = $this->conf( Base::O_OBJECT_LIFE );
$this->_cfg_persistent = $this->conf( Base::O_OBJECT_PERSISTENT );
$this->_cfg_admin = $this->conf( Base::O_OBJECT_ADMIN );
$this->_cfg_db = $this->conf( Base::O_OBJECT_DB_ID );
$this->_cfg_user = $this->conf( Base::O_OBJECT_USER );
$this->_cfg_pswd = $this->conf( Base::O_OBJECT_PSWD );
$this->_global_groups = $this->conf( Base::O_OBJECT_GLOBAL_GROUPS );
$this->_non_persistent_groups = $this->conf( Base::O_OBJECT_NON_PERSISTENT_GROUPS );
if ( $this->_cfg_method ) {
$this->_oc_driver = 'Redis';
}
$this->_cfg_enabled = $this->conf( Base::O_OBJECT ) && class_exists( $this->_oc_driver ) && $this->_cfg_host;
} elseif ( defined( 'self::CONF_FILE' ) && file_exists( WP_CONTENT_DIR . '/' . self::CONF_FILE ) ) {
// Get cfg from _data_file.
// Use self::const to avoid loading more classes.
$cfg = \json_decode( file_get_contents( WP_CONTENT_DIR . '/' . self::CONF_FILE ), true );
if ( ! empty( $cfg[ self::O_OBJECT_HOST ] ) ) {
$this->_cfg_debug = ! empty( $cfg[ Base::O_DEBUG ] ) ? $cfg[ Base::O_DEBUG ] : false;
$this->_cfg_method = ! empty( $cfg[ self::O_OBJECT_KIND ] ) ? $cfg[ self::O_OBJECT_KIND ] : false;
$this->_cfg_host = $cfg[ self::O_OBJECT_HOST ];
$this->_cfg_port = $cfg[ self::O_OBJECT_PORT ];
$this->_cfg_life = ! empty( $cfg[ self::O_OBJECT_LIFE ] ) ? $cfg[ self::O_OBJECT_LIFE ] : $this->_default_life;
$this->_cfg_persistent = ! empty( $cfg[ self::O_OBJECT_PERSISTENT ] ) ? $cfg[ self::O_OBJECT_PERSISTENT ] : false;
$this->_cfg_admin = ! empty( $cfg[ self::O_OBJECT_ADMIN ] ) ? $cfg[ self::O_OBJECT_ADMIN ] : false;
$this->_cfg_db = ! empty( $cfg[ self::O_OBJECT_DB_ID ] ) ? $cfg[ self::O_OBJECT_DB_ID ] : 0;
$this->_cfg_user = ! empty( $cfg[ self::O_OBJECT_USER ] ) ? $cfg[ self::O_OBJECT_USER ] : '';
$this->_cfg_pswd = ! empty( $cfg[ self::O_OBJECT_PSWD ] ) ? $cfg[ self::O_OBJECT_PSWD ] : '';
$this->_global_groups = ! empty( $cfg[ self::O_OBJECT_GLOBAL_GROUPS ] ) ? $cfg[ self::O_OBJECT_GLOBAL_GROUPS ] : [];
$this->_non_persistent_groups = ! empty( $cfg[ self::O_OBJECT_NON_PERSISTENT_GROUPS ] ) ? $cfg[ self::O_OBJECT_NON_PERSISTENT_GROUPS ] : [];
if ( $this->_cfg_method ) {
$this->_oc_driver = 'Redis';
}
$this->_cfg_enabled = class_exists( $this->_oc_driver ) && $this->_cfg_host;
} else {
$this->_cfg_enabled = false;
}
} else {
$this->_cfg_enabled = false;
}
// If OC not available, mark failure so OC methods return false early.
// NOTE: Do NOT call wp_using_ext_object_cache(false) here — it causes
// "Cannot redeclare wp_cache_init()" fatal on multisite (second call
// to wp_start_object_cache() would load cache.php again).
if ( ! $this->_cfg_enabled ) {
! defined( 'LITESPEED_OC_FAILURE' ) && define( 'LITESPEED_OC_FAILURE', true );
}
}
/**
* Add debug.
*
* @since 6.3
* @access private
*
* @param string $text Log text.
* @return void
*/
private function debug_oc( $text ) {
if ( defined( 'LSCWP_LOG' ) ) {
self::debug( $text );
return;
}
if ( Base::VAL_ON2 !== $this->_cfg_debug ) {
return;
}
$litespeed_data_folder = defined( 'LITESPEED_DATA_FOLDER' ) ? LITESPEED_DATA_FOLDER : 'litespeed';
$lscwp_content_dir = defined( 'LSCWP_CONTENT_DIR' ) ? LSCWP_CONTENT_DIR : WP_CONTENT_DIR;
$litespeed_static_dir = $lscwp_content_dir . '/' . $litespeed_data_folder;
$log_path_prefix = $litespeed_static_dir . '/debug/';
$log_file = $log_path_prefix . Debug2::FilePath( 'debug' );
if ( file_exists( $log_path_prefix . 'index.php' ) && file_exists( $log_file ) ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log(gmdate('m/d/y H:i:s') . ' - OC - ' . $text . PHP_EOL, 3, $log_file);
}
}
/**
* Check if the group belongs to transients or not.
*
* @since 1.8.3
* @access private
*
* @param string $group Group name.
* @return bool
*/
private function _is_transients_group( $group ) {
return in_array( $group, [ 'transient', 'site-transient' ], true );
}
/**
* Update WP object cache file config.
*
* @since 1.8
* @access public
*
* @param array $options Options to apply after update.
* @return void
*/
public function update_file( $options ) {
$changed = false;
// NOTE: When included in oc.php, `LSCWP_DIR` will show undefined, so this must be assigned/generated when used.
$_oc_ori_file = LSCWP_DIR . 'lib/object-cache.php';
$_oc_wp_file = WP_CONTENT_DIR . '/object-cache.php';
// Update cls file.
if ( ! file_exists( $_oc_wp_file ) || md5_file( $_oc_wp_file ) !== md5_file( $_oc_ori_file ) ) {
$this->debug_oc( 'copying object-cache.php file to ' . $_oc_wp_file );
copy( $_oc_ori_file, $_oc_wp_file );
$changed = true;
}
/**
* Clear object cache.
*/
if ( $changed ) {
$this->_reconnect( $options );
}
}
/**
* Remove object cache file.
*
* @since 1.8.2
* @access public
*
* @return void
*/
public function del_file() {
// NOTE: When included in oc.php, `LSCWP_DIR` will show undefined, so this must be assigned/generated when used.
$_oc_ori_file = LSCWP_DIR . 'lib/object-cache.php';
$_oc_wp_file = WP_CONTENT_DIR . '/object-cache.php';
if ( file_exists( $_oc_wp_file ) && md5_file( $_oc_wp_file ) === md5_file( $_oc_ori_file ) ) {
$this->debug_oc( 'removing ' . $_oc_wp_file );
wp_delete_file( $_oc_wp_file );
}
}
/**
* Try to build connection.
*
* @since 1.8
* @access public
*
* @return bool|null False on failure, true on success, null if unsupported.
*/
public function test_connection() {
return $this->_connect();
}
/**
* Force to connect with this setting.
*
* @since 1.8
* @access private
*
* @param array $cfg Reconnect configuration.
* @return void
*/
private function _reconnect( $cfg ) {
$this->debug_oc( 'Reconnecting' );
if ( isset( $this->_conn ) ) {
// error_log( 'Object: Quitting existing connection!' );
$this->debug_oc( 'Quitting existing connection' );
$this->flush();
$this->_conn = null;
$this->cls( false, true );
}
$cls = $this->cls( false, false, $cfg );
$cls->_connect();
if ( isset( $cls->_conn ) ) {
$cls->flush();
}
}
/**
* Connect to Memcached/Redis server.
*
* @since 1.8
* @access private
*
* @return bool|null False on failure, true on success, null if driver missing.
*/
private function _connect() {
if ( isset( $this->_conn ) ) {
// error_log( 'Object: _connected' );
return true;
}
if ( ! class_exists( $this->_oc_driver ) || ! $this->_cfg_host ) {
$this->debug_oc( '_oc_driver cls non existed or _cfg_host missed: ' . $this->_oc_driver . ' [_cfg_host] ' . $this->_cfg_host . ':' . $this->_cfg_port );
return false;
}
if ( defined( 'LITESPEED_OC_FAILURE' ) ) {
$this->debug_oc( 'LITESPEED_OC_FAILURE const defined' );
return false;
}
$this->debug_oc( 'Init ' . $this->_oc_driver . ' connection to ' . $this->_cfg_host . ':' . $this->_cfg_port );
$failed = false;
/**
* Connect to Redis.
*
* @since 1.8.1
* @see https://github.com/phpredis/phpredis/#example-1
*/
if ( 'Redis' === $this->_oc_driver ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler
set_error_handler( 'litespeed_exception_handler' );
try {
$this->_conn = new \Redis();
// error_log( 'Object: _connect Redis' );
if ( $this->_cfg_persistent ) {
if ( $this->_cfg_port ) {
$this->_conn->pconnect( $this->_cfg_host, $this->_cfg_port );
} else {
$this->_conn->pconnect( $this->_cfg_host );
}
} elseif ( $this->_cfg_port ) {
$this->_conn->connect( $this->_cfg_host, $this->_cfg_port );
} else {
$this->_conn->connect( $this->_cfg_host );
}
if ( $this->_cfg_pswd ) {
if ( $this->_cfg_user ) {
$this->_conn->auth( [ $this->_cfg_user, $this->_cfg_pswd ] );
} else {
$this->_conn->auth( $this->_cfg_pswd );
}
}
if (defined('Redis::OPT_REPLY_LITERAL')) {
$this->debug_oc( 'Redis set OPT_REPLY_LITERAL' );
$this->_conn->setOption(\Redis::OPT_REPLY_LITERAL, true);
}
if ( $this->_cfg_db ) {
if ( ! $this->_conn->select( $this->_cfg_db ) ) {
$this->debug_oc( 'Database ID is invalid' );
$failed = true;
}
}
$res = $this->_conn->rawCommand('PING');
if ( 'PONG' !== $res ) {
$this->debug_oc( 'Redis resp is wrong: ' . $res );
$failed = true;
}
} catch ( \Throwable $e ) {
$this->debug_oc( 'Redis connect error: ' . $e->getMessage() );
$failed = true;
} finally {
restore_error_handler();
}
} else {
// Connect to Memcached.
if ( $this->_cfg_persistent ) {
$this->_conn = new \Memcached( $this->_get_mem_id() );
// Check memcached persistent connection.
if ( $this->_validate_mem_server() ) {
// error_log( 'Object: _validate_mem_server' );
$this->debug_oc( 'Got persistent ' . $this->_oc_driver . ' connection' );
return true;
}
$this->debug_oc( 'No persistent ' . $this->_oc_driver . ' server list!' );
} else {
// error_log( 'Object: new memcached!' );
$this->_conn = new \Memcached();
}
$this->_conn->addServer( $this->_cfg_host, (int) $this->_cfg_port );
/**
* Add SASL auth.
*
* @since 1.8.1
* @since 2.9.6 Fixed SASL connection @see https://www.litespeedtech.com/support/wiki/doku.php/litespeed_wiki:lsmcd:new_sasl
*/
if ( $this->_cfg_user && $this->_cfg_pswd && method_exists( $this->_conn, 'setSaslAuthData' ) ) {
$this->_conn->setOption( \Memcached::OPT_BINARY_PROTOCOL, true );
$this->_conn->setOption( \Memcached::OPT_COMPRESSION, false );
$this->_conn->setSaslAuthData( $this->_cfg_user, $this->_cfg_pswd );
}
// Check connection.
if ( ! $this->_validate_mem_server() ) {
$failed = true;
}
}
// If failed to connect.
if ( $failed ) {
$this->debug_oc( '❌ Failed to connect ' . $this->_oc_driver . ' server!' );
$this->_conn = null;
$this->_cfg_enabled = false;
! defined( 'LITESPEED_OC_FAILURE' ) && define( 'LITESPEED_OC_FAILURE', true );
// Disable ext OC flag so WP transients fall back to wp_options table.
// After muplugins_loaded, all wp_start_object_cache() calls are done — safe to call directly.
// Before that (early bootstrap), defer via hook to avoid multisite "Cannot redeclare" fatal.
if ( function_exists( 'did_action' ) && did_action( 'muplugins_loaded' ) ) {
litespeed_oc_disable_ext_cache();
} elseif ( function_exists( 'add_action' ) ) {
add_action( 'muplugins_loaded', 'litespeed_oc_disable_ext_cache', -999 );
}
return false;
}
$this->debug_oc( '✅ Connected to ' . $this->_oc_driver . ' server.' );
return true;
}
/**
* Check if the connected memcached host is the one in cfg.
*
* @since 1.8
* @access private
*
* @return bool
*/
private function _validate_mem_server() {
$mem_list = $this->_conn->getStats();
if ( empty( $mem_list ) ) {
return false;
}
foreach ( $mem_list as $k => $v ) {
if ( substr( $k, 0, strlen( $this->_cfg_host ) ) !== $this->_cfg_host ) {
continue;
}
if ( ! empty( $v['pid'] ) || ! empty( $v['curr_connections'] ) ) {
return true;
}
}
return false;
}
/**
* Get memcached unique id to be used for connecting.
*
* @since 1.8
* @access private
*
* @return string
*/
private function _get_mem_id() {
$mem_id = 'litespeed';
if ( is_multisite() ) {
$mem_id .= '_' . get_current_blog_id();
}
return $mem_id;
}
/**
* Get cache.
*
* @since 1.8
* @access public
*
* @param string $key Cache key.
* @param string $group Optional. Cache group name.
* @return mixed|false
*/
public function get( $key, $group = '' ) {
if ( ! $this->_cfg_enabled ) {
return false;
}
if ( ! $this->_can_cache( $group ) ) {
return false;
}
if ( ! $this->_connect() ) {
return false;
}
$res = $this->_conn->get( $key );
return $res;
}
/**
* Set cache.
*
* @since 1.8
* @access public
*
* @param string $key Cache key.
* @param mixed $data Data to store.
* @param int $expire TTL seconds.
* @return bool
*/
public function set( $key, $data, $expire ) {
if ( ! $this->_cfg_enabled ) {
return false;
}
/**
* To fix the Cloud callback cached as its frontend call but the hash is generated in backend
* Bug found by Stan at Jan/10/2020
*/
// if ( ! $this->_can_cache() ) {
// return false;
// }
if ( ! $this->_connect() ) {
return false;
}
// Per WP Object Cache API, expire=0 means "no expiration".
// Key eviction is handled by the cache backend (Redis maxmemory / Memcached LRU).
$ttl = (int) $expire;
if ( 'Redis' === $this->_oc_driver ) {
try {
$options = ( $ttl > 0 ) ? [ 'ex' => $ttl ] : [];
$res = $this->_conn->set( $key, $data, $options );
} catch ( \RedisException $ex ) {
$res = false;
$msg = sprintf( __( 'Redis encountered a fatal error: %1$s (code: %2$d)', 'litespeed-cache' ), $ex->getMessage(), $ex->getCode() );
$this->debug_oc( $msg );
Admin_Display::error( $msg );
}
} else {
$res = $this->_conn->set( $key, $data, $ttl );
}
return $res;
}
/**
* Check if can cache or not.
*
* @since 1.8
* @access private
*
* @param string $group Optional. Cache group name.
* @return bool
*/
private function _can_cache( $group = '' ) {
// Transients always use OC regardless of Cache WP-Admin setting
if ( $this->_is_transients_group( $group ) ) {
return true;
}
if ( ! $this->_cfg_admin && defined( 'WP_ADMIN' ) ) {
return false;
}
return true;
}
/**
* Delete cache.
*
* @since 1.8
* @access public
*
* @param string $key Cache key.
* @return bool
*/
public function delete( $key ) {
if ( ! $this->_cfg_enabled ) {
return false;
}
if ( ! $this->_connect() ) {
return false;
}
if ( 'Redis' === $this->_oc_driver ) {
$res = $this->_conn->del( $key );
} else {
$res = $this->_conn->delete( $key );
}
return (bool) $res;
}
/**
* Clear all cache.
*
* @since 1.8
* @access public
*
* @return bool
*/
public function flush() {
if ( ! $this->_cfg_enabled ) {
$this->debug_oc( 'bypass flushing' );
return false;
}
if ( ! $this->_connect() ) {
return false;
}
$this->debug_oc( 'flush!' );
if ( 'Redis' === $this->_oc_driver ) {
$res = $this->_conn->flushDb();
} else {
$res = $this->_conn->flush();
$this->_conn->resetServerList();
}
return $res;
}
/**
* Add global groups.
*
* @since 1.8
* @access public
*
* @param string|string[] $groups Group(s) to add.
* @return void
*/
public function add_global_groups( $groups ) {
if ( ! is_array( $groups ) ) {
$groups = [ $groups ];
}
$this->_global_groups = array_merge( $this->_global_groups, $groups );
$this->_global_groups = array_unique( $this->_global_groups );
}
/**
* Check if is in global groups or not.
*
* @since 1.8
* @access public
*
* @param string $group Group name.
* @return bool
*/
public function is_global( $group ) {
return in_array( $group, $this->_global_groups, true );
}
/**
* Add non persistent groups.
*
* @since 1.8
* @access public
*
* @param string|string[] $groups Group(s) to add.
* @return void
*/
public function add_non_persistent_groups( $groups ) {
if ( ! is_array( $groups ) ) {
$groups = [ $groups ];
}
$this->_non_persistent_groups = array_merge( $this->_non_persistent_groups, $groups );
$this->_non_persistent_groups = array_unique( $this->_non_persistent_groups );
}
/**
* Check if is in non persistent groups or not.
*
* @since 1.8
* @access public
*
* @param string $group Group name.
* @return bool
*/
public function is_non_persistent( $group ) {
return in_array( $group, $this->_non_persistent_groups, true );
}
}