Laravel provides an expressive, unified API for various cache backends, allowing you to take advantage of their blazing-fast data retrieval and speed up your web application.

Using Laravel Cache is very simple and useful, and it can easily be configured on the file system, Redis, Memcached, or other drivers.

Why use it? There are several reasons, and many use cases. Suppose we have a website and a set of APIs that serve its posts, news, news categories, site menus, and so on. These APIs hit our database every time they are called. A cache layer is the solution to avoid repetitive queries and gain several microseconds in API responses.

In this example we store a "news show" query in the cache with a TTL of 3600 seconds, assigning it a unique cache key and a set of tags — useful to run selective cache purges:

$currentNews = Cache::remember(sha1(request()->fullUrl()), 3600, function () {
    return News::find(request()->id);
})->tags(['News', 'Api']);

return response()->json($currentNews);

With this snippet we get a noticeable speed-up after the first query, for the following 3600 seconds.

Be careful! You have to evaluate what can be cached and what cannot. Not every response is cacheable — user information, auth tokens, real-time data, and the like must not be cached, for security and architectural reasons.

For anything else, see the Laravel cache documentation: https://laravel.com/docs/10.x/cache

Did you ever try Laravel Cache?