A Laravel package that adds compression to your Laravel cache, reducing storage requirements for large cache values.
- Automatically compresses cache values before storage
- Automatically decompresses values when retrieved
- Compatible with all Laravel cache drivers
- Special handling for MongoDB to ensure UTF-8 compatibility
- Control compression via environment variables or per-call settings
- Compatible with Laravel's Cache Tags
- PHP 8.2+
- Laravel 10|11+
- PHP zlib extension (for compression)
You can install the package via composer:
composer require develupers/laravel-cache-compressYou can publish the config file with:
php artisan vendor:publish --tag="cache-compress-config"This is the contents of the published config file:
return [
/*
|--------------------------------------------------------------------------
| Enable Cache Compression
|--------------------------------------------------------------------------
|
| This option controls whether cache compression is enabled.
| You can disable it by setting this to false.
|
*/
'enabled' => env('CACHE_COMPRESS_ENABLED', true),
/*
|--------------------------------------------------------------------------
| Compression Level
|--------------------------------------------------------------------------
|
| This option controls the compression level used by gzdeflate.
| The value must be between 0 and 9, where:
| 0 = no compression
| 1 = minimal compression (fastest)
| 9 = maximum compression (slowest)
|
*/
'compression_level' => env('CACHE_COMPRESS_LEVEL', 6),
];The package adds a compress(), decompress(), withoutCompress() and withoutDecompress() method to Laravel's standard Cache facade:
use Illuminate\Support\Facades\Cache;
// Store with compression
Cache::compress()->put('key', $largeObject, 60); // 60 minutes
// Retrieve compressed data
$value = Cache::compress()->get('key');
// or
$value = Cache::decompress()->get('key');
// With a specific store
Cache::store('redis')->compress()->put('key', $value, 60);
$value = Cache::store('redis')->decompress()->get('key');Note: decompress() is just a shortcut for compress() and withoutDecompress() is just a shortcut for withoutCompress().
Alternatively, you can use the dedicated CacheCompress facade:
use Develupers\CacheCompress\Facades\CacheCompress;
// Store a value in the cache (will be compressed)
CacheCompress::put('key', $largeObject, 60); // 60 minutes
// Retrieve and automatically decompress the value
$value = CacheCompress::get('key');You can specify which cache store to use:
// Use the Redis store
$value = CacheCompress::store('redis')->get('key');
// Store with the file driver
CacheCompress::store('file')->put('key', $value, 60);If you want to use compression for all cache operations by default, you can replace Laravel's Cache facade with our CacheCompress facade by adding the following to your config/app.php:
'aliases' => Facade::defaultAliases()->merge([
//...
'Cache' => Develupers\CacheCompress\Facades\CacheCompress::class,
//...
]),With this change, all Cache:: calls in your application will automatically use compression without any additional code changes.
Cache::put('key', $value, 60); // This will be compressed
$value = Cache::get('key'); // This will be decompressedNote: Automatic compress only applies when CACHE_COMPRESS_ENABLED is set to true.
To disable compression for a specific operation at runtime, set compress(false). For example:
Cache::compress(false)->put('key', $value, 60);
Cache::compress(false)->get('key');
// or
Cache::withoutCompress()->put('key', $value, 60);
Cache::withoutDecompress()->get('key');All standard Laravel cache methods are supported:
// Remember a pattern
$value = CacheCompress::remember('key', 60, function () {
return expensive_operation();
});
// Forever
CacheCompress::forever('key', $value);
// Multiple items
$values = CacheCompress::many(['key1', 'key2']);
// Check if exists
if (CacheCompress::has('key')) {
// ...
}
// Delete
CacheCompress::forget('key');You can control compression through environment variables:
CACHE_COMPRESS_ENABLED=true
CACHE_COMPRESS_LEVEL=6
composer testPlease see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.