Helpers

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

  1. wp_cache_remember()
  2. wp_cache_sear()
  3. withoutMutations()
  4. flushBlog()
  5. flushRuntime()
  6. deleteFromMemory()

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 network_flush 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 network_flush configuration option.

global $wp_object_cache;

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

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

flushRuntime()

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

global $wp_object_cache;

// Flush the runtime cache and prefetches
$wp_object_cache->flushMemory();

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' );