WP_Object_Cache::replace() WordPress Method

The WordPress Object Cache is a powerful tool that allows you to store data in memory for quick retrieval. The replace() method allows you to replace an existing object in the cache with a new object. This is useful if you need to update the data in the cache without having to regenerate the entire cache.

WP_Object_Cache::replace( int|string $key, mixed $data, string $group = 'default', int $expire ) #

Replaces the contents in the cache, if contents already exist.


Description

Top ↑

See also


Top ↑

Parameters

$key

(int|string)(Required)What to call the contents in the cache.

$data

(mixed)(Required)The contents to store in the cache.

$group

(string)(Optional) Where to group the cache contents.

Default value: 'default'

$expire

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


Top ↑

Return

(bool) True if contents were replaced, false if original value does not exist.


Top ↑

Source

File: wp-includes/class-wp-object-cache.php

	public function replace( $key, $data, $group = 'default', $expire = 0 ) {
		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 );
	}


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.