├── .editorconfig ├── .gitignore ├── .travis.yml ├── CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── src ├── LaravelCacheGarbageCollector.php └── SkeletonClass.php └── tests └── LaravelCacheGarbageCollectorTest.php /.editorconfig: -------------------------------------------------------------------------------- 1 | ; This file is for unifying the coding style for different editors and IDEs. 2 | ; More information at http://editorconfig.org 3 | 4 | root = true 5 | 6 | [*] 7 | charset = utf-8 8 | indent_size = 4 9 | indent_style = space 10 | end_of_line = lf 11 | insert_final_newline = true 12 | trim_trailing_whitespace = true 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | composer.lock 3 | .idea 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | os: linux 4 | 5 | cache: 6 | directories: 7 | - $HOME/.cache/pip 8 | - $HOME/.composer/cache/files 9 | 10 | php: 11 | - 7.3 12 | - 8.0 13 | - 8.1 14 | 15 | env: 16 | - LARAVEL_VERSION=8.x-dev 17 | - LARAVEL_VERSION=9.x-dev 18 | 19 | jobs: 20 | exclude: 21 | - php: 7.3 22 | env: LARAVEL_VERSION=9.x-dev 23 | 24 | before_install: 25 | - echo "memory_limit=2G" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini 26 | - cp ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini ~/xdebug.ini 27 | - phpenv config-rm xdebug.ini 28 | - composer require "laravel/framework:${LARAVEL_VERSION}" 29 | 30 | install: travis_retry composer install --no-interaction --prefer-dist 31 | 32 | before_script: phpenv config-add ~/xdebug.ini 33 | 34 | script: vendor/bin/phpunit tests/ 35 | 36 | notifications: 37 | email: 38 | recipients: me@jdavidbaker.com 39 | -------------------------------------------------------------------------------- /CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. 4 | 5 | We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality. 6 | 7 | Examples of unacceptable behavior by participants include: 8 | 9 | * The use of sexualized language or imagery 10 | * Personal attacks 11 | * Trolling or insulting/derogatory comments 12 | * Public or private harassment 13 | * Publishing other's private information, such as physical or electronic addresses, without explicit permission 14 | * Other unethical or unprofessional conduct. 15 | 16 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, project maintainers commit themselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team. 17 | 18 | This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community in a direct capacity. Personal views, beliefs and values of individuals do not necessarily reflect those of the organisation or affiliated individuals and organisations. 19 | 20 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. 21 | 22 | This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/) 23 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | We accept contributions via Pull Requests on [Github](https://github.com/jdavidbakr/LaravelCacheGarbageCollector). 6 | 7 | 8 | ## Pull Requests 9 | 10 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). 11 | 12 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 13 | 14 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 15 | 16 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 17 | 18 | - **Create feature branches** - Don't ask us to pull from your master branch. 19 | 20 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 21 | 22 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 23 | 24 | 25 | ## Running Tests 26 | 27 | ``` bashß 28 | $ composer test 29 | ``` 30 | 31 | 32 | **Happy coding**! 33 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Jon Baker 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LaravelCacheGarbageCollector 2 | 3 | [![Latest Version on Packagist][ico-version]][link-packagist] 4 | [![Software License][ico-license]](LICENSE.md) 5 | [![Total Downloads][ico-downloads]][link-downloads] 6 | [![Travis CI][ico-travis]][link-travis] 7 | 8 | When using the file cache driver, Laravel creates the cache files but never purges expired ones. This can lead to 9 | a situation where you have a large number of unused and irrelevant cache files, especially if you do a lot of short-term 10 | caching in your system. 11 | 12 | This package creates an artisan command cache:gc that will garbage-collect your cache files, removing any that have expired. 13 | You may run this manually or include it in a schedule. 14 | 15 | Thanks to [TerrePorter](http://laravel.io/user/TerrePorter) for his suggestion on [laravel.io](http://laravel.io/forum/01-28-2016-cache-file-garbage-collection)! 16 | 17 | ## Install 18 | 19 | Via Composer 20 | 21 | ```bash 22 | $ composer require jdavidbakr/laravel-cache-garbage-collector 23 | ``` 24 | 25 | Then add the service provider to `app/Console/Kernel.php` in the \$commands array: 26 | 27 | ```php 28 | \jdavidbakr\LaravelCacheGarbageCollector\LaravelCacheGarbageCollector::class 29 | ``` 30 | 31 | ## Usage 32 | 33 | ```bash 34 | $ php artisan cache:gc 35 | ``` 36 | 37 | ## Contributing 38 | 39 | Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details. 40 | 41 | ## Security 42 | 43 | If you discover any security related issues, please email me@jdavidbaker.com instead of using the issue tracker. 44 | 45 | ## Credits 46 | 47 | - [Jon Baker][link-author] 48 | - [All Contributors][link-contributors] 49 | 50 | ## License 51 | 52 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 53 | 54 | [ico-version]: https://img.shields.io/packagist/v/jdavidbakr/laravel-cache-garbage-collector.svg?style=flat-square 55 | [ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square 56 | [ico-travis]: https://img.shields.io/travis/jdavidbakr/laravel-cache-garbage-collector/master.svg?style=flat-square 57 | [ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/jdavidbakr/laravel-cache-garbage-collector.svg?style=flat-square 58 | [ico-code-quality]: https://img.shields.io/scrutinizer/g/jdavidbakr/laravel-cache-garbage-collector.svg?style=flat-square 59 | [ico-downloads]: https://img.shields.io/packagist/dt/jdavidbakr/laravel-cache-garbage-collector.svg?style=flat-square 60 | [link-packagist]: https://packagist.org/packages/jdavidbakr/laravel-cache-garbage-collector 61 | [link-travis]: https://travis-ci.org/jdavidbakr/laravel-cache-garbage-collector 62 | [link-scrutinizer]: https://scrutinizer-ci.com/g/jdavidbakr/laravel-cache-garbage-collector/code-structure 63 | [link-code-quality]: https://scrutinizer-ci.com/g/jdavidbakr/laravel-cache-garbage-collector 64 | [link-downloads]: https://packagist.org/packages/jdavidbakr/laravel-cache-garbage-collector 65 | [link-author]: https://github.com/jdavidbakr 66 | [link-contributors]: ../../contributors 67 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jdavidbakr/laravel-cache-garbage-collector", 3 | "type": "library", 4 | "description": "A script that will clean up expired cache files if the system is using the files cache system", 5 | "keywords": [ 6 | "jdavidbakr", 7 | "LaravelCacheGarbageCollector" 8 | ], 9 | "minimum-stability": "dev", 10 | "homepage": "https://github.com/jdavidbakr/LaravelCacheGarbageCollector", 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Jon Baker", 15 | "email": "me@jdavidbaker.com", 16 | "homepage": "http://www.jdavidbaker.com", 17 | "role": "Developer" 18 | } 19 | ], 20 | "require": { 21 | "illuminate/support": "8.*|9.*|10.*|^11.0|^12.0", 22 | "php": ">=7.1.0", 23 | "mockery/mockery": "^1.3" 24 | }, 25 | "require-dev": { 26 | "phpunit/phpunit": "9.*|10.*|^11.5.3", 27 | "squizlabs/php_codesniffer": "~2.3|^3.9", 28 | "orchestra/testbench": "6.*|7.*|8.*|^9.0|^10.0" 29 | }, 30 | "autoload": { 31 | "psr-4": { 32 | "jdavidbakr\\LaravelCacheGarbageCollector\\": "src" 33 | } 34 | }, 35 | "autoload-dev": { 36 | "psr-4": { 37 | "jdavidbakr\\LaravelCacheGarbageCollector\\Test\\": "tests" 38 | } 39 | }, 40 | "extra": { 41 | "branch-alias": { 42 | "dev-master": "1.0-dev" 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/LaravelCacheGarbageCollector.php: -------------------------------------------------------------------------------- 1 | 'local', 37 | 'root' => config('cache.stores.file.path') 38 | ]; 39 | 40 | Config::set('filesystems.disks.fcache', $cacheDisk); 41 | 42 | // Grab the cache files 43 | $files = Storage::disk('fcache')->allFiles(); 44 | 45 | foreach ($files as $cachefile) { 46 | if ($cachefile == '.gitignore') { 47 | continue; 48 | } 49 | 50 | try { 51 | $contents = Storage::disk('fcache')->get($cachefile); 52 | 53 | // Get the expiration time 54 | $expire = substr($contents, 0, 10); 55 | 56 | if (Carbon::now()->timestamp < $expire) { 57 | continue; 58 | } 59 | 60 | Storage::disk('fcache')->delete($cachefile); 61 | } catch (FileNotFoundException $e) { 62 | continue; 63 | } 64 | } 65 | 66 | // Now that we've removed expired files, grab all directories 67 | $directories = Storage::disk('fcache')->allDirectories(); 68 | 69 | foreach ($directories as $directory) { 70 | try { 71 | $directoryFiles = Storage::disk('fcache')->allFiles($directory); 72 | 73 | if (count($directoryFiles)) { 74 | // We still have cache files in this directory, we don't want to remove it 75 | continue; 76 | } 77 | 78 | Storage::disk('fcache')->deleteDirectory($directory); 79 | } catch (FileNotFoundException $e) { 80 | continue; 81 | } 82 | } 83 | 84 | return Command::SUCCESS; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/SkeletonClass.php: -------------------------------------------------------------------------------- 1 | shouldReceive('allFiles') 21 | ->withNoArgs() 22 | ->once() 23 | ->andReturn([ 24 | 'directory/filename' 25 | ]); 26 | $mock->shouldReceive('get') 27 | ->once() 28 | ->with('directory/filename') 29 | ->andReturn(Carbon::now()->subMinute()->timestamp.':the cache data'); 30 | $mock->shouldReceive('delete') 31 | ->once() 32 | ->with('directory/filename'); 33 | 34 | $mock->shouldReceive('allDirectories') 35 | ->withNoArgs() 36 | ->once() 37 | ->andReturn([ 38 | 'directory' 39 | ]); 40 | $mock->shouldReceive('allFiles') 41 | ->with('directory') 42 | ->once() 43 | ->andReturn([]); 44 | $mock->shouldReceive('deleteDirectory') 45 | ->with('directory') 46 | ->once(); 47 | 48 | Storage::shouldReceive('disk') 49 | ->with('fcache') 50 | ->andReturn($mock); 51 | 52 | $command = new LaravelCacheGarbageCollector; 53 | 54 | $command->handle(); 55 | 56 | $this->assertTrue(true); 57 | } 58 | 59 | /** 60 | * @test 61 | */ 62 | public function it_doesnt_remove_non_expired_cache_entries() 63 | { 64 | $mock = Mockery::mock('disk'); 65 | 66 | $mock->shouldReceive('allFiles') 67 | ->once() 68 | ->andReturn([ 69 | 'directory/filename' 70 | ]); 71 | $mock->shouldReceive('get') 72 | ->once() 73 | ->with('directory/filename') 74 | ->andReturn(Carbon::now()->addMinute()->timestamp.':the cache data'); 75 | $mock->shouldReceive('delete') 76 | ->times(0) 77 | ->with('directory/filename'); 78 | 79 | $mock->shouldReceive('allDirectories') 80 | ->withNoArgs() 81 | ->once() 82 | ->andReturn([ 83 | 'directory' 84 | ]); 85 | $mock->shouldReceive('allFiles') 86 | ->with('directory') 87 | ->once() 88 | ->andReturn([ 89 | 'filename', 90 | ]); 91 | $mock->shouldReceive('deleteDirectory') 92 | ->times(0) 93 | ->with('directory'); 94 | 95 | Storage::shouldReceive('disk') 96 | ->with('fcache') 97 | ->andReturn($mock); 98 | $command = new LaravelCacheGarbageCollector; 99 | 100 | $command->handle(); 101 | 102 | $this->assertTrue(true); 103 | } 104 | } 105 | --------------------------------------------------------------------------------