WP Redis v0.3.0: Cache Groups

WP Redis is an object cache drop-in which lets you use Redis as a persistent storage backend. WP Redis v0.3.0, released today, brings you enhanced support for cache groups. For an introduction to object caching, and how persistent object caches can speed up your site, take a read through the WordPress at Scale article.

Bring Forth the Awesome

Why do cache groups matter? Because expiring caches is a famously "hard problem". As a developer, there are a lot of cases where you'd like to purge a group of cached values. However, most persistent object cache drop-ins, including WP Redis prior to v0.3.0, store object cache values in a lossy manner by concatenating `$key` and `$group` together to create a single key: `$wp_object_cache->cache[ $group . ':' . $key ] = $data`. This prevents you from efficiently and reliably purging all keys of a group at once. Cache groups make this easy.

Enable native cache groups by upgrading to WP-Redis v0.3.0 and adding `define( 'WP_REDIS_USE_CACHE_GROUPS', true );` to your `wp-config.php`. Once you've done so, you can take advantage of the new `wp_cache_delete_group( $group )` function to purge all keys of a given cache group. You will also need to manage natural cache expiration via group-wide time-to-live (TTL) settings; when enabled, WP Redis doesn't support setting expiration on individual keys.

An Example

Keeping track of the fruit in your refrigerator using the WordPress object cache? With native cache groups enabled, after you've returned from the grocery store, you can empty your memory with `wp_cache_delete_group( 'fruit' );`. No more `foreach( array( 'apple', 'banana', 'orange' ) as $key ) { wp_cache_delete( $key, 'fruit' ); }` or, worse, not being able to clear your memory because you can't remember which fruit you were trying to remember in the first place.

That's an abstract example, but it's not hard to think of practical use-cases. For instance, purging all the object cache that relates to a specific plugin, or section of a website. Or, purging the object cache for a specific user when a certain action is taken by them. These can be very difficult to manage without being able to leverage cache groups. 

These kinds of scenarios become most pressing when running WordPress at scale—e.g. when "just flush the whole cache" isn't a great answer. This is also when you need a high performance persistent object cache, so we're glad to bring Redis's unique capabilities to answer the call.

How do cache groups work? You might already know `wp_cache_set()` supports four parameters: $key, $data, $group, and $expire. On a standard WordPress install, an object cache value is stored in a structured manner on the `$wp_object_cache` global: `$wp_object_cache->cache[ $group ][ $key ] = $data`The lousy way. With the latest version of the plugin and `define( 'WP_REDIS_USE_CACHE_GROUPS', true );`, we use Redis's native hash data type to maintain this structured representation of the cache.

Supercharge your persistent object cache. Download WP Redis v0.3.0 from WordPress.org. Once you've built something awesome, file a Github issue with questions, feature requests, or comments.

Topics Development, Website Technology, WordPress