Imagine your application kept on performing a query on the user table like below. If you are not caching the data then the load will be high.
$users = User::get(['id', 'name', 'email']);
So to cache the data you can wrap the code above like this.
$users = cache()->remember('users', 300, function () { return User::get(['id', 'name', 'email']); });
We specify the cache key to be "users" and set the cache time to 300 seconds. On subsequent requests if there's data stored in the cache then it's returned otherwise a new cache is created and the data is returned to the user.
Leave a reply