Pragmatism in the real world

Slim's route cache file

When you have a lot of routes, that have parameters, consider using the router’s cache file to speed up performance.

To do this, you set the routerCacheFile setting to a valid file name. The next time the app is run, then the file is created which contains an associative array with data that means that the router doesn’t need to recompile the regular expressions that it uses.

For example:

$config = [
    'settings' => [
        'addContentLengthHeader' => false,
        'displayErrorDetails' => true, // set to false in production
        'routerCacheFile' => __DIR__ . '/../routes.cache.php',
    ],
];

$app = new Slim\App($config);

Note that there’s no invalidation on this cache, so if you add or change any routes, you need to delete this file. Generally, it’s best to only set this in production.

As a very contrived example to show how it works, consider this code:

<?php
require __DIR__ . '/../vendor/autoload.php';

$config = [
    'settings' => [
        'addContentLengthHeader' => false,
        'displayErrorDetails' => true, // set to false in production
        'routerCacheFile' => __DIR__ . '/../routes.cache.php',
    ],
];

$app = new Slim\App($config);

for ($groupName = 1; $groupName <= 25; $groupName++) {
    $app->group("/$groupName", function() {
        for ($i = 1; $i <= 1000; $i++) {
            $this->get("/$i/{id:\d+}", App\Action::class);
            $this->post("/$i/{id:\d+}", App\Action::class);
            $this->put("/$i/{id:\d+}", App\Action::class);
            $this->delete("/$i/{id:\d+}", App\Action::class);
        }
    });
}

// Run!
$start = microtime(true);
$app->run();
$diff = microtime(true) - $start;
echo "\n" . sprintf("%0.03f", $diff * 1000);

This application creates 25 groups, each with 4000 routes, each of which has a placeholder parameter with a constraint. That’s quite a lot of routes, but things take long enough that we can see timing. The App\Action does nothing.

On my computer, using PHP 7.0.18’s built-in web server, the first time we run it, we see this:

$ curl "http://localhost:8888/1/2/3"
2722.414

This took 2.7 seconds to execute. At the same time, it also created a file called routes.cache.php which is then used for the next run:

$ curl "http://localhost:8888/1/2/3"
263.228

This time, it took just 263ms.

That’s a big difference!

If you have a lot of complex routes in your Slim application, then I recommend that you test whether enabling route caching makes a difference.

3 thoughts on “Slim's route cache file

  1. "Note that there’s no invalidation on this cache, so if you add or change any routes, you need to delete this file. Generally, it’s best to only set this in production."

    You can write a code line that checks the routes file modification date and if it has changed, delete the cache file.

    1. Slim 4 uses the same router, so you can still cache routes. However as it no longer ships a container, you have to wire it yourself by calling the router's setCacheFile() method yourself.

Comments are closed.