activation.class.php
8 years ago
api.class.php
8 years ago
cdn.class.php
8 years ago
config.class.php
8 years ago
control.class.php
8 years ago
crawler-sitemap.class.php
8 years ago
crawler.class.php
8 years ago
data.class.php
8 years ago
esi.class.php
8 years ago
gui.class.php
8 years ago
import.class.php
8 years ago
litespeed-cache.class.php
8 years ago
litespeed.autoload.php
8 years ago
log.class.php
8 years ago
media.class.php
8 years ago
object.class.php
8 years ago
object.lib.php
8 years ago
optimize.class.php
8 years ago
optimizer.class.php
8 years ago
purge.class.php
8 years ago
router.class.php
8 years ago
tag.class.php
8 years ago
task.class.php
8 years ago
utility.class.php
8 years ago
vary.class.php
8 years ago
object.class.php
612 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The object cache class |
| 4 | * |
| 5 | * |
| 6 | * @since 1.8 |
| 7 | * @package LiteSpeed_Cache |
| 8 | * @subpackage LiteSpeed_Cache/inc |
| 9 | * @author LiteSpeed Technologies <info@litespeedtech.com> |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * Handle exception |
| 14 | */ |
| 15 | if ( ! function_exists( 'litespeed_exception_handler' ) ) { |
| 16 | function litespeed_exception_handler( $errno, $errstr, $errfile, $errline ) |
| 17 | { |
| 18 | throw new ErrorException($errstr, 0, $errno, $errfile, $errline) ; |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | |
| 23 | class LiteSpeed_Cache_Object |
| 24 | { |
| 25 | private static $_instance ; |
| 26 | |
| 27 | private $_oc_data_file ; |
| 28 | private $_conn ; |
| 29 | private $_cfg_enabled ; |
| 30 | private $_cfg_method ; |
| 31 | private $_cfg_host ; |
| 32 | private $_cfg_port ; |
| 33 | private $_cfg_persistent ; |
| 34 | private $_cfg_admin ; |
| 35 | private $_cfg_transients ; |
| 36 | private $_cfg_db ; |
| 37 | private $_cfg_user ; |
| 38 | private $_cfg_pswd ; |
| 39 | private $_default_life = 360 ; |
| 40 | |
| 41 | private $_oc_driver = 'Memcached' ; // Redis or Memcached |
| 42 | |
| 43 | private $_global_groups ; |
| 44 | private $_non_persistent_groups ; |
| 45 | |
| 46 | /** |
| 47 | * Init |
| 48 | * |
| 49 | * @since 1.8 |
| 50 | * @access private |
| 51 | */ |
| 52 | private function __construct( $cfg = false ) |
| 53 | { |
| 54 | defined( 'LSCWP_LOG' ) && LiteSpeed_Cache_Log::debug2( 'Object: init' ) ; |
| 55 | |
| 56 | $this->_oc_data_file = WP_CONTENT_DIR . '/.object-cache.ini' ; |
| 57 | |
| 58 | if ( $cfg ) { |
| 59 | $this->_cfg_method = $cfg[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_KIND ] ? true : false ; |
| 60 | $this->_cfg_host = $cfg[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_HOST ] ; |
| 61 | $this->_cfg_port = $cfg[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_PORT ] ; |
| 62 | $this->_cfg_life = $cfg[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_LIFE ] ; |
| 63 | $this->_cfg_persistent = $cfg[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_PERSISTENT ] ; |
| 64 | $this->_cfg_admin = $cfg[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_ADMIN ] ; |
| 65 | $this->_cfg_transients = $cfg[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_TRANSIENTS ] ; |
| 66 | $this->_cfg_db = $cfg[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_DB_ID ] ; |
| 67 | $this->_cfg_user = $cfg[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_USER ] ; |
| 68 | $this->_cfg_pswd = $cfg[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_PSWD ] ; |
| 69 | $this->_global_groups = explode( "\n", $cfg[ LiteSpeed_Cache_Config::ITEM_OBJECT_GLOBAL_GROUPS ] ) ; |
| 70 | $this->_non_persistent_groups = explode( "\n", $cfg[ LiteSpeed_Cache_Config::ITEM_OBJECT_NON_PERSISTENT_GROUPS ] ) ; |
| 71 | |
| 72 | if ( $this->_cfg_method ) { |
| 73 | $this->_oc_driver = 'Redis' ; |
| 74 | } |
| 75 | $this->_cfg_enabled = $cfg[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT ] && class_exists( $this->_oc_driver ) && $this->_cfg_host ; |
| 76 | |
| 77 | defined( 'LSCWP_LOG' ) && LiteSpeed_Cache_Log::debug( 'Object: init with cfg result : ', $this->_cfg_enabled ) ; |
| 78 | } |
| 79 | elseif ( class_exists( 'LiteSpeed_Cache' ) ) { |
| 80 | $this->_cfg_method = LiteSpeed_Cache::config( LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_KIND ) ? true : false ; |
| 81 | $this->_cfg_host = LiteSpeed_Cache::config( LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_HOST ) ; |
| 82 | $this->_cfg_port = LiteSpeed_Cache::config( LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_PORT ) ; |
| 83 | $this->_cfg_life = LiteSpeed_Cache::config( LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_LIFE ) ; |
| 84 | $this->_cfg_persistent = LiteSpeed_Cache::config( LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_PERSISTENT ) ; |
| 85 | $this->_cfg_admin = LiteSpeed_Cache::config( LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_ADMIN ) ; |
| 86 | $this->_cfg_transients = LiteSpeed_Cache::config( LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_TRANSIENTS ) ; |
| 87 | $this->_cfg_db = LiteSpeed_Cache::config( LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_DB_ID ) ; |
| 88 | $this->_cfg_user = LiteSpeed_Cache::config( LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_USER ) ; |
| 89 | $this->_cfg_pswd = LiteSpeed_Cache::config( LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_PSWD ) ; |
| 90 | $this->_global_groups = explode( "\n", get_option( LiteSpeed_Cache_Config::ITEM_OBJECT_GLOBAL_GROUPS ) ) ; |
| 91 | $this->_non_persistent_groups = explode( "\n", get_option( LiteSpeed_Cache_Config::ITEM_OBJECT_NON_PERSISTENT_GROUPS ) ) ; |
| 92 | |
| 93 | if ( $this->_cfg_method ) { |
| 94 | $this->_oc_driver = 'Redis' ; |
| 95 | } |
| 96 | $this->_cfg_enabled = LiteSpeed_Cache::config( LiteSpeed_Cache_Config::OPID_CACHE_OBJECT ) && class_exists( $this->_oc_driver ) && $this->_cfg_host ; |
| 97 | } |
| 98 | elseif ( file_exists( $this->_oc_data_file ) ) { // Get cfg from oc_data_file |
| 99 | $cfg = parse_ini_file( $this->_oc_data_file, true ) ; |
| 100 | $this->_cfg_method = ! empty( $cfg[ 'object_cache' ][ 'method' ] ) ? $cfg[ 'object_cache' ][ 'method' ] : false ; |
| 101 | $this->_cfg_host = $cfg[ 'object_cache' ][ 'host' ] ; |
| 102 | $this->_cfg_port = $cfg[ 'object_cache' ][ 'port' ] ; |
| 103 | $this->_cfg_life = ! empty( $cfg[ 'object_cache' ][ 'life' ] ) ? $cfg[ 'object_cache' ][ 'life' ] : $this->_default_life ; |
| 104 | $this->_cfg_persistent = ! empty( $cfg[ 'object_cache' ][ 'persistent' ] ) ? $cfg[ 'object_cache' ][ 'persistent' ] : false ; |
| 105 | $this->_cfg_admin = ! empty( $cfg[ 'object_cache' ][ 'cache_admin' ] ) ? $cfg[ 'object_cache' ][ 'cache_admin' ] : false ; |
| 106 | $this->_cfg_transients = ! empty( $cfg[ 'object_cache' ][ 'cache_transients' ] ) ? $cfg[ 'object_cache' ][ 'cache_transients' ] : false ; |
| 107 | $this->_cfg_db = ! empty( $cfg[ 'object_cache' ][ 'db' ] ) ? $cfg[ 'object_cache' ][ 'db' ] : 0 ; |
| 108 | $this->_cfg_user = ! empty( $cfg[ 'object_cache' ][ 'user' ] ) ? $cfg[ 'object_cache' ][ 'user' ] : '' ; |
| 109 | $this->_cfg_pswd = ! empty( $cfg[ 'object_cache' ][ 'pswd' ] ) ? $cfg[ 'object_cache' ][ 'pswd' ] : '' ; |
| 110 | $this->_global_groups = ! empty( $cfg[ 'object_cache' ][ 'global_groups' ] ) ? explode( ',', $cfg[ 'object_cache' ][ 'global_groups' ] ) : array() ; |
| 111 | $this->_non_persistent_groups = ! empty( $cfg[ 'object_cache' ][ 'non_persistent_groups' ] ) ? explode( ',', $cfg[ 'object_cache' ][ 'non_persistent_groups' ] ) : array() ; |
| 112 | |
| 113 | if ( $this->_cfg_method ) { |
| 114 | $this->_oc_driver = 'Redis' ; |
| 115 | } |
| 116 | $this->_cfg_enabled = class_exists( $this->_oc_driver ) && $this->_cfg_host ; |
| 117 | } |
| 118 | else { |
| 119 | $this->_cfg_enabled = false ; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get `Store Transients` setting value |
| 125 | * |
| 126 | * @since 1.8.3 |
| 127 | * @access public |
| 128 | */ |
| 129 | public function store_transients( $group ) |
| 130 | { |
| 131 | return $this->_cfg_transients && $this->_is_transients_group( $group ) ; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Check if the group belongs to transients or not |
| 136 | * |
| 137 | * @since 1.8.3 |
| 138 | * @access private |
| 139 | */ |
| 140 | private function _is_transients_group( $group ) |
| 141 | { |
| 142 | return in_array( $group, array( 'transient', 'site-transient' ) ) ; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Maintain WP object cache file |
| 147 | * |
| 148 | * @since 1.8 |
| 149 | * @access public |
| 150 | */ |
| 151 | public function update_file( $keep, $options = false ) |
| 152 | { |
| 153 | $wp_file = WP_CONTENT_DIR . '/object-cache.php' ; |
| 154 | $ori_file = LSCWP_DIR . 'lib/object-cache.php' ; |
| 155 | |
| 156 | // To keep file |
| 157 | if ( $keep ) { |
| 158 | // Update data file |
| 159 | $data = "[object_cache]" |
| 160 | . "\nmethod = " . $options[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_KIND ] |
| 161 | . "\nhost = " . $options[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_HOST ] |
| 162 | . "\nport = " . (int) $options[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_PORT ] |
| 163 | . "\nlife = " . $options[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_LIFE ] |
| 164 | . "\nuser = '" . $options[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_USER ] . "'" |
| 165 | . "\npswd = '" . $options[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_PSWD ] . "'" |
| 166 | . "\ndb = " . (int) $options[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_DB_ID ] |
| 167 | . "\npersistent = " . ( $options[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_PERSISTENT ] ? 1 : 0 ) |
| 168 | . "\ncache_admin = " . ( $options[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_ADMIN ] ? 1 : 0 ) |
| 169 | . "\ncache_transients = " . ( $options[ LiteSpeed_Cache_Config::OPID_CACHE_OBJECT_TRANSIENTS ] ? 1 : 0 ) |
| 170 | . "\nglobal_groups = " . implode( ',', explode( "\n", $options[ LiteSpeed_Cache_Config::ITEM_OBJECT_GLOBAL_GROUPS ] ) ) |
| 171 | . "\nnon_persistent_groups = " . implode( ',', explode( "\n", $options[ LiteSpeed_Cache_Config::ITEM_OBJECT_NON_PERSISTENT_GROUPS ] ) ) |
| 172 | ; |
| 173 | Litespeed_File::save( $this->_oc_data_file, $data ) ; |
| 174 | |
| 175 | // Update cls file |
| 176 | if ( ! file_exists( $wp_file ) || md5_file( $wp_file ) !== md5_file( $ori_file ) ) { |
| 177 | defined( 'LSCWP_LOG' ) && LiteSpeed_Cache_Log::debug( 'Object: copying object-cache.php file to ' . $wp_file ) ; |
| 178 | copy( $ori_file, $wp_file ) ; |
| 179 | } |
| 180 | } |
| 181 | else { |
| 182 | // To delete file |
| 183 | if ( file_exists( $wp_file ) ) { |
| 184 | defined( 'LSCWP_LOG' ) && LiteSpeed_Cache_Log::debug( 'Object: removing ' . $wp_file ) ; |
| 185 | unlink( $wp_file ) ; |
| 186 | } |
| 187 | file_exists( $this->_oc_data_file ) && unlink( $this->_oc_data_file ) ; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Remove object cache file |
| 193 | * |
| 194 | * @since 1.8.2 |
| 195 | * @access public |
| 196 | */ |
| 197 | public function del_file() |
| 198 | { |
| 199 | $wp_file = WP_CONTENT_DIR . '/object-cache.php' ; |
| 200 | $ori_file = LSCWP_DIR . 'lib/object-cache.php' ; |
| 201 | |
| 202 | if ( file_exists( $wp_file ) && md5_file( $wp_file ) === md5_file( $ori_file ) ) { |
| 203 | unlink( $wp_file ) ; |
| 204 | } |
| 205 | |
| 206 | file_exists( $this->_oc_data_file ) && unlink( $this->_oc_data_file ) ; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Try to build connection |
| 211 | * |
| 212 | * @since 1.8 |
| 213 | * @access public |
| 214 | */ |
| 215 | public function test_connection() |
| 216 | { |
| 217 | return $this->_connect() ; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Force to connect with this setting |
| 222 | * @return [type] [description] |
| 223 | */ |
| 224 | public function reconnect( $cfg ) |
| 225 | { |
| 226 | defined( 'LSCWP_LOG' ) && LiteSpeed_Cache_Log::debug( 'Object: Reconnecting' ) ; |
| 227 | // error_log( 'Object: reconnect !' ) ; |
| 228 | if ( isset( $this->_conn ) ) { |
| 229 | // error_log( 'Object: Quiting existing connection!' ) ; |
| 230 | defined( 'LSCWP_LOG' ) && LiteSpeed_Cache_Log::debug( 'Object: Quiting existing connection' ) ; |
| 231 | $this->flush() ; |
| 232 | $this->_conn = null ; |
| 233 | self::$_instance = null ; |
| 234 | } |
| 235 | |
| 236 | self::$_instance = new self( $cfg ) ; |
| 237 | self::$_instance->_connect() ; |
| 238 | if ( isset( self::$_instance->_conn ) ) { |
| 239 | self::$_instance->flush() ; |
| 240 | } |
| 241 | |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Connect to Memcached/Redis server |
| 246 | * |
| 247 | * @since 1.8 |
| 248 | * @access private |
| 249 | */ |
| 250 | private function _connect() |
| 251 | { |
| 252 | if ( isset( $this->_conn ) ) { |
| 253 | // error_log( 'Object: _connected' ) ; |
| 254 | return true ; |
| 255 | } |
| 256 | |
| 257 | if ( ! class_exists( $this->_oc_driver ) || ! $this->_cfg_host ) { |
| 258 | return null ; |
| 259 | } |
| 260 | |
| 261 | defined( 'LSCWP_LOG' ) && LiteSpeed_Cache_Log::debug( 'Object: connecting to ' . $this->_cfg_host . ':' . $this->_cfg_port ) ; |
| 262 | |
| 263 | $failed = false ; |
| 264 | /** |
| 265 | * Connect to Redis |
| 266 | * |
| 267 | * @since 1.8.1 |
| 268 | * @see https://github.com/phpredis/phpredis/#example-1 |
| 269 | */ |
| 270 | if ( $this->_oc_driver == 'Redis' ) { |
| 271 | defined( 'LSCWP_LOG' ) && LiteSpeed_Cache_Log::debug( 'Object: Init ' . $this->_oc_driver . ' connection' ) ; |
| 272 | |
| 273 | set_error_handler( 'litespeed_exception_handler' ) ; |
| 274 | try { |
| 275 | $this->_conn = new Redis() ; |
| 276 | // error_log( 'Object: _connect Redis' ) ; |
| 277 | |
| 278 | if ( $this->_cfg_persistent ) { |
| 279 | if ( $this->_cfg_port ) { |
| 280 | $this->_conn->pconnect( $this->_cfg_host, $this->_cfg_port ) ; |
| 281 | } |
| 282 | else { |
| 283 | $this->_conn->pconnect( $this->_cfg_host ) ; |
| 284 | } |
| 285 | } |
| 286 | else { |
| 287 | if ( $this->_cfg_port ) { |
| 288 | $this->_conn->connect( $this->_cfg_host, $this->_cfg_port ) ; |
| 289 | } |
| 290 | else { |
| 291 | $this->_conn->connect( $this->_cfg_host ) ; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | if ( $this->_cfg_pswd ) { |
| 296 | $this->_conn->auth( $this->_cfg_pswd ) ; |
| 297 | } |
| 298 | |
| 299 | if ( $this->_cfg_db ) { |
| 300 | $this->_conn->select( $this->_cfg_db ) ; |
| 301 | } |
| 302 | |
| 303 | $res = $this->_conn->ping() ; |
| 304 | |
| 305 | if ( $res != '+PONG' ) { |
| 306 | $failed = true ; |
| 307 | } |
| 308 | } |
| 309 | catch ( \Exception $e ) { |
| 310 | error_log( $e->getMessage() ) ; |
| 311 | $failed = true ; |
| 312 | } |
| 313 | catch ( ErrorException $e ) { |
| 314 | error_log( $e->getMessage() ) ; |
| 315 | $failed = true ; |
| 316 | } |
| 317 | restore_error_handler() ; |
| 318 | |
| 319 | } |
| 320 | /** |
| 321 | * Connect to Memcached |
| 322 | */ |
| 323 | else { |
| 324 | defined( 'LSCWP_LOG' ) && LiteSpeed_Cache_Log::debug( 'Object: Init ' . $this->_oc_driver . ' connection' ) ; |
| 325 | if ( $this->_cfg_persistent ) { |
| 326 | $this->_conn = new Memcached( $this->_get_mem_id() ) ; |
| 327 | |
| 328 | // Check memcached persistent connection |
| 329 | if ( $this->_validate_mem_server() ) { |
| 330 | // error_log( 'Object: _validate_mem_server' ) ; |
| 331 | defined( 'LSCWP_LOG' ) && LiteSpeed_Cache_Log::debug( 'Object: Got persistent ' . $this->_oc_driver . ' connection' ) ; |
| 332 | return true ; |
| 333 | } |
| 334 | |
| 335 | defined( 'LSCWP_LOG' ) && LiteSpeed_Cache_Log::debug( 'Object: No persistent ' . $this->_oc_driver . ' server list!' ) ; |
| 336 | } |
| 337 | else { |
| 338 | // error_log( 'Object: new memcached!' ) ; |
| 339 | $this->_conn = new Memcached ; |
| 340 | } |
| 341 | |
| 342 | $this->_conn->addServer( $this->_cfg_host, (int) $this->_cfg_port ) ; |
| 343 | |
| 344 | /** |
| 345 | * Add SASL auth |
| 346 | * @since 1.8.1 |
| 347 | */ |
| 348 | if ( $this->_cfg_user && $this->_cfg_pswd && method_exists( $this->_conn, 'setSaslAuthData' ) && ini_get( 'memcached.use_sasl' ) ) { |
| 349 | $this->_conn->setSaslAuthData( $this->_cfg_user, $this->_cfg_pswd ) ; |
| 350 | } |
| 351 | |
| 352 | // Check connection |
| 353 | if ( ! $this->_validate_mem_server() ) { |
| 354 | $failed = true ; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | // If failed to connect |
| 359 | if ( $failed ) { |
| 360 | defined( 'LSCWP_LOG' ) && LiteSpeed_Cache_Log::debug( 'Object: Failed to connect ' . $this->_oc_driver . ' server!' ) ; |
| 361 | $this->_conn = null ; |
| 362 | $this->_cfg_enabled = false ; |
| 363 | // error_log( 'Object: false!' ) ; |
| 364 | return false ; |
| 365 | } |
| 366 | |
| 367 | defined( 'LSCWP_LOG' ) && LiteSpeed_Cache_Log::debug2( 'Object: Connected' ) ; |
| 368 | |
| 369 | return true ; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Check if the connected memcached host is the one in cfg |
| 374 | * |
| 375 | * @since 1.8 |
| 376 | * @access private |
| 377 | */ |
| 378 | private function _validate_mem_server() |
| 379 | { |
| 380 | $mem_list = $this->_conn->getStats() ; |
| 381 | if ( empty( $mem_list ) ) { |
| 382 | return false ; |
| 383 | } |
| 384 | |
| 385 | foreach ( $mem_list as $k => $v ) { |
| 386 | if ( substr( $k, 0, strlen( $this->_cfg_host ) ) != $this->_cfg_host ) { |
| 387 | continue ; |
| 388 | } |
| 389 | if ( $v[ 'pid' ] > 0 ) { |
| 390 | return true ; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | return false ; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Get memcached unique id to be used for connecting |
| 399 | * |
| 400 | * @since 1.8 |
| 401 | * @access private |
| 402 | */ |
| 403 | private function _get_mem_id() |
| 404 | { |
| 405 | $mem_id = 'litespeed' ; |
| 406 | if ( is_multisite() ) { |
| 407 | $mem_id .= '_' . get_current_blog_id() ; |
| 408 | } |
| 409 | |
| 410 | return $mem_id ; |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Get cache |
| 415 | * |
| 416 | * @since 1.8 |
| 417 | * @access public |
| 418 | */ |
| 419 | public function get( $key ) |
| 420 | { |
| 421 | if ( ! $this->_cfg_enabled ) { |
| 422 | return null ; |
| 423 | } |
| 424 | |
| 425 | if ( ! $this->_can_cache() ) { |
| 426 | return null ; |
| 427 | } |
| 428 | |
| 429 | if( ! $this->_connect() ) { |
| 430 | return null ; |
| 431 | } |
| 432 | |
| 433 | // defined( 'LSCWP_LOG' ) && LiteSpeed_Cache_Log::debug2( 'Object: get ' . $key ) ; |
| 434 | |
| 435 | $res = $this->_conn->get( $key ) ; |
| 436 | |
| 437 | return $res ; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Set cache |
| 442 | * |
| 443 | * @since 1.8 |
| 444 | * @access public |
| 445 | */ |
| 446 | public function set( $key, $data, $expire ) |
| 447 | { |
| 448 | if ( ! $this->_cfg_enabled ) { |
| 449 | return null ; |
| 450 | } |
| 451 | |
| 452 | if ( ! $this->_can_cache() ) { |
| 453 | return null ; |
| 454 | } |
| 455 | |
| 456 | if( ! $this->_connect() ) { |
| 457 | return null ; |
| 458 | } |
| 459 | |
| 460 | // defined( 'LSCWP_LOG' ) && LiteSpeed_Cache_Log::debug2( 'Object: set ' . $key ) ; |
| 461 | |
| 462 | // error_log( 'Object: set ' . $key ) ; |
| 463 | |
| 464 | $ttl = $expire ?: $this->_cfg_life ; |
| 465 | |
| 466 | if ( $this->_oc_driver == 'Redis' ) { |
| 467 | $res = $this->_conn->setEx( $key, $ttl, $data ) ; |
| 468 | } |
| 469 | else { |
| 470 | $res = $this->_conn->set( $key, $data, $ttl ) ; |
| 471 | } |
| 472 | |
| 473 | return $res ; |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Check if can cache or not |
| 478 | * |
| 479 | * @since 1.8 |
| 480 | * @access private |
| 481 | */ |
| 482 | private function _can_cache() |
| 483 | { |
| 484 | if ( ! $this->_cfg_admin && defined( 'WP_ADMIN' ) ) { |
| 485 | return false ; |
| 486 | } |
| 487 | return true ; |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Delete cache |
| 492 | * |
| 493 | * @since 1.8 |
| 494 | * @access public |
| 495 | */ |
| 496 | public function delete( $key ) |
| 497 | { |
| 498 | if ( ! $this->_cfg_enabled ) { |
| 499 | return null ; |
| 500 | } |
| 501 | |
| 502 | if( ! $this->_connect() ) { |
| 503 | return null ; |
| 504 | } |
| 505 | |
| 506 | // defined( 'LSCWP_LOG' ) && LiteSpeed_Cache_Log::debug2( 'Object: delete ' . $key ) ; |
| 507 | |
| 508 | $res = $this->_conn->delete( $key ) ; |
| 509 | |
| 510 | return $res ; |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Clear all cache |
| 515 | * |
| 516 | * @since 1.8 |
| 517 | * @access public |
| 518 | */ |
| 519 | public function flush() |
| 520 | { |
| 521 | if ( ! $this->_cfg_enabled ) { |
| 522 | defined( 'LSCWP_LOG' ) && LiteSpeed_Cache_Log::debug( 'Object: bypass flushing' ) ; |
| 523 | return null ; |
| 524 | } |
| 525 | |
| 526 | if( ! $this->_connect() ) { |
| 527 | return null ; |
| 528 | } |
| 529 | |
| 530 | defined( 'LSCWP_LOG' ) && LiteSpeed_Cache_Log::debug( 'Object: flush!' ) ; |
| 531 | |
| 532 | if ( $this->_oc_driver == 'Redis' ) { |
| 533 | $res = $this->_conn->flushDb() ; |
| 534 | } |
| 535 | else { |
| 536 | $res = $this->_conn->flush() ; |
| 537 | $this->_conn->resetServerList() ; |
| 538 | } |
| 539 | |
| 540 | return $res ; |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Add global groups |
| 545 | * |
| 546 | * @since 1.8 |
| 547 | * @access public |
| 548 | */ |
| 549 | public function add_global_groups( $groups ) |
| 550 | { |
| 551 | if ( ! is_array( $groups ) ) { |
| 552 | $groups = array( $groups ) ; |
| 553 | } |
| 554 | |
| 555 | $this->_global_groups = array_merge( $this->_global_groups, $groups ) ; |
| 556 | $this->_global_groups = array_unique( $this->_global_groups ) ; |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Check if is in global groups or not |
| 561 | * |
| 562 | * @since 1.8 |
| 563 | * @access public |
| 564 | */ |
| 565 | public function is_global( $group ) |
| 566 | { |
| 567 | return in_array( $group, $this->_global_groups ) ; |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * Add non persistent groups |
| 572 | * |
| 573 | * @since 1.8 |
| 574 | * @access public |
| 575 | */ |
| 576 | public function add_non_persistent_groups( $groups ) |
| 577 | { |
| 578 | if ( ! is_array( $groups ) ) { |
| 579 | $groups = array( $groups ) ; |
| 580 | } |
| 581 | |
| 582 | $this->_non_persistent_groups = array_merge( $this->_non_persistent_groups, $groups ) ; |
| 583 | $this->_non_persistent_groups = array_unique( $this->_non_persistent_groups ) ; |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Check if is in non persistent groups or not |
| 588 | * |
| 589 | * @since 1.8 |
| 590 | * @access public |
| 591 | */ |
| 592 | public function is_non_persistent( $group ) |
| 593 | { |
| 594 | return in_array( $group, $this->_non_persistent_groups ) ; |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Get the current instance object. |
| 599 | * |
| 600 | * @since 1.8 |
| 601 | * @access public |
| 602 | * @return Current class instance. |
| 603 | */ |
| 604 | public static function get_instance() |
| 605 | { |
| 606 | if ( ! isset( self::$_instance ) ) { |
| 607 | self::$_instance = new self() ; |
| 608 | } |
| 609 | |
| 610 | return self::$_instance ; |
| 611 | } |
| 612 | } |