class-object-cache.php

Object Cache Manager.

Source includes/Cache/class-object-cache.php12 min readPart of Performance Optimisation

includes/Cache/class-object-cache.php

Object Cache Manager.

Namespace: PerformanceOptimise\\Inc · Lines: 3215

Class Object_Cache

Class Object_Cache

Source: includes/Cache/class-object-cache.php, line 23

class Object_Cache

Tags: @since 1.4.0

Constants

ConstantVisibilityValueLine
DROPIN_MARKERpublic\'Redis Object Cache Drop-in for Performance Optimisation\'30
LEGACY_DROPIN_MARKERpublic\'Redis Object Cache Drop-in\'38
CIRCUIT_OPTIONpublic\'wppo_object_cache_circuit\'51
FAIL_TRANSIENTpublic\'wppo_redis_failures\'60
CIRCUIT_NOTICE_TRANSIENTpublic\'wppo_object_cache_circuit_notice\'69
CIRCUIT_DISMISSED_OPTIONpublic\'wppo_object_cache_circuit_dismissed\'79
PARKED_SUFFIXpublic\'.wppo-disabled\'87
DISABLED_STATE_FILEpublic\'wppo-redis-disabled.json\'95
FAILURES_FILEpublic\'wppo-redis-failures.json\'103
ALLOWED_KEYSpublicRedis_Config_Policy::ALLOWED_KEYS116
CONFIG_HTACCESS_MARKERpublic\'WPPO Redis Config\'128
CONFIG_FILENAMEpublic\'wppo-redis-config.php\'142
NGINX_PROBE_TRANSIENTpublic\'wppo_nginx_config_probe\'157
CONFIG_TMP_SUFFIXpublic\'.tmp\'208
LAST_FAILURE_TRANSIENTpublic\'wppo_redis_last_failure\'2549

Properties

PropertyVisibilityTypeDefaultLine
$nginx_probe_memoprivate staticarray<string,bool>array()171
$circuit_state_memoprivate staticarray|nullnull186
$circuit_state_memo_setprivate staticboolfalse194
$dropin_pathprivatestring—250
$config_pathprivatestring—257
$template_pathprivatestring—264
$outage_bypassedprivate staticboolfalse949
$outage_blog_idprivate staticint0957
$outage_errorprivate static\\WP_Error|nullnull969
$last_flush_errorprivate\\WP_Error|nullnull2680
$own_dropin_memoprivatebool|nullnull2694

publicstatic get_uninstall_sidecar_paths()

public static function get_uninstall_sidecar_paths(): array

Canonical circuit-breaker sidecar paths for uninstall cleanup.

Return: string[] — Absolute paths (empty when WP_CONTENT_DIR is undefined).

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 230

public __construct()

public function __construct()

Constructor function.

Tags: @since 1.4.0 · @since 2.0.0 Filtered drop-in paths are validated for wp-content containment.

Source: includes/Cache/class-object-cache.php, line 272

public get_dropin_path()

public function get_dropin_path(): string

Active drop-in path (containment-validated).

Return: string.

Tags: @since 2.0.0

Source: includes/Cache/class-object-cache.php, line 308

public get_status()

public function get_status()

Retrieve current status of the object cache and Redis connectivity.

Return: array — The status array described above.

Tags: @since 1.4.0 · @since 2.2.0 Added `bypassed`, circuit, `serializers` and `last_failure` keys; fail-open outage short-circuit (at most one reconnect attempt per request per site).

Source: includes/Cache/class-object-cache.php, line 331

publicstatic reset_circuit_memo_for_tests()

public static function reset_circuit_memo_for_tests(): void

Reset the per-request circuit-state memo (unit-test helper).

Return: void.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 459

publicstatic reset_runtime_state()

public static function reset_runtime_state(): void

Reset all per-blog Object Cache runtime memos.

Return: void.

Tags: @since NEXT

Source: includes/Cache/class-object-cache.php, line 470

public get_circuit_state()

public function get_circuit_state(): array

Read the merged circuit-breaker state.

Return: array — Shape { open: bool, tripped_at: int, reason: string, error_code: string, failures: int }.

Tags: @since 2.0.0

Source: includes/Cache/class-object-cache.php, line 490

public auto_disable_circuit()

