* Object Cache API: WP_Object_Cache class
* Core class that implements an object cache.
* The WordPress Object Cache is used to save on trips to the database. The
* Object Cache stores all of the cache data to memory and makes the cache
* contents available by using a key, which is used to name and later retrieve
* The Object Cache can be replaced by other caching mechanisms by placing files
* in the wp-content folder which is looked at in wp-settings. If that file
* exists, then this file will not be included.
#[AllowDynamicProperties]
* Holds the cached objects.
private $cache = array();
* The amount of times the cache data was already stored in the cache.
* Amount of times the cache did not have the request in cache.
public $cache_misses = 0;
* List of global cache groups.
protected $global_groups = array();
* The blog prefix to prepend to keys in non-global groups.
* Holds the value of is_multisite().
* Sets up object properties.
public function __construct() {
$this->multisite = is_multisite();
$this->blog_prefix = $this->multisite ? get_current_blog_id() . ':' : '';
* Makes private properties readable for backward compatibility.
* @param string $name Property to get.
* @return mixed Property.
public function __get( $name ) {
* Makes private properties settable for backward compatibility.
* @param string $name Property to set.
* @param mixed $value Property value.
public function __set( $name, $value ) {
* Makes private properties checkable for backward compatibility.
* @param string $name Property to check if set.
* @return bool Whether the property is set.
public function __isset( $name ) {
return isset( $this->$name );
* Makes private properties un-settable for backward compatibility.
* @param string $name Property to unset.
public function __unset( $name ) {
* Serves as a utility function to determine whether a key is valid.
* @param int|string $key Cache key to check for validity.
* @return bool Whether the key is valid.
protected function is_valid_key( $key ) {
if ( is_string( $key ) && trim( $key ) !== '' ) {
if ( ! function_exists( '__' ) ) {
wp_load_translations_early();
$message = is_string( $key )
? __( 'Cache key must not be an empty string.' )
/* translators: %s: The type of the given cache key. */
: sprintf( __( 'Cache key must be an integer or a non-empty string, %s given.' ), $type );
sprintf( '%s::%s', __CLASS__, debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 )[1]['function'] ),
* Serves as a utility function to determine whether a key exists in the cache.
* @param int|string $key Cache key to check for existence.
* @param string $group Cache group for the key existence check.
* @return bool Whether the key exists in the cache for the given group.
protected function _exists( $key, $group ) {
return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) );
* Adds data to the cache if it doesn't already exist.
* @uses WP_Object_Cache::_exists() Checks to see if the cache already has data.
* @uses WP_Object_Cache::set() Sets the data after the checking the cache
* @param int|string $key What to call the contents in the cache.
* @param mixed $data The contents to store in the cache.
* @param string $group Optional. Where to group the cache contents. Default 'default'.
* @param int $expire Optional. When to expire the cache contents, in seconds.
* Default 0 (no expiration).
* @return bool True on success, false if cache key and group already exist.
public function add( $key, $data, $group = 'default', $expire = 0 ) {
if ( wp_suspend_cache_addition() ) {
if ( ! $this->is_valid_key( $key ) ) {
if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
$id = $this->blog_prefix . $key;
if ( $this->_exists( $id, $group ) ) {
return $this->set( $key, $data, $group, (int) $expire );
* Adds multiple values to the cache in one call.
* @param array $data Array of keys and values to be added.
* @param string $group Optional. Where the cache contents are grouped. Default empty.
* @param int $expire Optional. When to expire the cache contents, in seconds.
* Default 0 (no expiration).
* @return bool[] Array of return values, grouped by key. Each value is either
* true on success, or false if cache key and group already exist.
public function add_multiple( array $data, $group = '', $expire = 0 ) {
foreach ( $data as $key => $value ) {
$values[ $key ] = $this->add( $key, $value, $group, $expire );
* Replaces the contents in the cache, if contents already exist.
* @see WP_Object_Cache::set()
* @param int|string $key What to call the contents in the cache.
* @param mixed $data The contents to store in the cache.
* @param string $group Optional. Where to group the cache contents. Default 'default'.
* @param int $expire Optional. When to expire the cache contents, in seconds.
* Default 0 (no expiration).
* @return bool True if contents were replaced, false if original value does not exist.
public function replace( $key, $data, $group = 'default', $expire = 0 ) {
if ( ! $this->is_valid_key( $key ) ) {
if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
$id = $this->blog_prefix . $key;
if ( ! $this->_exists( $id, $group ) ) {
return $this->set( $key, $data, $group, (int) $expire );
* Sets the data contents into the cache.
* The cache contents are grouped by the $group parameter followed by the
* $key. This allows for duplicate IDs in unique groups. Therefore, naming of
* the group should be used with care and should follow normal function
* naming guidelines outside of core WordPress usage.
* The $expire parameter is not used, because the cache will automatically
* expire for each time a page is accessed and PHP finishes. The method is
* more for cache plugins which use files.
* @since 6.1.0 Returns false if cache key is invalid.
* @param int|string $key What to call the contents in the cache.
* @param mixed $data The contents to store in the cache.
* @param string $group Optional. Where to group the cache contents. Default 'default'.
* @param int $expire Optional. Not used.
* @return bool True if contents were set, false if key is invalid.
public function set( $key, $data, $group = 'default', $expire = 0 ) {
if ( ! $this->is_valid_key( $key ) ) {
if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
$key = $this->blog_prefix . $key;
if ( is_object( $data ) ) {
$this->cache[ $group ][ $key ] = $data;
* Sets multiple values to the cache in one call.
* @param array $data Array of key and value to be set.
* @param string $group Optional. Where the cache contents are grouped. Default empty.
* @param int $expire Optional. When to expire the cache contents, in seconds.
* Default 0 (no expiration).
* @return bool[] Array of return values, grouped by key. Each value is always true.
public function set_multiple( array $data, $group = '', $expire = 0 ) {
foreach ( $data as $key => $value ) {
$values[ $key ] = $this->set( $key, $value, $group, $expire );
* Retrieves the cache contents, if it exists.
* The contents will be first attempted to be retrieved by searching by the
* key in the cache group. If the cache is hit (success) then the contents
* On failure, the number of cache misses will be incremented.
* @param int|string $key The key under which the cache contents are stored.
* @param string $group Optional. Where the cache contents are grouped. Default 'default'.
* @param bool $force Optional. Unused. Whether to force an update of the local cache
* from the persistent cache. Default false.
* @param bool|null $found Optional. Whether the key was found in the cache (passed by reference).
* Disambiguates a return of false, a storable value. Default null.
* @return mixed|false The cache contents on success, false on failure to retrieve contents.
public function get( $key, $group = 'default', $force = false, &$found = null ) {
if ( ! $this->is_valid_key( $key ) ) {
if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
$key = $this->blog_prefix . $key;
if ( $this->_exists( $key, $group ) ) {
if ( is_object( $this->cache[ $group ][ $key ] ) ) {
return clone $this->cache[ $group ][ $key ];
return $this->cache[ $group ][ $key ];
$this->cache_misses += 1;
* Retrieves multiple values from the cache in one call.
* @param array $keys Array of keys under which the cache contents are stored.
* @param string $group Optional. Where the cache contents are grouped. Default 'default'.
* @param bool $force Optional. Whether to force an update of the local cache
* from the persistent cache. Default false.
* @return array Array of return values, grouped by key. Each value is either
* the cache contents on success, or false on failure.
public function get_multiple( $keys, $group = 'default', $force = false ) {
foreach ( $keys as $key ) {
$values[ $key ] = $this->get( $key, $group, $force );
* Removes the contents of the cache key in the group.
* If the cache key does not exist in the group, then nothing will happen.
* @param int|string $key What the contents in the cache are called.
* @param string $group Optional. Where the cache contents are grouped. Default 'default'.
* @param bool $deprecated Optional. Unused. Default false.
* @return bool True on success, false if the contents were not deleted.
public function delete( $key, $group = 'default', $deprecated = false ) {
if ( ! $this->is_valid_key( $key ) ) {
if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
$key = $this->blog_prefix . $key;
if ( ! $this->_exists( $key, $group ) ) {
unset( $this->cache[ $group ][ $key ] );
* Deletes multiple values from the cache in one call.
* @param array $keys Array of keys to be deleted.
* @param string $group Optional. Where the cache contents are grouped. Default empty.
* @return bool[] Array of return values, grouped by key. Each value is either
* true on success, or false if the contents were not deleted.
public function delete_multiple( array $keys, $group = '' ) {
foreach ( $keys as $key ) {
$values[ $key ] = $this->delete( $key, $group );
* Increments numeric cache item's value.
* @param int|string $key The cache key to increment.
* @param int $offset Optional. The amount by which to increment the item's value.
* @param string $group Optional. The group the key is in. Default 'default'.
* @return int|false The item's new value on success, false on failure.
public function incr( $key, $offset = 1, $group = 'default' ) {
if ( ! $this->is_valid_key( $key ) ) {
if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
$key = $this->blog_prefix . $key;
if ( ! $this->_exists( $key, $group ) ) {
if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) {
$this->cache[ $group ][ $key ] = 0;
$this->cache[ $group ][ $key ] += $offset;