Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

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

Original – by Freek Van der Herten – 3 minute read

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

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 checkConfig, checkRoutes and 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.

image

Stay up to date with all things Laravel, PHP, and JavaScript.

Follow me on Twitter. I regularly tweet out programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

avatar

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.

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.