Written by Giles Bennett
When we were revamping our site as part of the 10th anniversary of HummingbirdUK's founding - it's still hard to believe that it's been that long - we knew that we would move away from Wordpress, on which our sites had always been built in the past - to a site built on Laravel and using Tailwind CSS for the frontend. Wordpress is great, but it does bring with it an awful lot of cruft. Some of this is visible cruft, but most of it is behind-the-scenes cruft, and if one of your key drivers is to create a site that's lightning fast, it's easier to start with a blank canvas and add only what you need to it than it is to start with a full canvas and strip things away from it.
We'll be outlining some of the tips and tricks for getting a lightning fast site in some upcoming posts, but even if you don't try to make your Laravel site as efficient as possible from the outset, implementing caching for content which rarely changes is a simple way of getting a great speed boost out of an existing site.
Caching with APC
Whilst Laravel ships, out of the box, with support for multiple cache drivers, the easiest to implement (and the most likely to already be in place on your hosting) is APC. The Alternative PHP Cache, this speeds up the execution of PHP by ensuring that, the first time a script is executed, it is parsed and compiled, and the compiled response is then stored. The next time the same script is called, rather than having to to parse and compile it again, the already-compiled response is loaded from the cache and served up.
This works fine for the vast majority of approaches. If you take our site, for example, the home page content only changes either (a) when we update something manually, by changing our recent work section, for example, or (b) when a new blog post is published, in which case the list of recent blog posts needs to update to reflect the publishing of the new post.
This means that the page will generally not change from visitor to visitor - so rather than run all the code to put the page together each time a new visitor comes along, when the first visitor comes along we can parse and compile the necessary PHP code, then cache the resultant code and execute it for each subsquent visitor, until the page content changes.
Enabling APC
We don't propose to cover installing APC and getting it up and running on your server, but once you've done so, then it's just a simple tweak within your .env file in Laravel to tell it to use APC, rather than the default file option, for your caching needs.
#Caching
CACHE_DRIVER=apc
You then need to reload your configuration from the command line using :
php artisan optimize
Caching within your controller
Assuming that your routes.php file hands off to a controller, then the next stop is that controller. Using the example below for a single, simple, page, the getHome() method just returns the relevant view :
public function getHome(Request $request) {
return view('home');
}
All we need to do is to change this slightly. First we need to import the caching facade at the top of our controller :
use Illuminate\Support\Facades\Cache;
And then within our method we first need to see if we've already got a cached response for the homepage, and if so, we return that. If not, then we put together the data that makes up the view (using the view that we've already got), store it in the cache, and then return it :
if(Cache::has('home')) {
return Cache::get('home');
} else {
$data = view('home')->render();
Cache::put('home', $data);
return $data;
}
}
And that's it - the first time the homepage gets requested, the response is cached, and thereafter it is returned from the cache, which is significantly faster, rather than being put together each time.
Clearing the cache
This can be done programmatically. If, for example, you have some sort of backend editor for blog posts, then whenever a post is updated you would want to trigger a clear of the cache for that page. Or if a new post is added, you would want to trigger a refresh of - in our case - the homepage and any other pages that show the latest blog posts.
But at its most simple this too can be done via Artisan from the command line, using :
php artisan cache:clear