Automattic\WooCommerce\Internal\Api

QueryCache::get_cached_documentprivateWC 1.0

Retrieve a cached DocumentNode by hash.

Tries OPcache first when enabled and usable, then falls back to the WP object cache. APQ requests pass $for_apq=true so the object cache is consulted regardless of the standard-query toggle, matching the pre-OPcache behaviour where APQ always persisted via the object cache.

Method of the class: QueryCache{}

No Hooks.

Returns

DocumentNode|false.

Usage

// private - for code of main (parent) class only
$result = $this->get_cached_document( $hash, $for_apq );
$hash(string) (required)
The SHA-256 hash.
$for_apq(true|false)
Whether the lookup is for an APQ request.
Default: false

QueryCache::get_cached_document() code WC 10.9.1

private function get_cached_document( string $hash, bool $for_apq = false ) {
	if ( Main::is_opcache_enabled() && $this->is_opcache_usable() ) {
		$doc = $this->read_from_opcache( $hash );
		if ( false !== $doc ) {
			return $doc;
		}
	}

	if ( $for_apq || Main::is_object_cache_enabled() ) {
		$cached = wp_cache_get( $this->build_cache_key( $hash ), self::CACHE_GROUP );
		if ( is_array( $cached ) ) {
			try {
				return AST::fromArray( $cached );
			} catch ( \Throwable $e ) {
				return false;
			}
		}
	}

	return false;
}