cipi / cache
Purge pages of Cipi's nginx page cache from any PHP app — by path, prefix, URL or Laravel route.
Requires
- php: ^8.1
Requires (Dev)
- phpunit/phpunit: ^10.5 || ^11.0
Suggests
- illuminate/support: For the Laravel integration: facade, middleware, model trait, artisan commands
Provides
None
Conflicts
None
Replaces
None
README
Purge pages of Cipi's nginx page cache from any PHP app — by path, by prefix, by URL, or by Laravel route — and mark which responses may be cached at all.
On a Cipi server, cipi cache <app> on has nginx keep the pages PHP makes and answer the next visitors without running PHP (Page cache). A page then stays the old one until it expires. With this package, the code that changes some content purges the pages that show it — and only those.
composer require cipi/cache
PHP 8.1+. No dependency. For WordPress there is a plugin: cipi-sh/cache-wp.
Plain PHP
use Cipi\Cache\PageCache; $cache = PageCache::forThisApp(); // the app PHP runs as — on Cipi, the Linux user is the app $cache->purge('/blog/hello-world/'); // that page $cache->purge('/blog/*', '/'); // every page under /blog/, and the home page $cache->purge('https://shop.example.com/x/'); // that page on that host only $cache->purgeUnder('/blog/'); // the same as /blog/* $cache->clear(); // every page
| A page | Purges |
|---|---|
/blog/hello-world/ |
That page on every domain and alias of the app, http and https, GET and HEAD, with its ?utm_… variants |
/blog/* |
Every page whose path starts so: /blog/, /blog/page/2/, /blog/feed/ |
https://www.example.com/x/ |
That page on that host only. The host must be one of the app's |
A query string or a fragment is dropped (the path purges those variants); what a browser would encode is encoded. A page that is not one — no leading /, a host that is not a host — is an InvalidArgumentException before anything is purged. A purge Cipi refuses is a PurgeFailed, with the Result.
Many changes, one call
$cache = PageCache::forThisApp()->defer(); // … during the request, wherever content changes: $cache->purge('/blog/hello-world/'); $cache->purgeUnder('/category/news/'); // once, at the end of the script (or now: $cache->flush())
Deferred, every purge of a request is gathered and sent once — at flush() or when the script ends. A page under a prefix that is also asked for is not sent twice; past 1500 pages, one clear of everything is sent instead.
Which responses may be cached
PageCache::cacheFor(600); // Cache-Control: public, max-age=600 — nginx may keep this page 10 minutes PageCache::neverCache(); // Cache-Control: private, no-store — never
nginx keeps a page only when nothing says otherwise: no cookie set, no private or no-cache. See what is never cached.
Warm the pages that matter
$cache->purge('/', '/blog/*'); PageCache::warm(['https://example.com/', 'https://example.com/blog/']); // asked for again: cached before a visitor comes
Laravel
The service provider and the CipiCache facade are discovered.
use Cipi\Cache\Laravel\Facades\CipiCache; CipiCache::purge('/blog/hello-world/'); CipiCache::purgeRoute('posts.show', $post); // the path of a named route CipiCache::purgeRouteUnder('posts.index'); // that page and everything under it CipiCache::clear();
In a request, a queued job or a command, purges are gathered and sent once, when it ends.
Routes whose pages nginx may keep
Laravel marks every response no-cache, private, and the web group sets a session cookie: nginx keeps neither. The cipi.cache middleware makes a route's page cacheable for visitors who have no cookie yet:
Route::get('/blog/{post:slug}', [PostController::class, 'show']) ->name('posts.show') ->middleware('cipi.cache:3600'); // kept an hour (config cipi-cache.ttl when not said) Route::get('/account', AccountController::class)->middleware('cipi.nocache');
A successful GET of a visitor without cookies answers Cache-Control: public, max-age=3600 and sets no cookie, so nginx can keep it. It works inside the web group: the session and CSRF cookies those middleware add after the route's are taken out too, by a middleware the service provider puts first. A visitor with a session — signed in, a cart — is never served from the cache: nginx sees the cookie and passes the request to PHP.
Only on pages that are the same for everyone, with no form that carries a CSRF token: a cached token is someone else's.
A model that purges its own pages
use Cipi\Cache\Laravel\PurgesPageCache; class Post extends Model { use PurgesPageCache; public function cachedPages(): array { return [ route('posts.show', $this, false), // its page '/blog/*', // the listing and its pagination '/', ]; } }
Saved or deleted, the model's pages are purged — and its old ones after a change of slug.
Artisan
php artisan cipi:cache:purge /blog/hello-world/ '/blog/*' php artisan cipi:cache:purge --route=posts.index php artisan cipi:cache:purge --all php artisan cipi:cache:check # does this app reach Cipi?
Configuration
php artisan vendor:publish --tag=cipi-cache for config/cipi-cache.php. Everything has a default:
| Key | .env |
Default |
|---|---|---|
enabled |
CIPI_CACHE_ENABLED |
on in production, off elsewhere: a purge on a laptop does nothing and says so |
app |
CIPI_CACHE_APP |
the Linux user PHP runs as |
driver |
CIPI_CACHE_DRIVER |
sudo |
api.endpoint, api.token |
CIPI_API_URL, CIPI_API_TOKEN |
for the api driver |
defer |
— | true |
ttl |
— | 600, for cipi.cache without a time |
How a purge reaches Cipi
sudo (default) — on the Cipi server, as the app's own user:
sudo -n /usr/local/bin/cipi-cache-clear <app> [<page>…]
Cipi lets the app's user ask root for exactly that, with no password and for its own app only. The package runs it without a shell (proc_open with an argument list), 500 pages per call.
api — from anywhere: a queue worker on another machine, a CMS elsewhere, a CI job. Through the server's Cipi API, POST /api/apps/{app}/cache/purge, with a token that has the nginx-manage ability:
use Cipi\Cache\Runners\ApiRunner; $cache = new PageCache('blog', new ApiRunner('https://cipi-api.example.com', $token));
Behind Cloudflare Access, pass the service token's headers as the fifth argument (CIPI_API_CF_ACCESS_CLIENT_ID, …_SECRET in Laravel).
Tests — Cipi\Cache\Runners\FakeRunner records what would be purged:
$fake = new FakeRunner(); $cache = new PageCache('blog', $fake); // … $fake->pages(); // every page purged $fake->cleared(); // was every page purged
Requirements
PHP 8.1+, on a Cipi server whose apps can purge by page — a current Cipi (sudo cipi self-update). With the api driver, cipi/api 1.48+.
Tests
php tests/run.php
License
MIT