Automattic\WooCommerce\Caching

ObjectCache::set()publicWC 1.0

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

{} Это метод класса: ObjectCache{}

Возвращает

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

Использование

$ObjectCache = new ObjectCache();
$ObjectCache->set( $id, $object, $expiration ): bool;
$id(int|строка|null)
Id of the object to be cached, if null, get_object_id will be used to get it.
По умолчанию: null
$object(объект|массив) (обязательный)
The object to be cached.
$expiration(int)
Expiration of the cached data in seconds from the current time, or DEFAULT_EXPIRATION to use the default value.
По умолчанию: self::DEFAULT_EXPIRATION

Код ObjectCache::set() WC 6.9.4

public function set( $id = null, $object, 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 );

	if ( null === $id ) {
		$id = $this->get_object_id( $object );
		if ( null === $id ) {
			throw new CacheException( "Null id supplied and the cache class doesn't implement get_object_id", $this );
		}
	}

	$errors = $this->validate( $object );
	if ( null !== $errors && 1 === count( $errors ) ) {
		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 );
	}

	$data = $this->serialize( $object );

	/**
	 * Filters the serialized object that will be passed by an instance of ObjectCache to its cache engine to be cached.
	 *
	 * @since 6.8.0
	 *
	 * @param array $data The already serialized object data.
	 * @param array|object $object The object before serialization.
	 * @returns array The actual serialized object data that will be passed to the cache engine.
	 */
	$data = apply_filters( "woocommerce_after_serializing_{$this->object_type}_for_caching", $data, $object, $id );

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