Automattic\WooCommerce\Caching

ObjectCache::set()publicWC 1.0

Add an object to the cache, or update an already cached object.

Method of the class: ObjectCache{}

No Hooks.

Return

true|false. True on success, false on error.

Usage

$ObjectCache = new ObjectCache();
$ObjectCache->set( $object, $id, $expiration ): bool;
$object(object|array) (required)
The object to be cached.
$id(int|string|null)
Id of the object to be cached, if null, get_object_id will be used to get it.
Default: null
$expiration(int)
Expiration of the cached data in seconds from the current time, or DEFAULT_EXPIRATION to use the default value.
Default: self::DEFAULT_EXPIRATION

ObjectCache::set() code WC 8.7.0

public function set( $object, $id = null, int $expiration = self::DEFAULT_EXPIRATION ): bool {
	if ( null === $object ) {
		throw new CacheException( "Can't cache a null value", $this, $id );
	}

	if ( ! is_array( $object ) && ! is_object( $object ) ) {
		throw new CacheException( "Can't cache a non-object, non-array value", $this, $id );
	}

	if ( ! is_string( $id ) && ! is_int( $id ) && ! is_null( $id ) ) {
		throw new CacheException( "Object id must be an int, a string, or null for 'set'", $this, $id );
	}

	$this->verify_expiration_value( $expiration );

	$errors = $this->validate( $object );
	if ( ! is_null( $errors ) ) {
		try {
			$id = $this->get_id_from_object_if_null( $object, $id );
		} catch ( \Throwable $ex ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
			// Nothing else to do, we won't be able to add any significant object id to the CacheException and that's it.
		}

		if ( count( $errors ) === 1 ) {
			throw new CacheException( 'Object validation/serialization failed: ' . $errors[0], $this, $id, $errors );
		} elseif ( ! empty( $errors ) ) {
			throw new CacheException( 'Object validation/serialization failed', $this, $id, $errors );
		}
	}

	$id = $this->get_id_from_object_if_null( $object, $id );

	$this->last_cached_data = $object;
	return $this->get_cache_engine()->cache_object(
		$id,
		$object,
		self::DEFAULT_EXPIRATION === $expiration ? $this->default_expiration : $expiration,
		$this->get_object_type()
	);
}