WP_Object_Cache::set() WordPress Method

The WP_Object_Cache::set() method is used to add an object to the cache. This is useful for caching data that is expensive to generate, such as the results of a database query. The object will be stored in the cache for the specified number of seconds.

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

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.


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) Not used.


Top ↑

Return

(true) Always returns true.


Top ↑

Source

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

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


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.