Automattic\WooCommerce\Internal\Api

OpcacheFileExpiry::delete_expired_filespublic staticWC 1.0

Delete OPcache cache files older than QueryCache::get_cache_ttl().

AST contents are a pure function of the query, so this is a disk-usage bound, not a correctness concern. Returns the count.

Method of the class: OpcacheFileExpiry{}

No Hooks.

Returns

null. Nothing (null).

Usage

$result = OpcacheFileExpiry::delete_expired_files(): int;

OpcacheFileExpiry::delete_expired_files() code WC 10.9.1

public static function delete_expired_files(): int {
	$dir = QueryCache::get_opcache_cache_dir();
	if ( '' === $dir || ! is_dir( $dir ) ) {
		return 0;
	}

	$fs = ResolverHelpers::wp_filesystem();
	if ( ! $fs ) {
		return 0;
	}

	$files = glob( $dir . '/*.php' );
	if ( false === $files ) {
		return 0;
	}

	$cutoff = time() - QueryCache::get_cache_ttl();
	$count  = 0;
	foreach ( $files as $path ) {
		$mtime = $fs->mtime( $path );
		if ( false !== $mtime && $mtime < $cutoff && $fs->delete( $path ) ) {
			++$count;
		}
	}

	return $count;
}