Caching improvements in WordPress 6.0

As part of the release of WordPress 6.0, the new Performance team has been working on several improvements to the coreCore Core is the set of software required to run WordPress. The Core Development Team builds WordPress.. There are a few new additions to the WordPress Caching APIAPI An API or Application Programming Interface is a software intermediary that allows programs to interact with each other and share data in limited, clearly defined ways..

Batch API methods for Cache Operations (wp_cache_*_multiple)

The function wp_cache_get_multiple() was added in WordPress 5.5. This allowed for multiple cache keys to be collected in just one request. To complete this API, a full CRUDCRUD Create, read, update and delete, the four basic functions of storing data. (More on Wikipedia.) was needed and has been added via the following functions:

  • wp_cache_add_multiple
  • wp_cache_set_multiple
  • wp_cache_delete_multiple

All of these functions accept an array of data to be passed so that multiple cache objects can be created, edited, or deleted in a single cache call.

In WordPress core, these are just wrappers for core functions to allow multiple keys to be passed in one function call, but this would also allow object caching drop-in developers to implement them if their back-end supports it.

Example usage of wp_cache_add_multiple( $data, $group = '', $expire = 0 )

  • $data: Array of key and value pairs to be added.
  • $group: Optional. String. Where the cache contents are grouped. Default ”.
  • $expire: Optional. Int. When to expire the cache in seconds. Default 0 (no expiration).
wp_cache_add_multiple( array( 'foo1' => 'value1', 'foo2' => 'value2' ), 'group1' );

Example usage of wp_cache_delete_multiple( $data, $group = '' )

  • $data: Array of keys to be deleted.
  • $group: Optional. String. Where the cache contents are grouped. Default ”.
wp_cache_delete_multiple( array( 'foo1', 'foo2' ), 'group1' );

Example usage of wp_cache_set_multiple( $data, $group = '', $expire = 0 )

  • $data: Array of key and value pairs to be set.
  • $group: Optional. String. Where the cache contents are grouped. Default ”.
  • $expire: Optional. Int. When to expire the cache in seconds. Default 0 (no expiration).
wp_cache_set_multiple( array( 'foo1' => 'value1', 'foo2' => 'value2' ), 'group1' );

With these additions, some additional core refactoring has been done to utilize these new functions. See more details in TracTrac An open source project by Edgewall Software that serves as a bug tracker and project management tool for WordPress. #55029.

Allow runtime cache to be flushed (wp_cache_flush_runtime)

As discussed in the Performance issue #81 and Trac #55080, Core needed a way to allow users to flush the runtime (in-memory) cache without flushing the entire persistent cache.

This feature was often requested for instances where long-running processes such as Action Scheduler or WP-CLIWP-CLI WP-CLI is the Command Line Interface for WordPress, used to do administrative and development tasks in a programmatic way. The project page is http://wp-cli.org/ https://make.wordpress.org/cli/ are run.

Example usage of  wp_cache_flush_runtime()

$counter = 0;
foreach ( $posts as $post ) {
	wp_insert_post( $post );
	if ( 100 === $counter ) {
		wp_cache_flush_runtime();
		$counter = 0;
	} 
	$counter++;
}

The above example would reset the runtime cache after 100 posts are inserted into the database.

Thanks to @mxbclang, @milana_cap, @costdev, @webcommsat, and @spacedmonkey for peer review.

#6-0, #cache, #dev-notes, #dev-notes-6-0, #performance

Introduce wp_cache_get_multiple() in WordPress Core

Many object caching backends, such as Memcached and Redis support getting multiple values in a single request. Fetching multiple values in one request often results in much faster performance, as it means less requests on an external object cache. However WordPress coreCore Core is the set of software required to run WordPress. The Core Development Team builds WordPress. only supported getting one cache value at a time with the wp_cache_get() function. In WordPress 5.5 a new function was added called wp_cache_get_multiple() with the capability to get multiple cache keys in a single request. The function wp_cache_get_multiple() accepts an array of keys and fetches multiple cache values from the same group.

There are some existing plugins like Advanced Post Cache, Memcached Redux or Redis Object Cache that have already implemented a function called wp_cache_get_multi() that serves a similar purpose to wp_cache_get_multiple(). However this function had a different signature to wp_cache_get_multiple(), as it is allowed an array of keys and groups to be passed, allowing for any cache key to be fetched. To avoid conflicts with numerous plugins that implement the wp_cache_get_multi() function, it was decided to change the name of the function to wp_cache_get_multiple() to stop PHPPHP The web scripting language in which WordPress is primarily architected. WordPress requires PHP 5.6.20 or higher errors and developer confusion.

To start using wp_cache_get_multiple(), creators and maintainers of object caching plugins ( drop-ins ), will need to implement this new function. For plugins that are yet to implement this new function, core will detect that the wp_cache_get_multiple() function does not exist. If it does not exist, then adds a compatibility shim to add the wp_cache_get_multiple() function that simply calls wp_cache_get() internally.

Along with adding this new function, WordPress core has implemented it in the following places.

  • update_object_term_cache()
  • update_meta_cache()
  • _get_non_cached_ids()

See implementation Core TracTrac An open source project by Edgewall Software that serves as a bug tracker and project management tool for WordPress. ticketticket Created for both bug reports and feature development on the bug tracker.: #50352

See full details of the eight year journey to getting this ticket into core can be found in Core Trac #20875.

Props to justinahinon and sergeybiryukov for proofreading.

#5-5, #cache, #dev-notes