Object Cache Pro


Object Cache Pro comes with helper functions to make interacting with the object cache easier.

wp_cache_remember() #

Instead of using wp_cache_get(), checking the value and calling wp_cache_set() every time, use the wp_cache_remember():

$users = wp_cache_remember( 'users', DAY_IN_SECONDS, function () {
return get_users();
}, 'my-plugin' );

// PHP 7.4+
$users = wp_cache_remember( 'users', DAY_IN_SECONDS, fn() => get_users(), 'my-plugin' );

wp_cache_sear() #

Same as wp_cache_remember(), except the key is stored forever.

$comments = wp_cache_sear( 'post:42:comments', function () {
return get_comments( [ 'post_id' => 42 ] );
}, 'my-plugin' );

// PHP 7.4+
$users = wp_cache_sear( 'users', fn() => get_users(), 'my-plugin' );

withoutMutations() #

Sometimes you may want to bypass the serializer and compression and interact with Redis directly. In most cases rawCommand() is good enough, but beware, it does ignore prefixes and cache groups.

global $wp_object_cache;

$wp_object_cache->connection()->rawCommand( 'SET', 'my-counter', 42 );
$wp_object_cache->connection()->rawCommand( 'INCR', 'my-counter' );

For better logging and to preserve the WordPress context, prefixes, groups, use the more versatile the withoutMutations() method.

global $wp_object_cache;

$wp_object_cache->withoutMutations( function () {
$key = $this->id( 'counter', 'my-plugin' );

$this->connection->set( $key, 42 );
$this->connection->incr( $key );

return $this->connection->get( $key );
} );

flushBlog() #

Sometimes you may want to flush the cache for an individual blog in your multisite network and that’s easily done from the blog’s dashboard, when using the flush_network configuration option.

However for more fine-grained, programmatic control, you can use the flushBlog() method, which will clear both, the in-memory runtime cache, as well as the persistent storage (Redis).

global $wp_object_cache;

// Flush only blog 42’s cache
$wp_object_cache->flushBlog( 42, 'site' );

// Flush blog 42’s cache and all all global groups
$wp_object_cache->flushBlog( 42, 'global' );

You may omit the 2nd parameter and default to the flush_network configuration option.

global $wp_object_cache;

// Use the `flush_network` configuration option
$wp_object_cache->flushBlog( 42 );

// If `flush_network` is not configured, and the 2nd parameter is omitted,
// the entire cache will be flushed, equally to calling:
$wp_object_cache->flushBlog( 42, 'all' );

flushMemory() #

In some cases you may want to flush the in-memory runtime cache, you may do so with the flushMemory() method.

global $wp_object_cache;

// Flush the runtime cache and prefetches

deleteFromMemory() #

Removing a single key from the in-memory runtime cache, while leaving the persistent storage (Redis) untouched, can be done using the deleteFromMemory() method.

global $wp_object_cache;

// Remove the key from the in-memory cache
$wp_object_cache->deleteFromMemory( 'key', 'group' );

// Remove the key from Redis and the in-memory cache
wp_cache_delete( 'key', 'group' );