Automattic\WooCommerce\Internal\Api

QueryCache::write_to_opcacheprivateWC 1.0

Persist a parsed AST to the OPcache file backend.

Writes atomically (temp file + rename) so concurrent readers never see a partial file, and explicitly invalidates OPcache for the destination path so installs running with opcache.validate_timestamps=0 still see the new version.

Failures are intentionally silent: the caller already holds a valid DocumentNode, and a failed cache write only forfeits the optimisation for one request.

Метод класса: QueryCache{}

Хуков нет.

Возвращает

null. Ничего (null).

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

// private - только в коде основоного (родительского) класса
$result = $this->write_to_opcache( $hash, $document ): void;
$hash(строка) (обязательный)
The SHA-256 hash to cache under.
$document(DocumentNode) (обязательный)
The parsed AST.

Код QueryCache::write_to_opcache() WC 10.9.1

<?php
private function write_to_opcache( string $hash, DocumentNode $document ): void {
	$dir  = self::get_opcache_cache_dir();
	$path = $dir . '/' . $hash . '.php';
	$tmp  = $path . '.' . bin2hex( random_bytes( 8 ) ) . '.tmp';

	$contents = "<?php\nreturn " . var_export( $document->toArray(), true ) . ";\n"; // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export

	// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
	if ( false === file_put_contents( $tmp, $contents, LOCK_EX ) ) {
		return;
	}

	$fs = ResolverHelpers::wp_filesystem();
	if ( ! $fs || ! $fs->move( $tmp, $path, true ) ) {
		if ( $fs ) {
			$fs->delete( $tmp );
		}
		return;
	}

	if ( function_exists( 'opcache_invalidate' ) ) {
		opcache_invalidate( $path, true );
	}
	if ( function_exists( 'opcache_compile_file' ) ) {
		// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
		@opcache_compile_file( $path );
	}

	OpcacheFileExpiry::ensure_scheduled();
}