WP_Object_Cache::set( int|string $key, mixed $data, string $group = ‘default’, int $expire ): bool

Sets the data contents into the cache.

Description

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.

Parameters

$keyint|stringrequired
What to call the contents in the cache.
$datamixedrequired
The contents to store in the cache.
$groupstringoptional
Where to group the cache contents. Default 'default'.

Default:'default'

$expireintoptional
Not used.

Return

bool True if contents were set, false if key is invalid.

Source

public function set( $key, $data, $group = 'default', $expire = 0 ) {
	if ( ! $this->is_valid_key( $key ) ) {
		return false;
	}

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

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

	if ( is_object( $data ) ) {
		$data = clone $data;
	}

	$this->cache[ $group ][ $key ] = $data;
	return true;
}

Changelog

VersionDescription
6.1.0Returns false if cache key is invalid.
2.0.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.