Skip to content

Instantly share code, notes, and snippets.

@arlanram
Last active July 25, 2024 18:41
Show Gist options
  • Save arlanram/cf1b153a7be424e252ba94111961feb3 to your computer and use it in GitHub Desktop.
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
<?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));
}
}
@powerje
Copy link

powerje commented Jul 25, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment