Skip to main content

Programmatically disable the Persistent Object Cache

Temporarily disable the persistent object cache in PHP

T
Written by Team Pressidium
Updated over 2 weeks ago

A Persistent Object Cache improves page load times by reducing repeated database queries from the application or web server. Pressidium provides an efficient Object Cache out of the box, fully integrated with its multi-adaptive cache engine and backed by Redis. While persistent object caching can deliver significant performance gains, in some cases it may conflict with certain plugins or themes.

Important: This guide is intended for advanced users with PHP experience and familiarity with editing WordPress configuration files. Incorrect changes may cause site issues or downtime.

To debug potential issues, you can temporarily disable persistent object caching by setting the WP_REDIS_DISABLED constant to true in the wp-config.php configuration file. This will prevent the Pressidium object cache drop-in from using Redis to manage object cache entries.

define('WP_REDIS_DISABLED', true);

The following simplified PHP code shows how to conditionally disable the object cache based on the request URL and the HTTP Referer (when doing AJAX requests). The code must be placed in wp-config.php or in a file included from it (via an include , require or similar directive), since it has to execute early in the WordPress bootstrap process.

// wp-config.php
...
if ( file_exists( __DIR__ . "/conditionally-disable-object-cache.php" ) ){
require_once __DIR__ . "/conditionally-disable-object-cache.php";
}

/* That's all, stop editing! Happy publishing. */

<?php

// FILE: conditionally-disable-object-cache.php

// Update this array with the URIs that should not use persistent object caching
$pressidium_object_cache_excluded_uris = array(
'/uri-to-exclude',
'/another-uri-to-exclude',
);

function pressidium_should_disable_object_cache() {
global $pressidium_object_cache_excluded_uris;

if ( ( ( defined( 'WP_CLI' ) && WP_CLI ) || php_sapi_name() === 'cli' ) || ! isset( $_SERVER['REQUEST_URI'] ) ) {
return false;
}

$request_path = rtrim( parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ), '/' );
$request_path = empty( $request_path ) ? '/' : $request_path;

$excluded_uris = array();
foreach ( $pressidium_object_cache_excluded_uris as $uri ) {
$excluded_uris[] = '/' . trim( $uri, '/' );
}

if ( in_array( $request_path, $excluded_uris ) ) {
return true;
}

if ( defined( 'DOING_AJAX' ) && DOING_AJAX
&& isset( $_SERVER['HTTP_REFERER'] )
&& in_array( rtrim( $_SERVER['HTTP_REFERER'], '/' ), $excluded_uris ) ) {
return true;
}

return false;
}

if ( pressidium_should_disable_object_cache() && ! defined( 'WP_REDIS_DISABLED' ) ) {
define( 'WP_REDIS_DISABLED', true );
}

Important: This is a starting point and you will need to adapt the code based on your specific requirements. Strict input checking (e.g. HTTP Refererer can be spoofed or be empty) and additional validations based on the specific use case are strongly recommended.

Additional resources:

Did this answer your question?