PHPackages                             adrhumphreys/impetuous - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. adrhumphreys/impetuous

ActiveSilverstripe-vendormodule

adrhumphreys/impetuous
======================

Static caching made easy

02PHPCI failing

Since Aug 13Pushed 5y ago1 watchersCompare

[ Source](https://github.com/adrhumphreys/impetuous)[ Packagist](https://packagist.org/packages/adrhumphreys/impetuous)[ RSS](/packages/adrhumphreys-impetuous/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

Impetuous - Static caching
==========================

[](#impetuous---static-caching)

Static caching of pages made easy. This will cache 200 GET responses and store them by default in `public/cache`It will cache these files on request so you don't need any pesky queues. By default, a cached file will last for 1 day then it will be bypassed. When bypassed if the route is still returning a 200 then it will be re-cached and that cache will start to be served again (documented below to change).

What does it not cover?
-----------------------

[](#what-does-it-not-cover)

- You'll need to implement an invalidation strategy (for the CDN, documented below)
- You'll need to implement `onAfterWrite/onAfterPublish/onAfterUnpublish/onAfterDelete` on `DataObjects` to clear the cache for their linked pages (this will already do that for pages on publish, for other items it's documented below)
- You'll need to prevent caching specific requests for marketing tags such as `cid` (documented below)
- You'll need to decide on a recaching strategy (by default it's on publish of the page and invalidated after a day)
- You'll need to decide on your cache headers (documented below)
- Headers, these are not planned for yet

So what does it cover?
----------------------

[](#so-what-does-it-cover)

- Caching a request during the request lifecycle (this means you won't cache unvisited pages)
- Methods to hook into for invalidating via a CDN
- Functionality to serve the cached files
- Functionality to store the cached files along with database records
- A report on the cached responses
- Filtering to prevent caching incorrect responses
- The ability to programmatically warm the cache
- Automatic re-cache of pages after being cached for a period (defaults to 1 day)

But how does it scale?
----------------------

[](#but-how-does-it-scale)

Good question, this depends on the definition of scale. Cache serving scales up without much worry. The ability to re-cache the entire site is a tricky answer. If most requests to the site are to new uncached pages then you'll need to implement some prewarming strategy. If most of the pages serve the same content then you should be fine but you should look at implementing a whitelist for cacheable query params. Especially if using marketing tags.

Requirements
------------

[](#requirements)

- SilverStripe ^4.0
- A hankering for risk

Installation
------------

[](#installation)

```
composer require adrhumphreys/impetuous dev-master

```

Edit your `public/index.php` file to add the following before the autoloader from composer kicks in:

```
$requestHandler = require '../vendor/adrhumphreys/impetuous/includes/impetuous.php';

if (false !== $requestHandler('cache')) {
    die;
} else {
    header('X-Cache-Miss: ' . date(DateTime::COOKIE));
}
```

By default, the cache time for a file is 1 day to change this edit the line about to the time in seconds you want the cache to last. E.g. `$requestHandler('cache', 60)` will set the cache time to 60 seconds.

You'll need to ensure that the middleware `CacheResponse` is applied as the last middleware in the stack

### Environment values:

[](#environment-values)

`IMPETUOUS_CACHE_DIR`: This is where the cache is stored, defaults to `cache``IMPETUOUS_RECORD`: Determines if we will record the cache in the database too

Prevent caching specific routes:
--------------------------------

[](#prevent-caching-specific-routes)

You'll want to inject over `Cache` as a subclass and update `shouldCache` to return `false` for routes that shouldn't be cached. As an example if you wanted to never cache query params it would look like:

```
