★ Making sure routes, config and events of your Laravel app are cached in production

Written by / Original link on Jul. 18, 2022

A typical Laravel application will likely have many routes, config files and possibly some events. In your development environment, these routes and config files will be loaded and registered in each request. The performance penalty for this is not too big.

In a production environment, you want to cache these things. Laravel makes this easy by offering a couple of Artisan commands you can use in your deployment procedure.

php artisan optimize # will cache routes and config
php artisan event:cache # will cache events

By caching these things, you'll improve the performance of your Laravel app.

Making sure these things are cached in production

Laravel Health is a package that can detect various problems that are going on with your application and server. It can check disk space, CPU usage, if Horizon is running, and more.

We've added a new check that makes verifies if routes, config and events are cached.

This is how you can use it. Check in the package can be registered via Health::check() function.

// typically in a service provider


// other checks...


This check will pass if the config, routes and events are cached.

If you only want to check certain caches, you can call the checkConfig, checkRoutes and checkEvents methods. In this example, we'll only check for cached config and routes.


// other checks,


How this check works under the hood

Laravel makes detecting if things are cached easy. On the Illuminate\Foundation\Application class there are several checking methods.


When you look at the source code of the OptimizedAppCheck in the laravel-health package, you'll see that we simply use these methods.

Getting notified when things are not cached in production

Oh Dear is an all-in-one monitoring tool for your entire website. It can monitor uptime, SSL certificates, broken links, ... and much more. One of the checks it can perform is application health.

Oh Dear will not run any code inside your application or server. Instead, you should perform the checks yourself. Oh Dear will send an HTTP request to your application to a specific endpoint. Your application should respond with JSON containing the result of health checks.

Laravel Health package can build up the JSON the Oh Dear expects. Here are the docs on how to do that. With everything set up, Oh Dear can notify you via several channels.

Here's what the Slack notification looks like when something is not cached.



« De la tech et du soleil, nous étions à Sunny Tech ! 🌞🧑‍💻🦩 - If You're Not Paying for the Product, You Are... Possibly Just Consuming Goodwill for Free »