If you use Laravel debug bar when developing your app you'll notice there is a tab that will show you all the database queries your application is making.
This is a great tool to spot areas where you can improve the database queries or if there's data you can cache instead of making another database call.
When working on a project I noticed that when logged in, the application was making at least one extra database query to fetch the logged in user.
These calls are commonly called by checks to either get the logged in user by using
Auth::user() or to check if the user is logged in by using
These two calls will look up the user ID in session and then go an fetch the user record from the database based off this ID, therefore this is something we can change to cache the user record.
For this to work we're going to create a new user Auth provider which will inherit from the default
\Illuminate\Auth\EloquentUserProvider, this will override the
retrieveById method and get the value from cache instead of calling the database.
This will also need to update the cache whenever the user record is changed, allowing us to update the value with the new version of the user record.
First we're going to create a new user provider which will inherit from the
EloquentUserProvider this will attempt to get the user from cache, if no user is found then it will fetch the user from the database. To do this it will need to override the
retrieveById method so that this is called from the auth guards.
CacheUserProvider created we can register this as a new provider by adding the following into the auth service provider class.
This allows us to to use the
'cache-user' provider to fetch users.
With the CacheUserProvider registered as
cache-user we can choose to use this as our user provider by changing the
config/auth.php providers property.
When fetching users Laravel will use our new provider where we can lookup in cache first instead of fetching from the database.
Laravel with now use our provider to fetch the user, but we need to make sure that once the user is retrieved we add this user to cache. We need to make sure that when the user is updated we replace the cache with the updated user. We also need to make sure that if the user is deleted we remove the user from cache.
For this functionality we're going to use an observer to run off eloquent events and perform different functionality when the events are triggered.
To create an observer, add a new folder to your
app directory and add the following file.
With the observer created we need to attach this to the User model, this is done inside the
Now when you log into your application you'll notice that it will no longer be getting the user from the database and it will be able to be fetched from cache instead.
Get weekly updates to your email