Support the ongoing development of Laravel.io →
posted 8 years ago
Cache
Last updated 1 year ago.
0

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, ...

0

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.

0

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-garbag...

I have it in my kernel console scheduler to run every hour. So far it seems to work. Thanks again for the suggestion!

0

Thanks for the package. Is it possible to change the schedule to 1 week or 1 month?

0

Sign in to participate in this thread!

Eventy

Your banner here too?

jdavidbakr jdavidbakr Joined 28 Jul 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.