函数文档

wp_opcache_invalidate()

💡 云策文档标注

概述

wp_opcache_invalidate() 函数用于尝试清除单个 PHP 文件的 opcode 缓存,无需检查文件扩展名或 OPcache 扩展的可用性,并缓存失效能力以提高性能。

关键要点

  • 函数安全调用,自动处理 OPcache 扩展检查和文件扩展名验证。
  • 参数包括必需的文件路径 $filepath 和可选的强制失效标志 $force。
  • 返回布尔值,表示是否成功失效或无需失效,失败时返回 false。
  • 内部使用静态变量缓存失效能力,基于 opcache_invalidate() 可用性和 opcache.restrict_api 配置。
  • 提供过滤器 wp_opcache_invalidate_file 以控制是否执行失效操作。
  • 相关函数包括 wp_opcache_invalidate_directory() 等,用于目录级缓存清除。
  • 自 WordPress 5.5.0 版本引入。

代码示例

function wp_opcache_invalidate( $filepath, $force = false ) {
    static $can_invalidate = null;

    if ( null === $can_invalidate
        && function_exists( 'opcache_invalidate' )
        && ( ! ini_get( 'opcache.restrict_api' )
            || stripos( realpath( $_SERVER['SCRIPT_FILENAME'] ), ini_get( 'opcache.restrict_api' ) ) === 0 )
    ) {
        $can_invalidate = true;
    }

    if ( ! $can_invalidate ) {
        return false;
    }

    if ( '.php' !== strtolower( substr( $filepath, -4 ) ) ) {
        return false;
    }

    if ( apply_filters( 'wp_opcache_invalidate_file', true, $filepath ) ) {
        return opcache_invalidate( $filepath, $force );
    }

    return false;
}

注意事项

  • 函数仅在 OPcache 扩展可用且配置允许时执行失效操作,否则返回 false。
  • 文件路径必须包含 .php 扩展名,否则函数会提前返回 false。
  • 过滤器 wp_opcache_invalidate_file 可用于自定义失效逻辑,默认值为 true。
  • 性能优化通过缓存 $can_invalidate 实现,避免重复检查。

📄 原文内容

Attempts to clear the opcode cache for an individual PHP file.

Description

This function can be called safely without having to check the file extension or availability of the OPcache extension.

Whether or not invalidation is possible is cached to improve performance.

Parameters

$filepathstringrequired
Path to the file, including extension, for which the opcode cache is to be cleared.
$forcebooloptional
Invalidate even if the modification time is not newer than the file in cache.

Default:false

Return

bool True if opcache was invalidated for $filepath, or there was nothing to invalidate.
False if opcache invalidation is not available, or is disabled via filter.

Source

function wp_opcache_invalidate( $filepath, $force = false ) {
	static $can_invalidate = null;

	/*
	 * Check to see if WordPress is able to run `opcache_invalidate()` or not, and cache the value.
	 *
	 * First, check to see if the function is available to call, then if the host has restricted
	 * the ability to run the function to avoid a PHP warning.
	 *
	 * `opcache.restrict_api` can specify the path for files allowed to call `opcache_invalidate()`.
	 *
	 * If the host has this set, check whether the path in `opcache.restrict_api` matches
	 * the beginning of the path of the origin file.
	 *
	 * `$_SERVER['SCRIPT_FILENAME']` approximates the origin file's path, but `realpath()`
	 * is necessary because `SCRIPT_FILENAME` can be a relative path when run from CLI.
	 *
	 * For more details, see:
	 * - https://www.php.net/manual/en/opcache.configuration.php
	 * - https://www.php.net/manual/en/reserved.variables.server.php
	 * - https://core.trac.wordpress.org/ticket/36455
	 */
	if ( null === $can_invalidate
		&& function_exists( 'opcache_invalidate' )
		&& ( ! ini_get( 'opcache.restrict_api' )
			|| stripos( realpath( $_SERVER['SCRIPT_FILENAME'] ), ini_get( 'opcache.restrict_api' ) ) === 0 )
	) {
		$can_invalidate = true;
	}

	// If invalidation is not available, return early.
	if ( ! $can_invalidate ) {
		return false;
	}

	// Verify that file to be invalidated has a PHP extension.
	if ( '.php' !== strtolower( substr( $filepath, -4 ) ) ) {
		return false;
	}

	/**
	 * Filters whether to invalidate a file from the opcode cache.
	 *
	 * @since 5.5.0
	 *
	 * @param bool   $will_invalidate Whether WordPress will invalidate `$filepath`. Default true.
	 * @param string $filepath        The path to the PHP file to invalidate.
	 */
	if ( apply_filters( 'wp_opcache_invalidate_file', true, $filepath ) ) {
		return opcache_invalidate( $filepath, $force );
	}

	return false;
}

Hooks

apply_filters( ‘wp_opcache_invalidate_file’, bool $will_invalidate, string $filepath )

Filters whether to invalidate a file from the opcode cache.

Changelog

Version Description
5.5.0 Introduced.