object-cache.php

Redis Object Cache Drop-in for Performance Optimisation

Source templates/object-cache.php11 min readPart of Performance Optimisation

templates/object-cache.php

Redis Object Cache Drop-in for Performance Optimisation

Lines: 1821

Class WP_Object_Cache

WP_Object_Cache class.

Source: templates/object-cache.php, line 25

class WP_Object_Cache

Tags: @since 1.4.0

Constants

ConstantVisibilityValueLine
NON_CIRCUIT_ERROR_CODESprivatearray( \'missing_redis\', \'missing_cluster\', \'missing_sentinel\', \'low_nodes\', \'redis_version\' )426

Properties

PropertyVisibilityTypeDefaultLine
$cacheprivatearrayarray()31
$redisprivate\\Redis|\\RedisCluster|null—38
$redis_replicaprivate\\Redis|nullnull45
$redis_connectedprivateboolfalse52
$blog_prefixpublicstring—59
$saltprivatestring\'\'66
$no_mc_groupsprivatearrayarray( \'options\', \'alloptions\', \'network_options\' )1372
$global_groupsprivatearrayarray( \'image_editor\' )1379

public __construct()

public function __construct()

Constructor.

Source: templates/object-cache.php, line 76

public add_salt()

public function add_salt($salt)

Adds salt to the cache key prefix.

ParameterTypeDefaultDescription
$saltstring—The salt string to add.

Return: void.

Tags: @since 2.0.0

Source: templates/object-cache.php, line 143

private connect_redis()

private function connect_redis()

Initializes and connects the object cache to Redis using the configuration file.

Source: templates/object-cache.php, line 158

private fail_open_to_memory()

private function fail_open_to_memory(string $error_code, string $reason): void

Degrade to the in-memory fallback after a mid-request Redis error.

ParameterTypeDefaultDescription
$error_codestring—Machine-readable failure code.
$reasonstring—Human-readable failure description.

Return: void.

Tags: @since 2.2.0

Source: templates/object-cache.php, line 339

private log_redis_failure_once()

private function log_redis_failure_once(string $message): void

Log a Redis failure at most once per 5 minutes (and once per request).

ParameterTypeDefaultDescription
$messagestring—Failure description.

Return: void.

Source: templates/object-cache.php, line 365

private log_redis_recovery()

private function log_redis_recovery(): void

Log Redis recovery when a previously failed connection succeeds again.

Return: void.

Source: templates/object-cache.php, line 397

private get_circuit_threshold()

private function get_circuit_threshold(): int

Resolve the circuit-breaker failure threshold.

Return: int — Minimum 1.

Tags: @since 2.0.0

Source: templates/object-cache.php, line 438

private get_circuit_window()

private function get_circuit_window(): int

Resolve the circuit-breaker counting window in seconds.

Return: int — Minimum 1.

Tags: @since 2.0.0

Source: templates/object-cache.php, line 463

private record_redis_failure()

private function record_redis_failure(string $error_code, string $reason): void

Record one counted Redis failure and trip the breaker at threshold.

ParameterTypeDefaultDescription
$error_codestring—Machine-readable failure code.
$reasonstring—Human-readable failure description.

Return: void.

Tags: @since 2.0.0

Source: templates/object-cache.php, line 494

private clear_redis_failures()

private function clear_redis_failures(): void

Clear the circuit-breaker failure counter.

Return: void.

Tags: @since 2.0.0

Source: templates/object-cache.php, line 584

private trip_circuit_breaker()

private function trip_circuit_breaker(string $error_code, string $reason, array $state): void

Trip the circuit breaker: park this drop-in so failing Redis stops costing a connection timeout on every request.

ParameterTypeDefaultDescription
$error_codestring—Machine-readable failure code.
$reasonstring—Human-readable failure description.
$statearray—Counter state { count, first, last }.

Return: void.

Tags: @since 2.0.0

Source: templates/object-cache.php, line 614

private get_key()

private function get_key($key, =\'\' $group)

Retrieves the actual key prefixed correctly.

ParameterTypeDefaultDescription
$keystring—Cache key.
$group=\'\'—Cache group.

Return: string — Prefix cache key.

Source: templates/object-cache.php, line 694

public add()

public function add($key, $data, =\'default\' $group, =0 $expire)

Adds data to the cache if it doesn\’t already exist.

ParameterTypeDefaultDescription
$keyint|string—Cache key.
$datamixed—Cache data.
$group=\'default\'—Cache group.
$expire=0—Expiration.

Return: bool — True on success.

Source: templates/object-cache.php, line 715

public set()

public function set($key, $data, =\'default\' $group, =0 $expire)

Sets data to the cache.

ParameterTypeDefaultDescription
$keyint|string—Cache key.
$datamixed—Cache data.
$group=\'default\'—Cache group.
$expire=0—Expiration.

Return: bool — True on success.

Source: templates/object-cache.php, line 762

public get()

public function get($key, =\'default\' $group, =false $force, &=null $found)

Gets data from the cache.

ParameterTypeDefaultDescription
$keyint|string—Cache key.
$group=\'default\'—Cache group.
$force=false—Force from Redis.
$found&=null—Result flag.

Return: mixed — False if failed.

Source: templates/object-cache.php, line 801

public get_multiple()

public function get_multiple($keys, =\'default\' $group, =false $force)

Retrieves multiple values from the cache.

ParameterTypeDefaultDescription
$keysarray—Array of keys.
$group=\'default\'—Cache group.
$force=false—Force from Redis.

Return: array — Array of return values.

Source: templates/object-cache.php, line 849

public set_multiple()

public function set_multiple($data, =\'default\' $group, =0 $expire)

Sets multiple values to the cache.

ParameterTypeDefaultDescription
$dataarray—Array of keys and values.
$group=\'default\'—Cache group.
$expire=0—Expiration.

Return: bool — True on success.

Source: templates/object-cache.php, line 918

public delete()

public function delete($key, =\'default\' $group)

Deletes data from the cache.

ParameterTypeDefaultDescription
$keyint|string—Cache key.
$group=\'default\'—Cache group.

Return: bool — True on success.

Source: templates/object-cache.php, line 980

public delete_multiple()

public function delete_multiple($keys, =\'default\' $group)

Deletes multiple values from the cache.

ParameterTypeDefaultDescription
$keysarray—Array of keys.
$group=\'default\'—Cache group.

Return: bool — True on success.

Source: templates/object-cache.php, line 1003

public replace()

public function replace($key, $data, =\'default\' $group, =0 $expire)

Replaces existing data.

ParameterTypeDefaultDescription
$keyint|string—Cache key.
$datamixed—Cache data.
$group=\'default\'—Cache group.
$expire=0—Expiration.

Return: bool — True on success.

Source: templates/object-cache.php, line 1056

public flush()

public function flush()

Flushes the object cache for this site only.

Return: bool — True when the prefix verifies clean, false otherwise.

Tags: @since 2.0.0 Post-flush verification sample added; returns false when stale keys remain. · @since 2.2.0 Multisite scoping note for the object_cache_allow_flush_all filter added.

Source: templates/object-cache.php, line 1094

private scan_delete_pattern()

private function scan_delete_pattern(string $pattern): void

Delete every key matching a SCAN pattern (blog-prefix namespaced).

ParameterTypeDefaultDescription
$patternstring—SCAN match pattern.

Return: void.

Tags: @since 2.0.0

Source: templates/object-cache.php, line 1136

private verify_prefix_flushed()

private function verify_prefix_flushed(string $pattern): bool

Verify a flushed prefix with a bounded full-keyspace SCAN.

ParameterTypeDefaultDescription
$patternstring—SCAN match pattern.

Return: bool — True when no keys remain under the pattern.

Tags: @since 2.0.0

Source: templates/object-cache.php, line 1179

private scan_pattern_is_empty()

private function scan_pattern_is_empty(string $pattern, =null $node): bool

Check a SCAN pattern is empty via a bounded full-keyspace walk.

ParameterTypeDefaultDescription
$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).

Tags: @since 2.0.0

Source: templates/object-cache.php, line 1208

public flush_group()

public function flush_group($group)

Flushes a specific cache group.

ParameterTypeDefaultDescription
$groupstring—The cache group to flush.

Return: bool — True on success.

Source: templates/object-cache.php, line 1241

public incr()

public function incr($key, =1 $offset, =\'default\' $group)

Increases a cached value.

ParameterTypeDefaultDescription
$keyint|string—Cache key.
$offset=1—Offset amount.
$group=\'default\'—Cache group.

Return: int|bool — The new value or false.

Source: templates/object-cache.php, line 1275

public decr()

public function decr($key, =1 $offset, =\'default\' $group)

Decreases a cached value.

ParameterTypeDefaultDescription
$keyint|string—Cache key.
$offset=1—Offset amount.
$group=\'default\'—Cache group.

Return: int|bool — The new value or false.

Source: templates/object-cache.php, line 1306

public add_global_groups()

public function add_global_groups($groups)

Sets the list of global groups.

ParameterTypeDefaultDescription
$groupsarray—Global groups.

Return: void.

Source: templates/object-cache.php, line 1335

public add_non_persistent_groups()

public function add_non_persistent_groups($groups)

Sets the list of groups that should not be cached in Redis.

ParameterTypeDefaultDescription
$groupsarray—Non-persistent groups.

Return: void.

Source: templates/object-cache.php, line 1346

public __get()

public function __get($name)

Magic getter for backward compatibility.

ParameterTypeDefaultDescription
$namestring—Property name.

Return: mixed.

Source: templates/object-cache.php, line 1357

function wp_cache_add()

function wp_cache_add($key, $data, =\'\' $group, =0 $expire)

Adds data to the cache.

ParameterTypeDefaultDescription
$keyint|string—Cache key.
$datamixed—Cache data.
$group=\'\'—Cache group.
$expire=0—Expiration.

Return: bool — True on success.

Source: templates/object-cache.php, line 1396

function wp_cache_set()

function wp_cache_set($key, $data, =\'\' $group, =0 $expire)

Sets data to the cache.

ParameterTypeDefaultDescription
$keyint|string—Cache key.
$datamixed—Cache data.
$group=\'\'—Cache group.
$expire=0—Expiration.

Return: bool — True on success.

Source: templates/object-cache.php, line 1410

function wp_cache_get()

function wp_cache_get($key, =\'\' $group, =false $force, &=null $found)

Gets data from the cache.

ParameterTypeDefaultDescription
$keyint|string—Cache key.
$group=\'\'—Cache group.
$force=false—Force from Redis.
$found&=null—Result flag.

Return: mixed — False if failed.

Source: templates/object-cache.php, line 1424

function wp_cache_delete()

function wp_cache_delete($key, =\'\' $group)

Deletes data from the cache.

ParameterTypeDefaultDescription
$keyint|string—Cache key.
$group=\'\'—Cache group.

Return: bool — True on success.

Source: templates/object-cache.php, line 1436

function wp_cache_flush()

function wp_cache_flush()

Flushes the object cache.

Source: templates/object-cache.php, line 1444

function wp_cache_flush_group()

function wp_cache_flush_group($group)

Flushes a specific cache group.

ParameterTypeDefaultDescription
$groupstring—The cache group to flush.

Return: bool — True on success.

Source: templates/object-cache.php, line 1455

function wp_cache_init()

function wp_cache_init()

Initializes the object cache.

Source: templates/object-cache.php, line 1463

function wp_cache_replace()

function wp_cache_replace($key, $data, =\'\' $group, =0 $expire)

Replaces existing data.

ParameterTypeDefaultDescription
$keyint|string—Cache key.
$datamixed—Cache data.
$group=\'\'—Cache group.
$expire=0—Expiration.

Return: bool — True on success.

Source: templates/object-cache.php, line 1478

function wp_cache_add_global_groups()

function wp_cache_add_global_groups($groups)

Adds global groups.

ParameterTypeDefaultDescription
$groupsarray—Global groups.

Source: templates/object-cache.php, line 1488

function wp_cache_add_non_persistent_groups()

function wp_cache_add_non_persistent_groups($groups)

Adds non-persistent groups.

ParameterTypeDefaultDescription
$groupsarray—Non-persistent groups.

Source: templates/object-cache.php, line 1498

function wp_cache_incr()

function wp_cache_incr($key, =1 $offset, =\'\' $group)

Increases a cached value.

ParameterTypeDefaultDescription
$keyint|string—Cache key.
$offset=1—Offset amount.
$group=\'\'—Cache group.

Return: int|bool — The new value or false.

Source: templates/object-cache.php, line 1511

function wp_cache_decr()

function wp_cache_decr($key, =1 $offset, =\'\' $group)

Decreases a cached value.

ParameterTypeDefaultDescription
$keyint|string—Cache key.
$offset=1—Offset amount.
$group=\'\'—Cache group.

Return: int|bool — The new value or false.

Source: templates/object-cache.php, line 1524

function wp_cache_close()

function wp_cache_close()

Closes the cache connection on shutdown.

Return: bool — Always true.

Source: templates/object-cache.php, line 1538

function wp_cache_get_multiple()

function wp_cache_get_multiple($keys, =\'\' $group, =false $force)

Gets multiple values from the cache.

ParameterTypeDefaultDescription
$keysarray—Array of keys.
$group=\'\'—Cache group.
$force=false—Force from Redis.

Return: array — Array of return values.

Source: templates/object-cache.php, line 1567

function wp_cache_set_multiple()

function wp_cache_set_multiple($data, =\'\' $group, =0 $expire)

Sets multiple values to the cache.

ParameterTypeDefaultDescription
$dataarray—Array of keys and values.
$group=\'\'—Cache group.
$expire=0—Expiration.

Return: bool — True on success.

Source: templates/object-cache.php, line 1580

function wp_cache_delete_multiple()

function wp_cache_delete_multiple($keys, =\'\' $group)

Deletes multiple values from the cache.

ParameterTypeDefaultDescription
$keysarray—Array of keys.
$group=\'\'—Cache group.

Return: bool — True on success.

Source: templates/object-cache.php, line 1592

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).

ParameterTypeDefaultDescription
$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.

Tags: @since 2.0.0

Source: templates/object-cache.php, line 1614

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).

ParameterTypeDefaultDescription
$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.

Tags: @since 2.0.0

Source: templates/object-cache.php, line 1643

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).

ParameterTypeDefaultDescription
$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.

Tags: @since 2.0.0

Source: templates/object-cache.php, line 1669

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).

ParameterTypeDefaultDescription
$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.

Tags: @since 2.0.0

Source: templates/object-cache.php, line 1702

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).

ParameterTypeDefaultDescription
$cache_keystring—Cache key.
$groupstring—Cache group.
$saltstring|string[]—Salt when the group was last updated.

Return: bool — True if deleted, false otherwise.

Tags: @since 2.0.0

Source: templates/object-cache.php, line 1728

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).

ParameterTypeDefaultDescription
$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.

Tags: @since 2.0.0

Source: templates/object-cache.php, line 1749

function wp_cache_supports()

function wp_cache_supports($feature)

Determines whether the object cache implementation supports a particular feature.

ParameterTypeDefaultDescription
$featurestring—The feature to check support for.

Return: bool — True if the feature is supported, false otherwise.

Tags: @since 2.0.0

Source: templates/object-cache.php, line 1775

function wp_cache_add_salt()

function wp_cache_add_salt($salt)

Adds salt to the cache key prefix (WP 6.9+).

ParameterTypeDefaultDescription
$saltstring—The salt string to add.

Return: void.

Tags: @since 2.0.0

Source: templates/object-cache.php, line 1814

Hooks

Hooks referenced in templates/object-cache.php:

HookTypeLineNotes
wppo_object_cache_circuit_breaker_thresholdfilter447@param ×1
wppo_object_cache_circuit_breaker_windowfilter472@param ×1