Last active
July 25, 2024 18:41
-
-
Save arlanram/cf1b153a7be424e252ba94111961feb3 to your computer and use it in GitHub Desktop.
Laravel: Cache::remember() with middleware to cache all incoming request and handle them to cache storage or make request if key not found
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Middleware; | |
use Closure; | |
use Illuminate\Support\Facades\Cache; | |
class CacheQuery | |
{ | |
public function handle($request, Closure $next) | |
{ | |
return Cache::remember(sha1($request->fullUrl()), 3600, fn() => $next($request)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you may want some logic here to prevent errors from being cached, or maybe another middleware to detect errors and wipe the cache key