I made this to clean up my cache folder.
// make a storage disk for the cache location
$cacheDisk = [
'driver' => 'local',
'root' => storage_path('framework/cache')
];
\Config::set("filesystems.disks.fcache", $cacheDisk);
// grab the cache files
$d = Storage::disk('fcache')->allFiles();
// loop the files
foreach ($d as $key => $cachefile) {
// ignore that file
if ($cachefile == '.gitignore') {
continue;
}
// grab the contents of a file
$d1 = Storage::disk('fcache')->get($cachefile);
// grab the expire time from the file
$expire = substr($contents = $d1, 0, 10);
// check if it has expired
if (time() >= $expire) {
// delete the cachefile
Storage::disk('fcache')->delete($cachefile);
}
}
Currently it is just sitting in one of my ServiceProviders, but eventually I was thinking about making a EventListener and listening to the cache driver for 'cache.write' and then running the above code to clean the cache each time I write a new cache.
I have not had any performance problems yet, ...
Thanks, I will give that a try. I might hook this into a script that runs via the scheduler instead of on each cache write just to avoid the potential overhead.
Here's a package I built that creates an artisan command cache:gc to purge the expired cache files:
https://packagist.org/packages/jdavidbakr/laravel-cache-garbage-collector
I have it in my kernel console scheduler to run every hour. So far it seems to work. Thanks again for the suggestion!
Thanks for the package. Is it possible to change the schedule to 1 week or 1 month?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community