SmartCache optimizes Laravel caching for large datasets through intelligent compression (up to 70% size reduction), smart chunking, and automatic optimization - while maintaining Laravel's familiar Cache API.
Caching large datasets (10K+ records, API responses, reports) in Laravel can cause:
- Memory issues - Large arrays consume too much RAM
 - Storage waste - Uncompressed data fills Redis/Memcached quickly
 - Slow performance - Serializing/deserializing huge objects takes time
 - Cache stampede - Multiple processes regenerating expensive data simultaneously
 
SmartCache fixes all of this automatically.
composer require iazaran/smart-cacheThat's it! Works out-of-the-box. No configuration required.
Requirements:
- PHP 8.1+
 - Laravel 8.0 - 12.x
 - Any cache driver (Redis, File, Database, Memcached, Array)
 
Use exactly like Laravel's Cache facade:
use SmartCache\Facades\SmartCache;
// Works exactly like Laravel Cache
SmartCache::put('users', $users, 3600);
$users = SmartCache::get('users');
// Remember pattern
$users = SmartCache::remember('users', 3600, function() {
    return User::all();
});β¨ The Magic: Large data is automatically compressed and chunked - reducing cache size by up to 70%!
// Store
smart_cache(['products' => $products], 3600);
// Retrieve
$products = smart_cache('products');Large data is automatically compressed:
// Large API response - automatically compressed
$apiData = Http::get('api.example.com/large-dataset')->json();
SmartCache::put('api_data', $apiData, 3600);
// Automatically compressed with gzip, saving up to 70% spaceWhen it applies: Data > 50KB (configurable) Benefit: 60-80% size reduction
Large arrays are automatically split into manageable chunks:
// 10,000+ records - automatically chunked
$users = User::with('profile', 'posts')->get();
SmartCache::put('all_users', $users, 3600);
// Automatically split into 1000-item chunksWhen it applies: Arrays with 5000+ items (configurable) Benefit: Better memory usage, faster access
SmartCache chooses the best optimization automatically:
| Data Type | Size | Strategy | Benefit | 
|---|---|---|---|
| Large Arrays (5000+ items) | Any | Chunking | Better memory, faster access | 
| Text/Strings | >50KB | Compression | 60-80% size reduction | 
| Mixed Objects | >50KB | Compression | Optimal serialization | 
| API Responses | >100KB | Both | Best performance | 
| Small Data | <50KB | None | Fastest (no overhead) | 
Production Results (E-commerce Platform):
- 72% cache size reduction (15MB β 4.2MB)
 - 800MB daily Redis memory savings
 - 40% faster retrieval vs standard Laravel Cache
 - 94.3% cache hit ratio
 - 23ms average retrieval time
 
All advanced features are opt-in and disabled by default for maximum compatibility.
Prevent multiple processes from regenerating expensive cache simultaneously:
$lock = SmartCache::lock('expensive_operation', 10);
if ($lock->get()) {
    // Only one process executes this
    $data = expensiveApiCall();
    SmartCache::put('api_data', $data, 3600);
    $lock->release();
}
// Or use callback pattern
SmartCache::lock('regenerate_cache', 30)->get(function() {
    return regenerateExpensiveData();
});Benefit: Prevents cache stampede, reduces server load
Cache data in memory for the current request:
$memo = SmartCache::memo();
// First call hits cache, subsequent calls are instant
$users = $memo->remember('users', 3600, fn() => User::all());
$users = $memo->get('users'); // Instant! (from memory)
$users = $memo->get('users'); // Still instant!
// Perfect for loops
foreach ($products as $product) {
    $category = $memo->get("category_{$product->category_id}");
}Benefit: 10-100x faster repeated access within same request/job
Optimize multiple cache operations:
// Retrieve multiple keys
$values = SmartCache::many(['key1', 'key2', 'key3']);
// Store multiple keys
SmartCache::putMany([
    'key1' => 'value1',
    'key2' => 'value2',
], 3600);
// Delete multiple keys
SmartCache::deleteMultiple(['key1', 'key2', 'key3']);Auto-optimize compression levels based on data characteristics:
// Enable in config
config(['smart-cache.strategies.compression.mode' => 'adaptive']);
// Automatically selects optimal level:
SmartCache::put('hot_data', $frequentlyAccessed, 3600);  // Level 3-4 (faster)
SmartCache::put('cold_data', $rarelyAccessed, 3600);     // Level 7-9 (smaller)How it works:
- Analyzes data compressibility
 - Tracks access frequency
 - Hot data = faster compression
 - Cold data = higher compression
 
