| 1 |
<?php |
| 2 |
/** |
| 3 |
* ezCache Redis Object Cache Drop-in |
| 4 |
* |
| 5 |
* Automatically managed by the ezCache plugin. Do not edit manually. |
| 6 |
* |
| 7 |
* @package ezCache Redis Object Cache Drop-in |
| 8 |
*/ |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { exit; } |
| 10 |
|
| 11 |
class WP_Object_Cache { |
| 12 |
private $redis = null; |
| 13 |
private $connected = false; |
| 14 |
private $cache = []; |
| 15 |
private $non_persistent_groups = []; |
| 16 |
public $cache_hits = 0; |
| 17 |
public $cache_misses = 0; |
| 18 |
private $prefix = ''; |
| 19 |
|
| 20 |
public function __construct() { |
| 21 |
$this->prefix = (defined('DB_NAME') ? DB_NAME : 'wp') . ':' . ($GLOBALS['table_prefix'] ?? 'wp_') . ':'; |
| 22 |
$this->connect(); |
| 23 |
} |
| 24 |
|
| 25 |
private function connect() { |
| 26 |
if ( class_exists( 'Redis' ) ) { |
| 27 |
try { |
| 28 |
$this->redis = new Redis(); |
| 29 |
$this->redis->connect( '127.0.0.1', 6379, 1 ); |
| 30 |
$this->connected = true; |
| 31 |
} catch ( Exception $e ) { |
| 32 |
$this->connected = false; |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
private function build_key( $key, $group ) { |
| 38 |
return 'ezcache:obj:' . $this->prefix . ( $group ?: 'default' ) . ':' . $key; |
| 39 |
} |
| 40 |
|
| 41 |
public function add( $key, $data, $group = 'default', $expire = 0 ) { |
| 42 |
if ( $this->get( $key, $group ) !== false ) { return false; } |
| 43 |
return $this->set( $key, $data, $group, $expire ); |
| 44 |
} |
| 45 |
|
| 46 |
public function add_global_groups( $groups ) {} |
| 47 |
public function add_non_persistent_groups( $groups ) { |
| 48 |
$this->non_persistent_groups = array_merge( $this->non_persistent_groups, (array) $groups ); |
| 49 |
} |
| 50 |
|
| 51 |
public function decr( $key, $offset = 1, $group = 'default' ) { |
| 52 |
$val = (int) $this->get( $key, $group ) - $offset; |
| 53 |
$this->set( $key, $val, $group ); |
| 54 |
return $val; |
| 55 |
} |
| 56 |
|
| 57 |
public function delete( $key, $group = 'default' ) { |
| 58 |
$rkey = $this->build_key( $key, $group ); |
| 59 |
unset( $this->cache[ $rkey ] ); |
| 60 |
if ( $this->connected ) { |
| 61 |
try { $this->redis->del( [ $rkey ] ); } catch ( Exception $e ) {} |
| 62 |
} |
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
public function flush() { |
| 67 |
$this->cache = []; |
| 68 |
if ( $this->connected ) { |
| 69 |
try { |
| 70 |
// Iterate with a non-blocking SCAN cursor and delete in small |
| 71 |
// batches, instead of pulling every key into memory with KEYS and |
| 72 |
// issuing one huge DEL — which can exhaust PHP's memory limit (and |
| 73 |
// return a 500) on large sites with many cached objects. |
| 74 |
$pattern = 'ezcache:obj:' . $this->prefix . '*'; |
| 75 |
$use_unlink = method_exists( $this->redis, 'unlink' ); |
| 76 |
$this->redis->setOption( Redis::OPT_SCAN, Redis::SCAN_RETRY ); |
| 77 |
$iterator = null; |
| 78 |
while ( ( $keys = $this->redis->scan( $iterator, $pattern, 500 ) ) !== false ) { |
| 79 |
if ( ! empty( $keys ) ) { |
| 80 |
if ( $use_unlink ) { $this->redis->unlink( $keys ); } |
| 81 |
else { $this->redis->del( $keys ); } |
| 82 |
} |
| 83 |
} |
| 84 |
} catch ( Exception $e ) {} |
| 85 |
} |
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
public function get( $key, $group = 'default', $force = false, &$found = null ) { |
| 90 |
$rkey = $this->build_key( $key, $group ); |
| 91 |
if ( ! $force && array_key_exists( $rkey, $this->cache ) ) { |
| 92 |
$found = true; $this->cache_hits++; |
| 93 |
return is_object( $this->cache[ $rkey ] ) ? clone $this->cache[ $rkey ] : $this->cache[ $rkey ]; |
| 94 |
} |
| 95 |
if ( in_array( $group, $this->non_persistent_groups, true ) || ! $this->connected ) { |
| 96 |
$found = false; $this->cache_misses++; return false; |
| 97 |
} |
| 98 |
try { |
| 99 |
$raw = $this->redis->get( $rkey ); |
| 100 |
if ( $raw === false ) { $found = false; $this->cache_misses++; return false; } |
| 101 |
$data = maybe_unserialize( $raw ); |
| 102 |
$this->cache[ $rkey ] = $data; |
| 103 |
$found = true; $this->cache_hits++; |
| 104 |
return is_object( $data ) ? clone $data : $data; |
| 105 |
} catch ( Exception $e ) { $found = false; $this->cache_misses++; return false; } |
| 106 |
} |
| 107 |
|
| 108 |
public function get_multiple( $keys, $group = 'default', $force = false ) { |
| 109 |
$result = []; |
| 110 |
foreach ( $keys as $key ) { $result[ $key ] = $this->get( $key, $group, $force ); } |
| 111 |
return $result; |
| 112 |
} |
| 113 |
|
| 114 |
public function incr( $key, $offset = 1, $group = 'default' ) { |
| 115 |
$val = (int) $this->get( $key, $group ) + $offset; |
| 116 |
$this->set( $key, $val, $group ); |
| 117 |
return $val; |
| 118 |
} |
| 119 |
|
| 120 |
public function replace( $key, $data, $group = 'default', $expire = 0 ) { |
| 121 |
if ( $this->get( $key, $group ) === false ) { return false; } |
| 122 |
return $this->set( $key, $data, $group, $expire ); |
| 123 |
} |
| 124 |
|
| 125 |
public function reset() { $this->cache = []; } |
| 126 |
|
| 127 |
public function set( $key, $data, $group = 'default', $expire = 0 ) { |
| 128 |
$rkey = $this->build_key( $key, $group ); |
| 129 |
$value = is_object( $data ) ? clone $data : $data; |
| 130 |
$this->cache[ $rkey ] = $value; |
| 131 |
if ( in_array( $group, $this->non_persistent_groups, true ) || ! $this->connected ) { return true; } |
| 132 |
try { |
| 133 |
$s = maybe_serialize( $value ); |
| 134 |
if ( $expire > 0 ) { $this->redis->setex( $rkey, $expire, $s ); } |
| 135 |
else { $this->redis->set( $rkey, $s ); } |
| 136 |
} catch ( Exception $e ) {} |
| 137 |
return true; |
| 138 |
} |
| 139 |
|
| 140 |
public function set_multiple( $data, $group = 'default', $expire = 0 ) { |
| 141 |
$result = []; |
| 142 |
foreach ( $data as $k => $v ) { $result[ $k ] = $this->set( $k, $v, $group, $expire ); } |
| 143 |
return $result; |
| 144 |
} |
| 145 |
|
| 146 |
public function stats() { |
| 147 |
echo '<p><strong>Cache Hits:</strong> ' . $this->cache_hits . '<br />'; |
| 148 |
echo '<strong>Cache Misses:</strong> ' . $this->cache_misses . '</p>'; |
| 149 |
} |
| 150 |
|
| 151 |
public function switch_to_blog( $blog_id ) {} |
| 152 |
public function delete_multiple( $keys, $group = 'default' ) { |
| 153 |
$result = []; |
| 154 |
foreach ( $keys as $key ) { $result[ $key ] = $this->delete( $key, $group ); } |
| 155 |
return $result; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
function wp_cache_add( $key, $data, $group = '', $expire = 0 ) { global $wp_object_cache; return $wp_object_cache->add( $key, $data, $group, $expire ); } |
| 160 |
function wp_cache_add_global_groups( $groups ) { global $wp_object_cache; $wp_object_cache->add_global_groups( $groups ); } |
| 161 |
function wp_cache_add_non_persistent_groups( $groups ) { global $wp_object_cache; $wp_object_cache->add_non_persistent_groups( $groups ); } |
| 162 |
function wp_cache_close() { return true; } |
| 163 |
function wp_cache_decr( $key, $offset = 1, $group = '' ) { global $wp_object_cache; return $wp_object_cache->decr( $key, $offset, $group ); } |
| 164 |
function wp_cache_delete( $key, $group = '' ) { global $wp_object_cache; return $wp_object_cache->delete( $key, $group ); } |
| 165 |
function wp_cache_delete_multiple( $keys, $group = '' ) { global $wp_object_cache; return $wp_object_cache->delete_multiple( $keys, $group ); } |
| 166 |
function wp_cache_flush() { global $wp_object_cache; return $wp_object_cache->flush(); } |
| 167 |
function wp_cache_flush_runtime() { global $wp_object_cache; $wp_object_cache->reset(); return true; } |
| 168 |
function wp_cache_flush_group( $group ) { return true; } |
| 169 |
function wp_cache_get( $key, $group = '', $force = false, &$found = null ) { global $wp_object_cache; return $wp_object_cache->get( $key, $group, $force, $found ); } |
| 170 |
function wp_cache_get_multiple( $keys, $group = '', $force = false ) { global $wp_object_cache; return $wp_object_cache->get_multiple( $keys, $group, $force ); } |
| 171 |
function wp_cache_incr( $key, $offset = 1, $group = '' ) { global $wp_object_cache; return $wp_object_cache->incr( $key, $offset, $group ); } |
| 172 |
function wp_cache_init() { global $wp_object_cache; $wp_object_cache = new WP_Object_Cache(); } |
| 173 |
function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) { global $wp_object_cache; return $wp_object_cache->replace( $key, $data, $group, $expire ); } |
| 174 |
function wp_cache_reset() { global $wp_object_cache; $wp_object_cache->reset(); } |
| 175 |
function wp_cache_set( $key, $data, $group = '', $expire = 0 ) { global $wp_object_cache; return $wp_object_cache->set( $key, $data, $group, $expire ); } |
| 176 |
function wp_cache_set_multiple( $data, $group = '', $expire = 0 ) { global $wp_object_cache; return $wp_object_cache->set_multiple( $data, $group, $expire ); } |
| 177 |
function wp_cache_switch_to_blog( $blog_id ) { global $wp_object_cache; $wp_object_cache->switch_to_blog( $blog_id ); } |
| 178 |
|