templates/object-cache.php
Redis Object Cache Drop-in for Performance Optimisation
Class WP_Object_Cache
WP_Object_Cache class.
class WP_Object_CacheConstants
| Constant | Visibility | Value | Line |
|---|---|---|---|
NON_CIRCUIT_ERROR_CODES | private | array( \'missing_redis\', \'missing_cluster\', \'missing_sentinel\', \'low_nodes\', \'redis_version\' ) | 426 |
Properties
| Property | Visibility | Type | Default | Line |
|---|---|---|---|---|
$cache | private | array | array() | 31 |
$redis | private | \\Redis|\\RedisCluster|null | — | 38 |
$redis_replica | private | \\Redis|null | null | 45 |
$redis_connected | private | bool | false | 52 |
$blog_prefix | public | string | — | 59 |
$salt | private | string | \'\' | 66 |
$no_mc_groups | private | array | array( \'options\', \'alloptions\', \'network_options\' ) | 1372 |
$global_groups | private | array | array( \'image_editor\' ) | 1379 |
public __construct()
public function __construct()Constructor.
public add_salt()
public function add_salt($salt)Adds salt to the cache key prefix. Parameter Type Default Description $saltstring— The salt string to add.
Return: void.
private connect_redis()
private function connect_redis()Initializes and connects the object cache to Redis using the configuration file.
private fail_open_to_memory()
private function fail_open_to_memory(string $error_code, string $reason): voidDegrade to the in-memory fallback after a mid-request Redis error. Parameter Type Default Description $error_codestring— Machine-readable failure code. $reasonstring— Human-readable failure description.
Return: void.
private log_redis_failure_once()
private function log_redis_failure_once(string $message): voidLog a Redis failure at most once per 5 minutes (and once per request). Parameter Type Default Description $messagestring— Failure description.
Return: void.
private log_redis_recovery()
private function log_redis_recovery(): voidLog Redis recovery when a previously failed connection succeeds again.
Return: void.
private get_circuit_threshold()
private function get_circuit_threshold(): intResolve the circuit-breaker failure threshold.
Return: int — Minimum 1.
private get_circuit_window()
private function get_circuit_window(): intResolve the circuit-breaker counting window in seconds.
Return: int — Minimum 1.
private record_redis_failure()
private function record_redis_failure(string $error_code, string $reason): voidRecord one counted Redis failure and trip the breaker at threshold. Parameter Type Default Description $error_codestring— Machine-readable failure code. $reasonstring— Human-readable failure description.
Return: void.
private clear_redis_failures()
private function clear_redis_failures(): voidClear the circuit-breaker failure counter.
Return: void.
private trip_circuit_breaker()
private function trip_circuit_breaker(string $error_code, string $reason, array $state): voidTrip the circuit breaker: park this drop-in so failing Redis stops costing a connection timeout on every request. Parameter Type Default Description $error_codestring— Machine-readable failure code. $reasonstring— Human-readable failure description. $statearray— Counter state { count, first, last }.
Return: void.
private get_key()
private function get_key($key, =\'\' $group)Retrieves the actual key prefixed correctly. Parameter Type Default Description $keystring— Cache key. $group=\'\'— Cache group.
Return: string — Prefix cache key.
public add()
public function add($key, $data, =\'default\' $group, =0 $expire)Adds data to the cache if it doesn\’t already exist. Parameter Type Default Description $keyint|string— Cache key. $datamixed— Cache data. $group=\'default\'— Cache group. $expire=0— Expiration.
Return: bool — True on success.
public set()
public function set($key, $data, =\'default\' $group, =0 $expire)Sets data to the cache. Parameter Type Default Description $keyint|string— Cache key. $datamixed— Cache data. $group=\'default\'— Cache group. $expire=0— Expiration.
Return: bool — True on success.
public get()
public function get($key, =\'default\' $group, =false $force, &=null $found)Gets data from the cache. Parameter Type Default Description $keyint|string— Cache key. $group=\'default\'— Cache group. $force=false— Force from Redis. $found&=null— Result flag.
Return: mixed — False if failed.
public get_multiple()
public function get_multiple($keys, =\'default\' $group, =false $force)Retrieves multiple values from the cache. Parameter Type Default Description $keysarray— Array of keys. $group=\'default\'— Cache group. $force=false— Force from Redis.
Return: array — Array of return values.
public set_multiple()
public function set_multiple($data, =\'default\' $group, =0 $expire)Sets multiple values to the cache. Parameter Type Default Description $dataarray— Array of keys and values. $group=\'default\'— Cache group. $expire=0— Expiration.
Return: bool — True on success.
public delete()
public function delete($key, =\'default\' $group)Deletes data from the cache. Parameter Type Default Description $keyint|string— Cache key. $group=\'default\'— Cache group.
Return: bool — True on success.
public delete_multiple()
public function delete_multiple($keys, =\'default\' $group)Deletes multiple values from the cache. Parameter Type Default Description $keysarray— Array of keys. $group=\'default\'— Cache group.
Return: bool — True on success.
public replace()
public function replace($key, $data, =\'default\' $group, =0 $expire)Replaces existing data. Parameter Type Default Description $keyint|string— Cache key. $datamixed— Cache data. $group=\'default\'— Cache group. $expire=0— Expiration.
Return: bool — True on success.
public flush()
public function flush()Flushes the object cache for this site only.
Return: bool — True when the prefix verifies clean, false otherwise.
private scan_delete_pattern()
private function scan_delete_pattern(string $pattern): voidDelete every key matching a SCAN pattern (blog-prefix namespaced). Parameter Type Default Description $patternstring— SCAN match pattern.
Return: void.
private verify_prefix_flushed()
private function verify_prefix_flushed(string $pattern): boolVerify a flushed prefix with a bounded full-keyspace SCAN. Parameter Type Default Description $patternstring— SCAN match pattern.
Return: bool — True when no keys remain under the pattern.
private scan_pattern_is_empty()
private function scan_pattern_is_empty(string $pattern, =null $node): boolCheck a SCAN pattern is empty via a bounded full-keyspace walk. Parameter Type Default Description $patternstring— SCAN match pattern. $node=null— Cluster node (RedisCluster scan signature), null for standalone.
Return: bool — True when no keys match, or the scan itself failed (fail-open).
public flush_group()
public function flush_group($group)Flushes a specific cache group. Parameter Type Default Description $groupstring— The cache group to flush.
Return: bool — True on success.
public incr()
public function incr($key, =1 $offset, =\'default\' $group)Increases a cached value. Parameter Type Default Description $keyint|string— Cache key. $offset=1— Offset amount. $group=\'default\'— Cache group.
Return: int|bool — The new value or false.
public decr()
public function decr($key, =1 $offset, =\'default\' $group)Decreases a cached value. Parameter Type Default Description $keyint|string— Cache key. $offset=1— Offset amount. $group=\'default\'— Cache group.
Return: int|bool — The new value or false.
public add_global_groups()
public function add_global_groups($groups)Sets the list of global groups. Parameter Type Default Description $groupsarray— Global groups.
Return: void.
public add_non_persistent_groups()
public function add_non_persistent_groups($groups)Sets the list of groups that should not be cached in Redis. Parameter Type Default Description $groupsarray— Non-persistent groups.
Return: void.
public __get()
public function __get($name)Magic getter for backward compatibility. Parameter Type Default Description $namestring— Property name.
Return: mixed.
function wp_cache_add()
function wp_cache_add($key, $data, =\'\' $group, =0 $expire)Adds data to the cache. Parameter Type Default Description $keyint|string— Cache key. $datamixed— Cache data. $group=\'\'— Cache group. $expire=0— Expiration.
Return: bool — True on success.
function wp_cache_set()
function wp_cache_set($key, $data, =\'\' $group, =0 $expire)Sets data to the cache. Parameter Type Default Description $keyint|string— Cache key. $datamixed— Cache data. $group=\'\'— Cache group. $expire=0— Expiration.
Return: bool — True on success.
function wp_cache_get()
function wp_cache_get($key, =\'\' $group, =false $force, &=null $found)Gets data from the cache. Parameter Type Default Description $keyint|string— Cache key. $group=\'\'— Cache group. $force=false— Force from Redis. $found&=null— Result flag.
Return: mixed — False if failed.
function wp_cache_delete()
function wp_cache_delete($key, =\'\' $group)Deletes data from the cache. Parameter Type Default Description $keyint|string— Cache key. $group=\'\'— Cache group.
Return: bool — True on success.
function wp_cache_flush()
function wp_cache_flush()Flushes the object cache.
function wp_cache_flush_group()
function wp_cache_flush_group($group)Flushes a specific cache group. Parameter Type Default Description $groupstring— The cache group to flush.
Return: bool — True on success.
function wp_cache_init()
function wp_cache_init()Initializes the object cache.
function wp_cache_replace()
function wp_cache_replace($key, $data, =\'\' $group, =0 $expire)Replaces existing data. Parameter Type Default Description $keyint|string— Cache key. $datamixed— Cache data. $group=\'\'— Cache group. $expire=0— Expiration.
Return: bool — True on success.
function wp_cache_add_global_groups()
function wp_cache_add_global_groups($groups)Adds global groups. Parameter Type Default Description $groupsarray— Global groups.
function wp_cache_add_non_persistent_groups()
function wp_cache_add_non_persistent_groups($groups)Adds non-persistent groups. Parameter Type Default Description $groupsarray— Non-persistent groups.
function wp_cache_incr()
function wp_cache_incr($key, =1 $offset, =\'\' $group)Increases a cached value. Parameter Type Default Description $keyint|string— Cache key. $offset=1— Offset amount. $group=\'\'— Cache group.
Return: int|bool — The new value or false.
function wp_cache_decr()
function wp_cache_decr($key, =1 $offset, =\'\' $group)Decreases a cached value. Parameter Type Default Description $keyint|string— Cache key. $offset=1— Offset amount. $group=\'\'— Cache group.
Return: int|bool — The new value or false.
function wp_cache_close()
function wp_cache_close()Closes the cache connection on shutdown.
Return: bool — Always true.
function wp_cache_get_multiple()
function wp_cache_get_multiple($keys, =\'\' $group, =false $force)Gets multiple values from the cache. Parameter Type Default Description $keysarray— Array of keys. $group=\'\'— Cache group. $force=false— Force from Redis.
Return: array — Array of return values.
function wp_cache_set_multiple()
function wp_cache_set_multiple($data, =\'\' $group, =0 $expire)Sets multiple values to the cache. Parameter Type Default Description $dataarray— Array of keys and values. $group=\'\'— Cache group. $expire=0— Expiration.
Return: bool — True on success.
function wp_cache_delete_multiple()
function wp_cache_delete_multiple($keys, =\'\' $group)Deletes multiple values from the cache. Parameter Type Default Description $keysarray— Array of keys. $group=\'\'— Cache group.
Return: bool — True on success.
function wp_cache_get_salted()
function wp_cache_get_salted($cache_key, $group, $salt)Retrieves salted data from the cache (WP 6.9+ native override). Parameter Type Default Description $cache_keystring— The cache key used for storage and retrieval. $groupstring— The cache group used for organizing data. $saltstring|string[]— The salt indicating when the cache group was last updated.
Return: mixed|false — The cached data, or false if not found or outdated.
function wp_cache_set_salted()
function wp_cache_set_salted($cache_key, $data, $group, $salt, =0 $expire)Stores salted data in the cache (WP 6.9+ native override). Parameter Type Default Description $cache_keystring— The cache key under which to store the data. $datamixed— The data to be cached. $groupstring— The cache group to which the data belongs. $saltstring|string[]— The salt indicating when the cache group was last updated. $expire=0— When to expire the cache contents, in seconds.
Return: bool — True on success, false on failure.
function wp_cache_get_multiple_salted()
function wp_cache_get_multiple_salted($cache_keys, $group, $salt)Retrieves multiple salted items from the cache (WP 6.9+ native override). Parameter Type Default Description $cache_keysstring[]— Array of cache keys to retrieve. $groupstring— The group of the cache to check. $saltstring|string[]— The salt indicating when the cache group was last updated.
Return: array — Associative array of cache values, keyed by cache key; false when missing or outdated.
function wp_cache_set_multiple_salted()
function wp_cache_set_multiple_salted($data, $group, $salt, =0 $expire)Stores multiple salted items in the cache (WP 6.9+ native override). Parameter Type Default Description $datamixed[]— Associative array of keys and values to store. $groupstring— The group to which the cached data belongs. $saltstring|string[]— The salt indicating when the cache group was last updated. $expire=0— When to expire the cache contents, in seconds.
Return: bool[] — Array of return values keyed by cache key.
function wp_cache_delete_salted()
function wp_cache_delete_salted($cache_key, $group, $salt)Deletes salted data from the cache (WP 6.9+ native override). Parameter Type Default Description $cache_keystring— Cache key. $groupstring— Cache group. $saltstring|string[]— Salt when the group was last updated.
Return: bool — True if deleted, false otherwise.
function wp_cache_delete_multiple_salted()
function wp_cache_delete_multiple_salted($cache_keys, $group, $salt)Deletes multiple salted items from the cache (WP 6.9+ native override). Parameter Type Default Description $cache_keysstring[]— Array of cache keys. $groupstring— Cache group. $saltstring|string[]— Salt when the group was last updated.
Return: bool[] — Array of results keyed by cache key.
function wp_cache_supports()
function wp_cache_supports($feature)Determines whether the object cache implementation supports a particular feature. Parameter Type Default Description $featurestring— The feature to check support for.
Return: bool — True if the feature is supported, false otherwise.
function wp_cache_add_salt()
function wp_cache_add_salt($salt)Adds salt to the cache key prefix (WP 6.9+). Parameter Type Default Description $saltstring— The salt string to add.
Return: void.
Hooks
Hooks referenced in templates/object-cache.php: Hook Type Line Notes wppo_object_cache_circuit_breaker_thresholdfilter 447 @param ×1 wppo_object_cache_circuit_breaker_windowfilter 472 @param ×1