Automattic\WooCommerce\Internal\Caches

VersionStringGenerator::store_versionprotectedWC 1.0

Store the version string in cache with a filterable TTL.

Method of the class: VersionStringGenerator{}

Returns

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

Usage

// protected - for code of main (parent) or child class
$result = $this->store_version( $id, $version ): bool;
$id(string) (required)
The ID to store the version string for.
$version(string) (required)
The version string to store.

VersionStringGenerator::store_version() code WC 10.7.0

protected function store_version( string $id, string $version ): bool {
	$cache_key = $this->get_cache_key( $id );

	/**
	 * Filter the TTL for version string cache.
	 *
	 * @param int    $ttl Time to live in seconds. Default 1 day.
	 * @param string $id  The ID.
	 *
	 * @since 10.4.0
	 */
	$ttl = apply_filters( 'woocommerce_version_string_generator_ttl', DAY_IN_SECONDS, $id );
	$ttl = max( 0, (int) $ttl );

	$result = wp_cache_set( $cache_key, $version, self::CACHE_GROUP, $ttl );

	if ( is_bool( $result ) ) {
		return $result;
	}

	// Some object cache implementations may return non-boolean values.
	// Verify the store by reading the value back.
	$stored_value = wp_cache_get( $cache_key, self::CACHE_GROUP );
	if ( $stored_value === $version ) {
		return true;
	}

	// The stored value doesn't match; clean up and report failure.
	if ( false !== $stored_value ) {
		wp_cache_delete( $cache_key, self::CACHE_GROUP );
	}
	return false;
}