HTTP Cache

PyroCMS comes with a powerful and versatile HTTP cache ready to use out of the box. The HTTP cache generates static response files then returns them upon subsequent requests for blistering fast load times.

Caching Requests

HTTP caching, once enabled, manages itself automatically and can be easily configured to your needs. Cached responses are unique by full URI and query string and are grouped by URI. This means that /exmaple?foo and /example?bar are cached uniquely but can be purged by referencing /example only. More on this below.

Enabling HTTP Cache

You can enable the HTTP cache via the control panel in Settings > System > Caching. You can also publish the streams configuration and modify the httpcache.php file or lastly define HTTP_CACHE=true in your .env file.

Bypassing Cache

You can bypass HTTP cache by excluding the path in settings or by defining streams::httpcache=false or ttl=0 in your route action or setting a 0 TTL on your response:

$router->get(
    'example/foo/bar',
    [
        'streams::httpcache'  => false,
        'uses' => 'Foo\BarController@example',
    ]
);

$router->get(
    'example/foo/bar',
    [
        'ttl'  => 0,
        'uses' => 'Foo\BarController@example',
    ]
);

Within a controller you can set the ttl on the view response:

return $this->response->view('example/foo/bar')->setTtl(0);

Custom Timeout Rules

You can also set custom TTL values in seconds by using similar methods as described above:

$router->get(
    'example/foo/bar',
    [
        'ttl'  => 60 * 60 * 4, // 4 hours
        'uses' => 'Foo\BarController@example',
    ]
);

Within a controller you can set the ttl on the response:

return $this->response->view('example/foo/bar')->setTtl(60 * 60 * 4);

Purging Cache

HTTP cache will purge view, preview, and index named routes for entry models. For example when a post is modified the following named routes are purged from HTTP cache automatically:

'anomaly.module.posts::posts.index'  // Example: $post->route('index');
'anomaly.module.posts::posts.view'  // Example: $post->route('view');
'anomaly.module.posts::posts.preview' // Example: $post->route('preview');

You can manually purge HTTP cache by URI with the \Anomaly\Streams\Platform\Http\HttpCache class:

$cache->puge('/example/foo/bar');

The above code will purge /example/foo/bar, query string cache files like /example/foo/bar?*, and localized paths too like /fr/example/foo/bar for all enabled locales.

Clearing Cache

You can clear all cache using the \Anomaly\Streams\Platform\Http\HttpCache class as well:

$cache->clear();

Or via artisan with the httpcache:clear command:

php artisan httpcache:clear