wp_cache_replace()WP 2.0.0

Replaces/updates the data of the specified cache only if the cache already exists; otherwise, it does nothing (will return false).

The function is similar to wp_cache_set(), but differs in that wp_cache_replace() does nothing if the cache does not yet exist.

One of the caching functions:

wp_cache_add( $key, $data, $group, $expire )
wp_cache_set( $key, $data, $group, $expire )
wp_cache_get( $key, $group )
wp_cache_delete( $key, $group )
wp_cache_replace( $key, $data, $group, $expire )
wp_cache_flush()
wp_cache_add_non_persistent_groups( $groups )
1 time — 0.00012 sec (fast) | 50000 times — 1.23 sec (fast)

No Hooks.

Returns

true|false. false - if the cache does not exist, true - if the data was updated.

Usage

wp_cache_replace( $key, $data, $group, $expire );
$key(int/string) (required)
The cache key.
$data(mixed) (required)
The data to be placed in the specified cache.
$group(string)
The cache group in which to search for the key specified in $key.
Default: ''
$expire(integer)
When the cache will be considered expired. In WP it does not work and is intended for caching plugins.

Examples

0

#1 Usage Example

$main_authors = array( getuserdata(1), get_userdata(10), get_userdata(7) );

$replaced = wp_cache_replace('main_authors', $main_authors, 'authors' );

if( $replaced  ){
	echo 'Done! The data has been replaced.';
}
else {
	echo 'Can`t replace the cache because it doesn`t exist yet.';
}

Notes

Changelog

Since 2.0.0 Introduced.

wp_cache_replace() code WP 6.8.3

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

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