Caching of data is very important for any application and if we can save SQL queries, then it is always a bonus. In my last tutorial, I created the node object which was fetching its data from multiple tables and in different queries. In this tutorial I will cache the node object and save the SQL queries.
The Debug bar bundle for Laravel 4
Laravel 3 came with a debug toolbar out of the box which is simply awesome. It shows the queries which were fired to generate the page and also any log messages if we write them. This proved to be a very handy tool specially because we could simply leave the log messages in the code and switch off the debug bar in the config file. In Laravel 4, we need to install the bundle – barryvdh > laravel-debugbar and the good news it is got even better.
Installing Debug bar
I will install this first and then proceed to the caching of queries. I will use Composer to install the bundle. The documentation on how to install it is already present in the git readme of the bundle, I am just following the same.
Yes, you need to have composer and git in your environment variables for this to run properly and also the php SSL extension is required if you want to download the bundles with git via composer; so make sure you have them.
Once done, we need to add two lines in our app.php file and run an artisan command to publish the module. Once we this successfully, we can see the debug bar on our L4 landing page like this:
Now, if you check my code on the blog page, we will find that for 4 blog posts, I am running 9 queries. To add more I have not even attached the picture information and also any other field which would result in even more queries.
So the idea is that once a node is created, it will hardly change; hence we would cache it. On top of that I will first check if the caching is enabled or not so that I can avoid it during development.
The app.php setting
In the app.php file, I have added a new variable caching which I have set to true. And inside my helper class in the libraries folder I have first checked for the setting in the app file, and if it is true then only I check if the cache is present.
The model changes for cache
To use this code, I have made a few changes in my Blog model. I was using getBlogs to fetch all the blogs based on blog id. Instead of that, now getBlogs first detects if I want a single blog or multiple blogs and accordingly calls the getBlog (singular) method which controls the query part.
This way, I have not touched the Node model and still cache the object and so next time I look for a blog and if the cache is present, it will fetch data from the cache and not fire any query.
Here is the screen shot of the page when the cache was present, you can see that not even a single query was fired because the first method which I was using to fetch the latest blog is also cache now. Hence we get a page with 4 blog posts with not even a single query.
On line 62 of the blog model, I have first called my Helper function to check if the cache is present. It checks the settings, and then the cache object and returns the object or false. And then the before returning in the last line of the getBlog method, I am setting the cache.
I have uploaded all of the code on Github so that you can easily check it out. Github code