WP_CLI

FileCache::clean()publicWP-CLI 1.0

Clean cache based on time to live and max size

Method of the class: FileCache{}

No Hooks.

Return

true|false.

Usage

$FileCache = new FileCache();
$FileCache->clean();

FileCache::clean() code WP-CLI 2.8.0-alpha

public function clean() {
	if ( ! $this->enabled ) {
		return false;
	}

	$ttl      = $this->ttl;
	$max_size = $this->max_size;

	// Unlink expired files.
	if ( $ttl > 0 ) {
		try {
			$expire = new DateTime();
			$expire->modify( '-' . $ttl . ' seconds' );

			$finder = $this->get_finder()->date( 'until ' . $expire->format( 'Y-m-d H:i:s' ) );
			foreach ( $finder as $file ) {
				unlink( $file->getRealPath() );
			}
		} catch ( Exception $e ) {
			WP_CLI::error( $e->getMessage() );
		}
	}

	// Unlink older files if max cache size is exceeded.
	if ( $max_size > 0 ) {
		$files = array_reverse( iterator_to_array( $this->get_finder()->sortByAccessedTime()->getIterator() ) );
		$total = 0;

		foreach ( $files as $file ) {
			if ( ( $total + $file->getSize() ) <= $max_size ) {
				$total += $file->getSize();
			} else {
				unlink( $file->getRealPath() );
			}
		}
	}

	return true;
}