Title: cache – Make WordPress Core

---

#  Tag Archives: cache

 [  ](https://profiles.wordpress.org/furi3r/) [furi3r](https://profiles.wordpress.org/furi3r/)
10:19 am _on_ April 29, 2022     
Tags: [6.0 ( 76 )](https://make.wordpress.org/core/tag/6-0/),
cache, [dev-notes ( 621 )](https://make.wordpress.org/core/tag/dev-notes/), [dev-notes-6.0 ( 20 )](https://make.wordpress.org/core/tag/dev-notes-6-0/),
[performance ( 410 )](https://make.wordpress.org/core/tag/performance/)   

# 󠀁[Caching improvements in WordPress 6.0](https://make.wordpress.org/core/2022/04/29/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](http://en.wikipedia.org/wiki/Create,_read,_update_and_delete).)
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).

    ```notranslate
    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 ”.

    ```notranslate
    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).

    ```notranslate
    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](https://core.trac.wordpress.org/ticket/55029).

## Allow runtime cache to be flushed (`wp_cache_flush_runtime`)

As discussed in the Performance issue [#81](https://github.com/WordPress/performance/issues/81)
and Trac [#55080](https://core.trac.wordpress.org/ticket/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/](http://wp-cli.org/) [https://make.wordpress.org/cli/](https://make.wordpress.org/cli/)
are run.

**Example usage of  `wp_cache_flush_runtime()`**

    ```notranslate
    $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](https://profiles.wordpress.org/mxbclang/), _[@milana_cap](https://profiles.wordpress.org/milana_cap/),
[@costdev](https://profiles.wordpress.org/costdev/), [@webcommsat](https://profiles.wordpress.org/webcommsat/),
and [@spacedmonkey](https://profiles.wordpress.org/spacedmonkey/)_ for peer review._

[#6-0](https://make.wordpress.org/core/tag/6-0/), [#cache](https://make.wordpress.org/core/tag/cache/),
[#dev-notes](https://make.wordpress.org/core/tag/dev-notes/), [#dev-notes-6-0](https://make.wordpress.org/core/tag/dev-notes-6-0/),
[#performance](https://make.wordpress.org/core/tag/performance/)

 [  ](https://profiles.wordpress.org/spacedmonkey/) [Jonny Harris](https://profiles.wordpress.org/spacedmonkey/)
11:40 am _on_ August 11, 2020     
Tags: [5.5 ( 87 )](https://make.wordpress.org/core/tag/5-5/),
cache, [dev-notes ( 621 )](https://make.wordpress.org/core/tag/dev-notes/)   

# 󠀁[Introduce wp_cache_get_multiple() in WordPress Core](https://make.wordpress.org/core/2020/08/11/introduce-wp_cache_get_multiple/)󠁿

Many object caching backends, such as [Memcached](https://memcached.org/) and [Redis](https://redis.io/)
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 capabilitycapability
A **capability** is permission to perform one or more types of task. Checking if
a user has a capability is performed by the `current_user_can` function. Each user
of a WordPress site might have some permissions but not others, depending on their
role. For example, users who have the Author role usually have permission to edit
their own posts (the “edit_posts” capability), but not permission to edit other 
users’ posts (the “edit_others_posts” 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](https://github.com/Automattic/advanced-post-cache),
[Memcached Redux](https://wordpress.org/plugins/memcached-redux/) or [Redis Object Cache](https://github.com/ericmann/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 7.4 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](https://core.trac.wordpress.org/ticket/50352)

See full details of the eight year journey to getting this ticket into core can 
be found in Core Trac [#20875](https://core.trac.wordpress.org/ticket/20875).

Props to [justinahinon](https://profiles.wordpress.org/justinahinon/) and [sergeybiryukov](https://profiles.wordpress.org/sergeybiryukov/)
for proofreading.

[#5-5](https://make.wordpress.org/core/tag/5-5/), [#cache](https://make.wordpress.org/core/tag/cache/),
[#dev-notes](https://make.wordpress.org/core/tag/dev-notes/)