As part of the release of WordPress 6.0, the new Performance team has been working on several improvements to the core 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 API 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 CRUD 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 Trac 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-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