WP_Object_Cache::add()publicWP 2.0.0

Adds data to the cache if it doesn't already exist.

Method of the class: WP_Object_Cache{}

No Hooks.

Return

true|false. True on success, false if cache key and group already exist.

Usage

$WP_Object_Cache = new WP_Object_Cache();
$WP_Object_Cache->add( $key, $data, $group, $expire );
$key(int|string) (required)
What to call the contents in the cache.
$data(mixed) (required)
The contents to store in the cache.
$group(string)
Where to group the cache contents.
Default: 'default'
$expire(int)
When to expire the cache contents, in seconds.
Default: 0 (no expiration)

Changelog

Since 2.0.0 Introduced.

WP_Object_Cache::add() code WP 6.5.2

public function add( $key, $data, $group = 'default', $expire = 0 ) {
	if ( wp_suspend_cache_addition() ) {
		return false;
	}

	if ( ! $this->is_valid_key( $key ) ) {
		return false;
	}

	if ( empty( $group ) ) {
		$group = 'default';
	}

	$id = $key;
	if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
		$id = $this->blog_prefix . $key;
	}

	if ( $this->_exists( $id, $group ) ) {
		return false;
	}

	return $this->set( $key, $data, $group, (int) $expire );
}