wp_cache_add() WordPress Function

The wp_cache_add() function allows you to add data to the WordPress object cache. This is useful for caching data that is expensive to generate, such as results from a database query. The function takes two arguments: the key to use for the cached data, and the data to be cached. The function returns true if the data was successfully cached, or false if there was an error.

wp_cache_add( int|string $key, mixed $data, string $group = '', int $expire ) #

Adds data to the cache, if the cache key doesn’t already exist.


Description

Top ↑

See also


Top ↑

Parameters

$key

(int|string)(Required)The cache key to use for retrieval later.

$data

(mixed)(Required)The data to add to the cache.

$group

(string)(Optional) The group to add the cache to. Enables the same key to be used across groups.

Default value: ''

$expire

(int)(Optional) When the cache data should expire, in seconds. Default 0 (no expiration).


Top ↑

Return

(bool) True on success, false if cache key and group already exist.


Top ↑

Source

File: wp-includes/cache.php

function wp_cache_add( $key, $data, $group = '', $expire = 0 ) {
	global $wp_object_cache;

	return $wp_object_cache->add( $key, $data, $group, (int) $expire );
}


Top ↑

Changelog

Changelog
VersionDescription
2.0.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.