public function auto_disable_circuit(string=\'\' $reason)

Trip the circuit from fully-booted WordPress: park the drop-in.

ParameterTypeDefaultDescription
$reasonstring=\'\'—Human-readable trip reason.

Return: bool|\\WP_Error — True on success, WP_Error for foreign drop-ins or filesystem failures.

Tags: @since 2.0.0

Source: includes/Cache/class-object-cache.php, line 592

public probe_recovery()

public function probe_recovery()

Probe Redis and close the circuit when it recovers.

Return: bool|\\WP_Error — True when the circuit is closed (or was never open), WP_Error while Redis is still unreachable.

Tags: @since 2.0.0

Source: includes/Cache/class-object-cache.php, line 694

public clear_circuit_state()

public function clear_circuit_state(): void

Clear every circuit-breaker artefact: option, notice transient, failure counter, disabled-state bridge, and parked drop-in sibling.

Return: void.

Tags: @since 2.0.0

Source: includes/Cache/class-object-cache.php, line 732

publicstatic is_own_dropin_content()

public static function is_own_dropin_content($content): bool

Check whether a drop-in file content belongs to this plugin.

ParameterTypeDefaultDescription
$contentmixed—Raw file contents.

Return: bool — True when the content carries this plugin\’s marker.

Tags: @since 2.0.0

Source: includes/Cache/class-object-cache.php, line 772

private is_own_dropin()

private function is_own_dropin(): bool

Whether the installed drop-in carries this plugin\’s marker.

Return: bool — True when the drop-in is ours (or absent), false for foreign files.

Tags: @since 2.0.0

Source: includes/Cache/class-object-cache.php, line 790

private read_json_state_file()

private function read_json_state_file(string $path)

Read a small JSON state file from wp-content.

ParameterTypeDefaultDescription
$pathstring—Absolute file path.

Return: array|null — Decoded array, or null when missing/unreadable/invalid.

Tags: @since 2.0.0

Source: includes/Cache/class-object-cache.php, line 833

private get_parked_path()

private function get_parked_path(): string

Absolute path of the parked drop-in sibling.

Return: string.

Tags: @since 2.0.0

Source: includes/Cache/class-object-cache.php, line 864

private get_disabled_state_path()

private function get_disabled_state_path(): string

Absolute path of the disabled-state bridge file.

Return: string.

Tags: @since 2.0.0

Source: includes/Cache/class-object-cache.php, line 874

private get_failures_path()

private function get_failures_path(): string

Absolute path of the drop-in failure-counter file.

Return: string.

Tags: @since 2.0.0

Source: includes/Cache/class-object-cache.php, line 887

privatestatic restricted_file_mode()

private static function restricted_file_mode(): int

Owner-only file mode for Redis state/config/drop-in writes.

Return: int — File mode (0600 intersected with FS_CHMOD_FILE).

Tags: @since 2.3.0

Source: includes/Cache/class-object-cache.php, line 906

privatestatic restrict_file_mode()

private static function restrict_file_mode($wp_filesystem, string $path): void

Best-effort owner-only chmod on an already-written file.

ParameterTypeDefaultDescription
$wp_filesystemobject—Filesystem instance.
$pathstring—Absolute file path.

Return: void.

Tags: @since 2.3.0

Source: includes/Cache/class-object-cache.php, line 924

privatestatic current_outage_blog_id()

private static function current_outage_blog_id(): int

Resolve the current blog ID for outage-bypass scoping (never fatals).

Return: int — Current blog ID, or 0 when unavailable.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 977

privatestatic sync_outage_scope()

private static function sync_outage_scope(): void

Reset the in-request bypass when the blog changed since arming.

Return: void.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 999

privatestatic arm_in_request_bypass()

private static function arm_in_request_bypass($error): void

Arm the in-request bypass for the current blog.

ParameterTypeDefaultDescription
$error\\WP_Error—Original failure to replay to repeat callers.

Return: void.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 1020

privatestatic is_transient_outage_error()

private static function is_transient_outage_error(string $code, string=\'\' $message): bool

Whether a Redis failure code is a transient outage (vs permanent misconfiguration).

ParameterTypeDefaultDescription
$codestring—Failure code.
$messagestring=\'\'—Failure message (scanned for auth signals).

Return: bool — True when the failure may heal without config changes.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 1041

privatestatic scrub_redis_message()

private static function scrub_redis_message(string $message): string

Scrub a Redis failure/circuit message before admin/REST surfacing.

ParameterTypeDefaultDescription
$messagestring—Raw message.

Return: string — Scrubbed message (max 200 chars).

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 1077

private connect_internal()

private function connect_internal($config)

Internal helper to connect to Redis based on config.

ParameterTypeDefaultDescription
$configarray—Configuration array.

Return: \\Redis|\\RedisCluster|\\WP_Error.

Tags: @since 1.4.0 · @since 2.2.0 Blog-scoped bypass short-circuit (no cross-site leakage after switch_to_blog()).

Source: includes/Cache/class-object-cache.php, line 1113

public wppo_redis_outage_fallback()

public function wppo_redis_outage_fallback(=array() $config)

Single-call Redis outage fallback helper (class-layer fail-open).

ParameterTypeDefaultDescription
$config=array()—Connection configuration (pre-filtered).

Return: \\Redis|\\RedisCluster|\\WP_Error — Connected client, or WP_Error (including `redis_bypassed` while bypassed).

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 1157

publicstatic is_outage_bypassed()

public static function is_outage_bypassed(): bool

Whether the in-request outage bypass is armed (current site).

Return: bool — True when subsequent cache calls short-circuit to uncached in this request.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 1234

publicstatic reset_outage_bypass()

public static function reset_outage_bypass(): void

Reset the in-request outage bypass (tests and explicit recovery).

Return: void.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 1248

public is_outage_flagged()

public function is_outage_flagged(): bool

Whether the persistent outage status flag is set.

Return: bool — True when Redis was recorded as bypassed until recovery.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 1265

private arm_outage_flag()

private function arm_outage_flag(): bool

Arm the persistent outage status flag (additive settings key).

Return: bool — True on the unarmed → armed transition (callers log only then); false when already armed or unavailable.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 1308

private clear_outage_flag()

private function clear_outage_flag(): void

Clear the persistent outage status flag on recovery.

Return: void.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 1357

private trip_circuit_on_outage()

private function trip_circuit_on_outage($error): void

Trip the plugin-side circuit after a transient outage (fail open, never fatal).

ParameterTypeDefaultDescription
$error\\WP_Error—Original transient failure.

Return: void.

Tags: @since 2.3.0

Source: includes/Cache/class-object-cache.php, line 1421

privatestatic ensure_redis_helper()

private static function ensure_redis_helper(string $helper_function): bool

Ensure the Redis connection helper file is loaded and a helper function from it is available.

ParameterTypeDefaultDescription
$helper_functionstring—Helper function name that must exist after loading.

Return: bool — True when the helper function is available.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 1609

privatestatic safe_filesize()

private static function safe_filesize(string $path)

Race-tolerant filesize(): clears the stat cache and suppresses the TOCTOU warning when the file vanishes between checks.

ParameterTypeDefaultDescription
$pathstring—File path.

Return: int|false — Size in bytes or false.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 1628

public get_redis_config()

public function get_redis_config(): array

Get the merged Redis configuration with filter.

Return: array — The Redis configuration.

Tags: @since 2.0.0

Source: includes/Cache/class-object-cache.php, line 1646

public ping()

public function ping(=array() $config)

Ping the Redis server to test connection.

ParameterTypeDefaultDescription
$config=array()—Connection configuration.

Return: bool|\\WP_Error — True if connected, WP_Error on failure.

Tags: @since 1.4.0

Source: includes/Cache/class-object-cache.php, line 1693

publicstatic is_valid_config_content()

public static function is_valid_config_content($contents): bool

Whether rendered Redis config source looks structurally valid.

ParameterTypeDefaultDescription
$contentsmixed—Candidate config source.

Return: bool — True when the source has the expected config shape.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 1797

private write_config_atomic()

private function write_config_atomic(string $content, $wp_filesystem)

Publish Redis config source atomically via tmp-write + verify + rename.

ParameterTypeDefaultDescription
$contentstring—Rendered config PHP source.
$wp_filesystemmixed—Filesystem object from `Util::init_filesystem()`.

Return: bool|\\WP_Error — True on verified publish, WP_Error on any failure.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 1838

private delete_config_tmp_quietly()

private function delete_config_tmp_quietly($wp_filesystem, string $path): void

Best-effort delete of a staging tmp path; never throws.

ParameterTypeDefaultDescription
$wp_filesystemmixed—Filesystem object.
$pathstring—Tmp path to remove.

Return: void.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 2018

private sweep_orphan_config_tmp()

private function sweep_orphan_config_tmp($wp_filesystem): void

Sweep orphan config staging files left by an interrupted write.

ParameterTypeDefaultDescription
$wp_filesystemmixed—Filesystem object.

Return: void.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 2043

private publish_dropin_atomic()

private function publish_dropin_atomic($wp_filesystem)

Publish the object-cache drop-in atomically with a foreign re-check.

ParameterTypeDefaultDescription
$wp_filesystemmixed—Filesystem object from Util::init_filesystem().

Return: bool|\\WP_Error — True on verified publish, WP_Error (foreign_dropin/write_error) on failure.

Tags: @since 2.3.0

Source: includes/Cache/class-object-cache.php, line 2121

private sweep_orphan_dropin_tmp()

private function sweep_orphan_dropin_tmp($wp_filesystem, string=\'\' $current_tmp): void

Sweep orphan object-cache drop-in staging siblings (best-effort, never throws).

ParameterTypeDefaultDescription
$wp_filesystemmixed—Filesystem object.
$current_tmpstring=\'\'—Tmp path just consumed (already moved; skipped when still listed).

Return: void.

Tags: @since 2.3.0

Source: includes/Cache/class-object-cache.php, line 2246

public enable()

public function enable($config)

Install the Redis object-cache drop-in by writing the plugin config and copying the drop-in into place.

ParameterTypeDefaultDescription
$configarray—Connection configuration used to generate the Redis config file.

Return: bool|\\WP_Error — `true` on success, `WP_Error` on failure (possible error codes: `missing_extension`, `foreign_dropin`, `write_error`).

Tags: @since 1.4.0

Source: includes/Cache/class-object-cache.php, line 2291

public disable()

public function disable()

Disable the object cache by deleting the drop-in file.

Return: bool|\\WP_Error — True on success, WP_Error on failure.

Tags: @since 1.4.0

Source: includes/Cache/class-object-cache.php, line 2421

privatestatic protect_config_file()

private static function protect_config_file(): void

Write Apache/LiteSpeed deny rules shielding the redis config file.

Return: void.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 2491

public log_redis_failure()

public function log_redis_failure(string $code, string $message): void

Record a Redis failure in-app (activity log + admin-notice transient).

ParameterTypeDefaultDescription
$codestring—Machine-readable failure code.
$messagestring—Human-readable failure description.

Return: void.

Tags: @since 2.0.0

Source: includes/Cache/class-object-cache.php, line 2564

public get_serializer_support()

public function get_serializer_support(): array

Report which serializers the current phpredis build can safely use.

Return: array — Shape { active: string, igbinary: bool, msgpack: bool, php: bool }.

Tags: @since 2.0.0

Source: includes/Cache/class-object-cache.php, line 2631

public get_last_failure_payload()

public function get_last_failure_payload()

Read the latest recorded Redis failure for admin/REST surfacing.

Return: array|null — Shape { code: string, message: string, time: int } or null.

Tags: @since 2.0.0

Source: includes/Cache/class-object-cache.php, line 2705

public get_last_flush_error()

public function get_last_flush_error()

Latest flush failure, if any.

Return: \\WP_Error|null — The WP_Error set by the last failed flush(), or null.

Tags: @since 2.0.0

Source: includes/Cache/class-object-cache.php, line 2728

public flush()

public function flush()

Flush the complete object cache with best-effort memory-delta logging.

Return: bool — True when flushed, false otherwise.

Tags: @since 1.4.0 · @since 2.0.0 Removed plugin-side re-verification (drop-in verifies with retry); failures exposed via get_last_flush_error().

Source: includes/Cache/class-object-cache.php, line 2749

publicstatic get_config_path()

public static function get_config_path(): string

Absolute path of the Redis config file (\’\’ when undeterminable).

Return: string.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 2799

publicstatic is_nginx_config_exposed()

public static function is_nginx_config_exposed(): bool

Whether the Redis config file is directly fetchable over HTTP on Nginx.

Return: bool — True when the config file looks directly fetchable.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 2845

privatestatic probe_transient_key()

private static function probe_transient_key(string $suffix): string

Blog-prefixed probe transient key with back-compat fallback.

ParameterTypeDefaultDescription
$suffixstring—Key suffix after the base transient name.

Return: string — Prefixed key, or the raw key when Util is unavailable.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 2938

publicstatic clear_nginx_probe_cache()

public static function clear_nginx_probe_cache(): void

Drop the cached Nginx exposure probe verdict.

Return: void.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 2964

public flush_scoped()

public function flush_scoped(): bool

Flush the object cache scoped to the current blog on multisite.

Return: bool — True when the scoped flush succeeded, false otherwise.

Tags: @since 2.2.0

Source: includes/Cache/class-object-cache.php, line 3079

private read_redis_memory_bytes()

private function read_redis_memory_bytes()

Read Redis used_memory in bytes via INFO (best-effort).

Return: int|null — Bytes used, or null when unreachable/unavailable.

Tags: @since 2.0.0

Source: includes/Cache/class-object-cache.php, line 3170

Hooks

Hooks referenced in includes/Cache/class-object-cache.php:

HookTypeLineNotes
wppo_object_cache_dropin_pathfilter274—
wppo_object_cache_configfilter1675@param ×1
wppo_object_cache_configfilter1710@param ×1
wppo_object_cache_configfilter2302—
wppo_nginx_probe_clear_sitesfilter3000@param ×1