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

Rewrite #7

Merged
merged 4 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .github/SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Security Policy

If you discover any security related issues, please email hello@arif.me instead of using the issue tracker.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### 0.0.5
- Only read part of the file to check if it's expired. https://github.com/arifhp86/laravel-clear-expired-cache-file/pull/6 by @jmdz

### 0.0.4
- Fix typo in readme. https://github.com/arifhp86/laravel-clear-expired-cache-file/pull/3 by @attogram

### 0.0.3
- Compatibility with Laravel 6. https://github.com/arifhp86/laravel-clear-expired-cache-file/pull/2 by @zaxxo

### 0.0.2
- Fix wrong config syntax. https://github.com/arifhp86/laravel-clear-expired-cache-file/pull/1 by @barryp
55 changes: 55 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Contributing

Contributions are **welcome** and will be fully **credited**.

Please read and understand the contribution guide before creating an issue or pull request.

## Etiquette

This project is open source, and as such, the maintainers give their free time to build and maintain the source code
held within. They make the code freely available in the hope that it will be of use to other developers. It would be
extremely unfair for them to suffer abuse or anger for their hard work.

Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the
world that developers are civilized and selfless people.

It's the duty of the maintainer to ensure that all submissions to the project are of sufficient
quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used.

## Viability

When requesting or submitting new features, first consider whether it might be useful to others. Open
source projects are used by many developers, who may have entirely different needs to your own. Think about
whether or not your feature is likely to be used by other users of the project.

## Procedure

Before filing an issue:

- Attempt to replicate the problem, to ensure that it wasn't a coincidental incident.
- Check to make sure your feature suggestion isn't already present within the project.
- Check the pull requests tab to ensure that the bug doesn't have a fix in progress.
- Check the pull requests tab to ensure that the feature isn't already in progress.

Before submitting a pull request:

- Check the codebase to ensure that your feature doesn't already exist.
- Check the pull requests to ensure that another person hasn't already submitted the feature or fix.

## Requirements

If the project maintainer has any additional requirements, you will find them listed here.

- **[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](https://pear.php.net/package/PHP_CodeSniffer).

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **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](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.

**Happy coding**!
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) arifhp86 <arifhp86@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
105 changes: 95 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,113 @@
# Clear Expired Cache files in Laravel
# Clear Expired Cache Files

This package will remove any expired cache file, this only works with `file` cache driver. It will also remove any empty folder after removing the expired cache files, so your cache folder will be totally clean.
[![Latest Stable Version](https://img.shields.io/github/release/arifhp86/laravel-clear-expired-cache-file.svg?style=flat-square)](https://github.com/arifhp86/laravel-clear-expired-cache-file/releases)
[![MIT Licensed](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Total Downloads](https://img.shields.io/packagist/dt/arifhp86/laravel-clear-expired-cache-file.svg?style=flat-square)](https://packagist.org/packages/arifhp86/laravel-clear-expired-cache-file)

When you use the `file` cache driver, you may have noticed that the cache files are never deleted automatically. They are overwritten when the cache is refreshed which is good, but if there is some randomness in you cache keys, or for some other reason some of your cache files are never touched again, they will stay on your server forever. This package helps you to get rid of these lingering files.

This package only adds one artisan command: `php artisan cache:clear-expired` and it will delete all cache files that has already been expired.

## Installation

1. Add arifhp86/laravel-clear-expired-cache-file to your project:
You can install the package via composer:

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

2. For **Laravel >= 5.5** we use Package Auto-Discovery, so you may skip this step.
For **Laravel < 5.5**, add `CacheClearServiceProvider` to the providers array in config/app.php:
## Usage

Run the following command
```bash
php artisan cache:clear-expired
```
### Run on schedule
You can also schedule the command to run automatically. For example, if you want to run the command every day at 1 AM, you can add the following to your `app/Console/Kernel.php` file:

```php
Arifhp86\ClearExpiredCacheFile\Providers\CacheClearServiceProvider::class,
protected function schedule(Schedule $schedule)
{
$schedule->command('cache:clear-expired')->dailyAt('01:00');
}
```

## Usage
### Dry run
You can run the command in dry run mode. In this mode, the command will not delete any files. It will only show you the files that will be deleted if you run the command without the `--dry-run` option.

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

### Disable directory deletion
By default, the command will delete all empty directories after deleting the expired files. You can disable this behavior by using the `--disable-directory-delete` option.

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

### Events
The command triggers three events:
- `Arifhp86\ClearExpiredCacheFile\Events\GarbageCollectionStarting`: Before the command runs.
- `Arifhp86\ClearExpiredCacheFile\Events\GarbageCollectionEnded`: After the command runs.
- `Arifhp86\ClearExpiredCacheFile\Events\GarbageCollectionFailed`: If the command fails.

You can use these events to run your own code before or after the command runs.
#### Send email notification after the command runs

```php
// 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
//...
});
```

#### Send email notification if the command fails

```php
// 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
//...
});
```

## License & Copyright
## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

## Security Vulnerabilities

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

## Credits

- [Arifur Rahman](https://github.com/arifhp86)
- [All Contributors](../../contributors)

## License

MIT, (c) 2019 Arifur Rahman
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
"email": "arifhp86@gmail.com"
}],
"require": {
"php": ">=5.4.0",
"illuminate/support": ">=5.4.0"
"php": "^7.0|^8.0",
"laravel/framework": ">=5.5",
"wilderborn/partyline": "^1.0"
},
"autoload": {
"psr-4": {
Expand All @@ -19,7 +20,7 @@
"extra": {
"laravel": {
"providers": [
"Arifhp86\\ClearExpiredCacheFile\\Providers\\CacheClearServiceProvider"
"Arifhp86\\ClearExpiredCacheFile\\ServiceProvider"
]
}
}
Expand Down
Loading