arifhp86/laravel-clear-expired-cache-file

删除laravel过期的缓存文件/文件夹

v1.0.1 2024-04-20 15:14 UTC

This package is auto-updated.

Last update: 2024-09-20 16:07:50 UTC


README

Latest Stable Version MIT Licensed Total Downloads

当你使用file缓存驱动程序时,你可能已经注意到缓存文件永远不会被自动删除。在刷新缓存时它们会被覆盖,这是好的,但如果你的缓存键有随机性,或者由于其他原因某些缓存文件再也没有被触及,它们将永远留在你的服务器上。这个包可以帮助你清除这些滞留的文件。

这个包只添加了一个 artisan 命令:php artisan cache:clear-expired,它将删除所有已经过期的缓存文件。

安装

你可以通过 composer 安装这个包

composer require arifhp86/laravel-clear-expired-cache-file

用法

运行以下命令

php artisan cache:clear-expired

定时运行

你也可以安排命令自动运行。例如,如果你想每天凌晨1点运行这个命令,你可以在你的 app/Console/Kernel.php 文件中添加以下内容

protected function schedule(Schedule $schedule)
{
    $schedule->command('cache:clear-expired')->dailyAt('01:00');
}

干燥运行

你可以以干燥运行模式运行命令。在这个模式下,命令不会删除任何文件。它只会显示如果运行命令不使用 --dry-run 选项将会被删除的文件。

php artisan cache:clear-expired --dry-run

禁用目录删除

默认情况下,命令会在删除过期文件后删除所有空目录。你可以通过使用 --disable-directory-delete 选项来禁用此行为。

php artisan cache:clear-expired --disable-directory-delete

事件

命令触发三个事件

  • Arifhp86\ClearExpiredCacheFile\Events\GarbageCollectionStarting:在命令运行之前。
  • Arifhp86\ClearExpiredCacheFile\Events\GarbageCollectionEnded:在命令运行之后。
  • Arifhp86\ClearExpiredCacheFile\Events\GarbageCollectionFailed:如果命令失败。

你可以使用这些事件在命令运行前后执行自己的代码。

在命令运行后发送电子邮件通知

// app/Providers/EventServiceProvider.php boot method

use Arifhp86\ClearExpiredCacheFile\Events\GarbageCollectionEnded;

Event::listen(function (GarbageCollectionEnded $event) {
    $timeTaken = $event->time; // in seconds
    $memory = $event->memory; // in bytes
    $numberOfFilesDeleted = $event->expiredFiles->getCount();
    $diskCleared = $event->expiredFiles->getFormattedSize();
    $remainingFiles = $event->activeFiles->getCount();
    $remainingDisk = $event->activeFiles->getFormattedSize();
    $numberOfDeletedDirectories = $event->deletedDirectories;
    
    // Send email notification
    //...
});

如果命令失败,发送电子邮件通知

// app/Providers/EventServiceProvider.php boot method

use Arifhp86\ClearExpiredCacheFile\Events\GarbageCollectionFailed;

Event::listen(function (GarbageCollectionFailed $event) {
    $exception = $event->exception;
    $message = $exception->getMessage();
    $stackTrace = $exception->getTraceAsString();
    
    // Send email notification
    //...
});

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

贡献

请参阅CONTRIBUTING以获取详细信息。

安全漏洞

请参阅我们的安全策略了解如何报告安全漏洞。

致谢

许可

MIT 许可证 (MIT)。请参阅许可文件以获取更多信息。