WP_CLI
FileCache::clean
Clean cache based on time to live and max size
Метод класса: FileCache{}
Хуков нет.
Возвращает
true|false.
Использование
$FileCache = new FileCache(); $FileCache->clean();
Код FileCache::clean() FileCache::clean WP-CLI 2.13.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;
}