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.

Method of the class: QueryCache{}

No Hooks.

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->write_to_opcache( $hash, $document ): void;
$hash(string) (required)
The SHA-256 hash to cache under.
$document(DocumentNode) (required)
The parsed AST.

QueryCache::write_to_opcache() code 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();
}