yared / laravel-smart-cache
Automatic cache invalidation for Laravel — clear cache when models change, no manual work
Package info
github.com/yared-ayele-debela/laravel-auto-cache
pkg:composer/yared/laravel-smart-cache
Requires
- php: ^8.2
- illuminate/cache: ^10.0|^11.0|^12.0
- illuminate/database: ^10.0|^11.0|^12.0
- illuminate/support: ^10.0|^11.0|^12.0
Requires (Dev)
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2026-05-15 23:39:49 UTC
README
Automatic cache invalidation for Laravel — clear cache when models change, no manual work.
The Problem
In Laravel, developers write cache like this:
Cache::remember('products', 3600, function () { return Product::all(); });
But when a product changes:
Product::create([...]); Product::update([...]); Product::delete();
The cache still contains old data. Developers must manually clear it:
Cache::forget('products');
This becomes messy and error-prone.
The Solution
Smart Cache automatically clears cache when related models change.
use SmartCache; // Register the dependency SmartCache::watch(Product::class, 'products'); // Use it SmartCache::remember('products', function () { return Product::all(); });
Now when Product::create(), Product::update(), or Product::delete() runs, the cache is automatically cleared. No manual work.
Installation
composer require yared/laravel-smart-cache
Publish config (optional):
php artisan vendor:publish --tag=smart-cache-config
Quick Start
1. Register model → cache mapping
use SmartCache; SmartCache::watch(Product::class, 'products');
Or watch multiple keys:
SmartCache::watch(Product::class, ['products', 'homepage_products']);
2. Use SmartCache instead of Cache
$products = SmartCache::remember('products', function () { return Product::all(); });
That's it. When any Product is created, updated, deleted, or restored, the products cache is automatically invalidated.
Core Features
Automatic Model Watching
The package listens to Laravel model events:
createdupdateddeletedrestored
Cache Dependency Mapping
Map one model to multiple cache keys:
SmartCache::watch(Product::class, [ 'products', 'homepage_products', 'featured_products', ]);
Cache Tag Support (Redis / Memcached)
For tagged caches:
SmartCache::tags(['products']) ->watch(Product::class) ->remember('list', fn () => Product::all());
When a product changes, the entire products tag is flushed.
Smart Query Hash Cache
Automatically generate cache key from query:
$products = SmartCache::query( Product::where('category_id', 1)->where('price', '>', 100) );
The cache key is generated from the query hash. When any Product changes, related query caches are invalidated.
CacheableModel Trait
Define cache keys directly on your model:
use Yared\SmartCache\Traits\CacheableModel; class Product extends Model { use CacheableModel; public static function cacheKeys(): array { return ['products', 'homepage_products']; } }
Configuration
Publish the config file:
php artisan vendor:publish --tag=smart-cache-config
Config-based mappings
Define mappings in config/smart-cache.php:
'mappings' => [ 'App\Models\Product' => ['products', 'homepage_products'], 'App\Models\Category' => ['categories', 'menu_categories'], ],
Debug mode
Enable to log cache hits, misses, and invalidations:
SMART_CACHE_DEBUG=true
Dashboard
Enable the cache monitoring dashboard:
SMART_CACHE_DASHBOARD=true
Then visit /cache-monitor to see:
- Cache hits / misses / hit rate
- Invalidations
- Model → cache mappings
Advanced Usage
Custom TTL
SmartCache::remember('products', fn () => Product::all(), 7200); // 2 hours
Manual invalidation
SmartCache::forget('products');
Invalidate by model (programmatic)
SmartCache::invalidateForModel(Product::class, 'Manual refresh');
Package Structure
laravel-smart-cache/
├── src/
│ ├── Services/
│ │ ├── CacheManager.php
│ │ ├── CacheWatcher.php
│ │ └── TaggedCacheManager.php
│ ├── Listeners/
│ │ └── ModelEventListener.php
│ ├── Debug/
│ │ └── CacheDebugger.php
│ ├── Facades/
│ │ └── SmartCache.php
│ ├── Traits/
│ │ └── CacheableModel.php
│ └── SmartCacheServiceProvider.php
├── config/
│ └── smart-cache.php
└── README.md
Requirements
- PHP 8.2+
- Laravel 10.x, 11.x, or 12.x
- Illuminate Cache (Redis/Memcached for tag support)
License
MIT