PHP code example of hishabee / laravel-query-cache
1. Go to this page and download the library: Download hishabee/laravel-query-cache library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hishabee / laravel-query-cache example snippets
// 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();
// Query:
User::where('active', true)->cache()->get();
// Generated Tags:
[
'table:users', // Base table tag
'where:users:active:1' // Condition tag
]
// 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!
$key = 'query_cache:' . md5($fullQuery);
// Example:
// SELECT * FROM users WHERE active = '1'
// Becomes: query_cache:a1b2c3d4e5f6...
'duration' => env('QUERY_CACHE_DURATION', 3600),
User::where('active', true)
->cache(7200) // Cache for 2 hours
->get();
// Clear cache for specific table
Cache::tags(['table:users'])->flush();
// Clear cache for specific condition
Cache::tags(['where:users:role:admin'])->flush();