Load large datasets on-demand to save memory:
// Enable in config
config(['smart-cache.strategies.chunking.lazy_loading' => true]);
// Returns LazyChunkedCollection
$largeDataset = SmartCache::get('100k_records');
// Chunks loaded on-demand (max 3 in memory)
foreach ($largeDataset as $record) {
    processRecord($record);
}
// Access specific items
$item = $largeDataset[50000]; // Only loads needed chunkBenefit: 30-50% memory savings for large datasets
Auto-select best serialization method:
// Automatically chooses:
SmartCache::put('simple', ['key' => 'value'], 3600);  // JSON (fastest)
SmartCache::put('complex', $objectGraph, 3600);       // igbinary/PHPMethods: JSON β igbinary (if available) β PHP serialize
Monitor cache operations in real-time:
// Enable in config
config(['smart-cache.events.enabled' => true]);
// Listen to events
Event::listen(CacheHit::class, fn($e) => Log::info("Hit: {$e->key}"));
Event::listen(CacheMissed::class, fn($e) => Log::warning("Miss: {$e->key}"));
Event::listen(OptimizationApplied::class, fn($e) =>
    Log::info("Optimized {$e->key}: {$e->ratio}% reduction")
);Events: CacheHit, CacheMissed, KeyWritten, KeyForgotten, OptimizationApplied
Serve stale data while refreshing in background:
$apiData = SmartCache::swr('github_repos', function() {
    return Http::get('https://api.github.com/user/repos')->json();
}, 300, 900); // 5min fresh, 15min staleFor slowly changing data:
$siteConfig = SmartCache::stale('site_config', function() {
    return Config::fromDatabase();
}, 3600, 86400); // 1hour fresh, 24hour staleProactively refresh before expiration:
$analytics = SmartCache::refreshAhead('daily_analytics', function() {
    return Analytics::generateReport();
}, 1800, 300); // 30min TTL, 5min refresh windowPattern-based cache clearing:
// Clear by pattern
SmartCache::flushPatterns([
    'user_*',           // All user keys
    'api_v2_*',         // All API v2 cache
    '/product_\d+/'     // Regex: product_123, product_456
]);
// Dependency tracking
SmartCache::dependsOn('user_posts', 'user_profile');
SmartCache::invalidate('user_profile'); // Also clears user_posts$metrics = SmartCache::getPerformanceMetrics();
// Returns: hit_ratio, compression_savings, operation_timing, etc.
$analysis = SmartCache::analyzePerformance();
// Returns: health score, recommendations, issues# Status overview
php artisan smart-cache:status
# Detailed analysis
php artisan smart-cache:status --force
# Clear cache
php artisan smart-cache:clear
php artisan smart-cache:clear expensive_api_callExecute commands via web interface (no SSH needed):
$status = SmartCache::executeCommand('status');
$clearResult = SmartCache::executeCommand('clear', ['key' => 'api_data']);php artisan vendor:publish --tag=smart-cache-config// config/smart-cache.php
return [
    // Size thresholds for optimization
    'thresholds' => [
        'compression' => 1024 * 50,  // 50KB - compress data larger than this
        'chunking' => 1024 * 100,    // 100KB - chunk arrays larger than this
    ],
    // Optimization strategies
    'strategies' => [
        'compression' => [
            'enabled' => true,
            'mode' => 'fixed',       // 'fixed' or 'adaptive'
            'level' => 6,            // 1-9 (higher = better compression)
        ],
        'chunking' => [
            'enabled' => true,
            'chunk_size' => 1000,    // Items per chunk
            'lazy_loading' => false, // Enable for memory savings
            'smart_sizing' => false, // Auto-calculate chunk size
        ],
    ],
    // Events (disabled by default for performance)
    'events' => [
        'enabled' => false,
    ],
    // Performance monitoring
    'monitoring' => [
        'enabled' => true,
        'metrics_ttl' => 3600,
    ],
];| Driver | Compression | Chunking | Locks | All Features | 
|---|---|---|---|---|
| Redis | β | β | β | β Full Support | 
| File | β | β | β | β Full Support | 
| Database | β | β | β | β Full Support | 
| Array | β | β | β | β Full Support | 
| Memcached | β | β | β | β Full Support | 
All Laravel cache drivers are fully supported!
SmartCache is a drop-in replacement - your existing code works unchanged:
// Before (Laravel Cache)
use Illuminate\Support\Facades\Cache;
Cache::put('users', $users, 3600);
$users = Cache::get('users');
// After (SmartCache) - just change the import
use SmartCache\Facades\SmartCache;
SmartCache::put('users', $users, 3600);  // Now automatically optimized!
$users = SmartCache::get('users');       // Automatically restored!That's it! No code changes needed. You immediately get:
- β Automatic compression for large data
 - β Smart chunking for large arrays
 - β All new features available
 
π Full Documentation - Complete guide with examples
SmartCache includes 300+ comprehensive tests covering all functionality:
composer test
# With coverage
composer test-coverageContributions are welcome! Please see CONTRIBUTING.md for details.
MIT License. See LICENSE for details.
- π¦ Packagist: iazaran/smart-cache
 - π Issues: GitHub Issues
 - π Docs: Full Documentation
 
Built with β€οΈ for the Laravel community
Optimize caching for large data - from simple apps to enterprise systems