Smart SQL query caching for Laravel with tag-based invalidation
hishabee/laravel-query-cache is a Laravel package for smart sql query caching for laravel with tag-based invalidation.
It currently has 0 GitHub stars and 6 downloads on Packagist (latest version v1.0.3).
Install it with composer require hishabee/laravel-query-cache.
Discover more Laravel packages by hishabee
or browse all Laravel packages to compare alternatives.
Last updated
Smart SQL query caching for Laravel with tag-based invalidation and precise cache clearing.
composer require hishabee/laravel-query-cache
The package will automatically register its service provider.
Publish the configuration file:
php artisan vendor:publish --tag=query-cache-config
Add these variables to your .env file:
QUERY_CACHE_ENABLED=true
QUERY_CACHE_STRATEGY=manual
QUERY_CACHE_DURATION=3600
QUERY_CACHE_STORE=redis
// Cache a specific query for 1 hour
User::where('active', true)
->cache(3600)
->get();
// Cache with default duration from config
Post::latest()
->cache()
->paginate();
// Disable cache for a specific query (when strategy is 'all')
Order::where('status', 'pending')
->cache(false)
->get();
The package uses a sophisticated tagging system to enable precise cache invalidation:
// Query:
User::where('active', true)->cache()->get();
// Generated Tags:
[
'table:users', // Base table tag
'where:users:active:1' // Condition tag
]
// Query:
User::where('active', true)
->where('role', 'admin')
->cache()
->get();
// Generated Tags:
[
'table:users',
'where:users:active:1',
'where:users:role:admin'
]
When a table is updated, the package intelligently invalidates only relevant cached queries:
// Original cached query
User::where('role', 'admin')->cache()->get();
// When this update happens:
User::where('role', 'user')->update(['active' => false]);
// Only cache entries with these tags are invalidated:
[
'table:users',
'where:users:role:user'
]
// The cached 'admin' query remains valid!
Cache keys are generated based on the full query including bindings:
$key = 'query_cache:' . md5($fullQuery);
// Example:
// SELECT * FROM users WHERE active = '1'
// Becomes: query_cache:a1b2c3d4e5f6...
You can set cache duration at multiple levels:
'duration' => env('QUERY_CACHE_DURATION', 3600),
User::where('active', true)
->cache(7200) // Cache for 2 hours
->get();
// Multiple joins
User::join('orders', 'users.id', '=', 'orders.user_id')
->join('products', 'orders.product_id', '=', 'products.id')
->where('orders.status', 'completed')
->cache()
->get();
// Nested conditions
User::where(function($query) {
$query->where('role', 'admin')
->orWhere('role', 'manager');
})
->cache()
->get();
// Clear cache for specific table
Cache::tags(['table:users'])->flush();
// Clear cache for specific condition
Cache::tags(['where:users:role:admin'])->flush();
Enable debug mode in your .env:
QUERY_CACHE_DEBUG=true
This will log:
Example log output:
[Query Cache] Hit: SELECT * FROM users WHERE active = '1'
[Query Cache] Tags: table:users, where:users:active:1
[Query Cache] Invalidated tags: table:users, where:users:role:admin
Cache Store
Cache Duration
Excluded Tables
excluded_tables in configContributions are welcome! Please feel free to submit a Pull Request.
MIT License. See LICENSE.md for details.