Making sure routes, config and events of your Laravel app are cached in production
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
// typically in a service provider use Spatie\Health\Facades\Health; use Spatie\Health\Checks\Checks\OptimizedAppCheck; Health::checks([ // other checks... OptimizedAppCheck::new(), ]);
This check will pass if the config, routes and events are cached.
If you only want to check certain caches, you can call the
checkEvents methods. In this example, we'll only check for cached config and routes.
use Spatie\Health\Facades\Health; use Spatie\Health\Checks\Checks\OptimizedAppCheck; Health::checks([ // other checks, OptimizedAppCheck::new() ->checkConfig() ->checkRoutes(), ]);
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.
app()->configurationIsCached() app()->routesAreCached() app()->eventsAreCached()
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.
just an fyi you have a typo in the link to Oh Dear in the body of your article. Thanks for your contributions to Laravel and community.
Surprised to see the
optimizecommand being recommended here when the Laravel upgrade notes states this will be removed in the future.
optimizecommand also doesn't compile the Blade templates which can aid performance and is executed using