Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make prune method more robust against runtime exceptions #20

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use GuzzleHttp\ClientInterface;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Filesystem\FilesystemManager;
use RuntimeException;
use SplFileInfo;
use Symfony\Component\Finder\Finder;

Expand Down Expand Up @@ -220,17 +221,38 @@ public function prune()
$files = Finder::create()
->files()
->ignoreDotFiles(true)
// This will return the least recently accessed files first.
->sortByAccessedTime()
->in($this->config['path'])
->getIterator();

while ($totalSize > $allowedSize && ($file = $files->current())) {
$files = iterator_to_array($files);
// This will return the least recently accessed files first.
// We use a custom sorting function which ignores errors (because files may
// have been deleted in the meantime).
uasort($files, function (SplFileInfo $a, SplFileInfo $b) {
try {
$aTime = $a->getATime();
} catch (RuntimeException $e) {
return 1;
}

try {
$bTime = $b->getATime();
} catch (RuntimeException $e) {
return -1;
}

return $aTime - $bTime;
});

foreach ($files as $file) {
if ($totalSize <= $allowedSize) {
break;
}

$fileSize = $file->getSize();
if ($this->delete($file)) {
$totalSize -= $fileSize;
}
$files->next();
}
}
}
Expand Down
Loading