iazaran / smart-cache
Smart Cache is a caching optimization package designed to enhance the way your Laravel application handles data caching. It intelligently manages large data sets by compressing, chunking, or applying other optimization strategies to keep your application performant and efficient.
Requires
- php: ^8.1
- illuminate/cache: >=8.0
- illuminate/console: >=8.0
- illuminate/contracts: >=8.0
- illuminate/support: >=8.0
README
Laravel SmartCache is a caching optimization package designed to enhance the way your Laravel application handles data caching. It intelligently manages large data sets by compressing, chunking, or applying other optimization strategies to keep your application performant and efficient.
๐ Features
- ๐ Auto-detects large cache payloads
- ๐ฆ Compresses data before caching
- ๐งฉ Chunks large arrays or objects into manageable parts
- ๐ง Intelligent serialization
- โป๏ธ Seamless retrieval and reconstruction
- โ๏ธ Extensible strategy pattern for custom optimizations
- ๐ก๏ธ Optional fallback for incompatible drivers
- ๐ Laravel-style helper function support
๐ฆ Installation
composer require iazaran/smart-cache
Laravel 8+ is supported. Tested with Redis and file cache drivers.
โ๏ธ Configuration
After installation, publish the config file:
php artisan vendor:publish --tag=smart-cache-config
The config file allows you to define thresholds, compression strategies, chunk sizes, and enabled features.
๐งช Usage
Use SmartCache
just like Laravel's Cache
facade:
use SmartCache\Facades\SmartCache; SmartCache::put('key', $largeData, now()->addMinutes(10)); $data = SmartCache::get('key');
Or use the global helper function, similar to Laravel's cache()
helper:
// Get a value $value = smart_cache('key'); // Get with default value $value = smart_cache('key', 'default'); // Store a value smart_cache(['key' => $largeData], now()->addMinutes(10)); // Access the SmartCache instance $cache = smart_cache();
Or inject it into your services:
public function __construct(\SmartCache\Contracts\SmartCache $cache) { $this->cache = $cache; }
๐ง Optimization Strategies
SmartCache includes several strategies out of the box:
- Compression: Uses gzip or other drivers
- Chunking: Splits large data structures
- Encoding: Serializes data safely
- Driver-Aware: Avoids incompatible features based on driver
These strategies can be customized or extended by implementing SmartCache\Contracts\OptimizationStrategy
.
๐ Example
$data = range(1, 10000); SmartCache::put('numbers', $data, 600); // Behind the scenes: // - Checks size // - Compresses or chunks as needed // - Stores metadata for retrieval // Or with helper function smart_cache(['numbers' => $data], 600);
๐งฐ Artisan Commands
php artisan smart-cache:clear php artisan smart-cache:status
Built with โค๏ธ for developers who care about performance.