Laravel 5.0 - Route Caching

(This is part of a series of posts on New Features in Laravel 5.0.)

  1. Laravel 5.0 - Form Requests
  2. Laravel 5.0 - ValidatesWhenResolved
  3. Laravel 5.0 - Directory structure and namespace
  4. Laravel 5.0 - Route Caching
  5. Laravel 5.0 - Cloud File Drivers
  6. Laravel 5.0 - Method Injection
  7. Laravel 5.0 - Route Annotations (removed)
  8. Laravel 5.0 - Event Annotations (removed)
  9. Laravel 5.0 - Middleware (Filter-style)
  10. Laravel 5.0 - Environment Detection & Environment Variables
  11. Laravel 5.0 - Event Scheduling
  12. Laravel 5.0 - Commands & Handlers
  13. Upgrading from Laravel 4 to Laravel 5
  14. Bringing Whoops Back to Laravel 5
  15. Laravel 5.0 - Events & Handlers
  16. Laravel 5.0 - Generating Missing Events
  17. Laravel 5.0 - Custom Error Pages
  18. Laravel 5.0 - Eloquent Attribute Casting

Performance optimization within our PHP code isn't always something at the forefront of our minds, but our backend code--especially our more complex operations--can impact our sites' request times to the tune of tens and maybe even hundreds of milliseconds. It may not sound like much, but a few hundred milliseconds can mean a huge difference in the perceived quickness of your applications.

Whether or not you were aware of it, the routing logic in Laravel 4 and earlier--especially as you have more and more routes in your application--were one such place for performance bottlenecks. A site with just a few hundred routes could, in the past, lose up to a half second just for the framework to register those routes. Fear no more, as Laravel 5 introduces route caching, optimizing the performance of your routes (except Closure routes, so it's time to move them all to controllers).

Using Route Caching #

There's not a lot to using this feature, honestly. There's a new Artisan command, route:cache, which serializes the results of your routes.php file--it's performing the operation of parsing the routes once and then storing those result. Sort of like pre-compiling a Handlebars template, if you've ever done that before.

That's it! Now your routes are being parsed from the cached file, not your routes file. You can make all the changes you want to routes.php and the routing of your app won't change until you re-cache.

Pros and Cons #

The pros are pretty clear: your site gets faster.

The cons, however, need to be noted: Once you cache the site's routes once, you'll now have to re-cache your routes every time you make any changes to routes.php, or the changes won't show up. This could cause confusion for new developers, or even for you if you just happen to forget you were using caching.

Thankfully, there are two things that can help you here. First, you can run artisan route:clear, and artisan will delete your route cache. Second, you can consider only caching on your production server. Maybe only run artisan route:cache as a post-deploy hook in Git, or just run it as a part of your Forge deploy process.

Konklużjoni #

The performance benefit of route caching might not be worth the potential confusion for you. If so, you can pretend this doesn't exist. But for groups with more managed deploy processes, the idea of something this simple trimming off a half second or more of load time on every page is huge.


Comments? I'm @stauffermatt on Twitter


Tags: laravel | 5.0 | laravel 5

Matt Stauffer headshot

Hi, I'm Matt Stauffer.

I'm partner & technical director at Tighten Co.

You can find me on Twitter at @stauffermatt


Like what you're reading?

I wrote an entire 450+ page book for O'Reilly: Laravel: Up and Running.

You can order the eBook or print book today.