Cache\Intents
Netson\Platform\Cache\Intents · All classes
What to clear from the page cache: specific URLs, the whole site, URLs to re-warm, or the sitemaps. Collect everything one change affects, then pass it to Cache\Site::flush().
Use one Intents per change (one save, one import, one request): adding the same URL twice is harmless, and flushing once at the end means one request to the platform instead of many. Asking for the whole site replaces any URLs collected before it.
URLs are absolute and on the site's own hostname, as visitors use them:
https://www.example.nl/news/hello, built with Laravel's url(). A URL on another
hostname (also example.nl when the site is www.example.nl) makes the platform
discard the whole request, so build them from your app's own base URL.
Every add method takes a $source: a short label for what caused the change, such
as 'news:published' or 'menu:saved'. It appears in the platform's cache log, so
when you ask us why a page was (or was not) cleared, we can tell you which change
did it. Use <what>:<event> in lower case; an id is fine ('article:saved:1234').
Never put personal data in it. When several calls feed one flush, the log keeps the
first label and counts the others.
Example
/**
* After content changed: clear the pages that show it, fetch new pages into the cache
* and let the platform re-read the sitemap. Returns one line for your log.
*
* @param string $base the site's own base URL, e.g. url('/')
* @param list<string> $changedPaths pages that show the change, e.g. ['/articles/hello', '/articles']
* @param list<string> $newPaths pages that did not exist before, e.g. ['/articles/hello']
* @param bool $everyPage true when the change is on every page (menu, footer)
*/
function contentChanged(string $base, array $changedPaths, array $newPaths, bool $everyPage): string
{
$cache = app(NetsonCloud::class)->cache();
if (!$cache->available()) {
return 'page cache not available: ' . $cache->reason();
}
$changes = new Intents();
if ($everyPage) {
$changes->addAll('menu:saved');
} else {
$changes->addUrls(array_map(fn (string $p): string => $base . $p, $changedPaths), 'article:saved');
}
if ($newPaths !== []) {
$changes->addWarmUrls(array_map(fn (string $p): string => $base . $p, $newPaths), 'article:published');
$changes->addSitemaps('article:published'); // the sitemap now lists more pages
}
if ($changes->isEmpty()) {
return 'page cache: nothing to clear';
}
$result = $cache->flush($changes, '');
return $result->isOk() ? 'page cache: requested' : "page cache: {$result->status} ({$result->reason})";
}
Methods
addUrls()
public function addUrls(array $urls, string $source): void
Ask for these URLs to be purged from the page cache.
Use it for the pages a change shows up on: the page itself and the overviews that list it. The next visitor gets a fresh copy, and the platform re-warms them.
$urls(array): absolute URLs on the site's own hostname, e.g.[url('/news/hello'), url('/news')]; non-strings and empty strings are skipped$source(string): what caused this, e.g.'news:published'(see the class description)
See the example at the top of this page.
Example
/**
* Clear the cached news overview and the item that changed.
*/
function purgeNewsPages(string $base, string $slug): bool
{
if (!class_exists(NetsonCloud::class)) {
return false; // no library on this machine: nothing to purge
}
$cache = app(NetsonCloud::class)->cache();
if (!$cache->available()) {
return false; // this site has no page cache
}
$changes = new Intents();
$changes->addUrls(["{$base}/news/{$slug}", "{$base}/news"], 'news:published');
return $cache->flush($changes, '')->isOk();
}
addAll()
public function addAll(string $source): void
Ask for the whole site's page cache to be purged.
For a change that shows up on every page, such as the menu, the footer or the site's settings. It replaces any URLs added before it; use it sparingly, as every page is then built again on its next visit.
$source(string): what caused this, e.g.'menu:saved'(see the class description)
See the example at the top of this page.
addWarmUrls()
public function addWarmUrls(array $urls, string $source): void
Ask for these URLs to be fetched into the page cache right away.
For a page that is new, such as an article published a moment ago: the platform does not know it yet, so it is not cleared, it is visited once so the first real visitor already gets the fast, cached copy. Asking for the whole site does not cancel it.
$urls(array): absolute URLs on the site's own hostname, e.g.[url('/news/hello')]; non-strings and empty strings are skipped$source(string): what caused this, e.g.'news:published'(see the class description)
See the example at the top of this page.
addSitemaps()
public function addSitemaps(string $source): void
Ask the platform to re-read this site's sitemaps.
The platform finds the pages it keeps warm through the site's sitemaps. After a change that adds or removes pages, ask it to read them again instead of waiting for its regular refresh. Asking for the whole site already includes this.
$source(string): what caused this, e.g.'news:published'(see the class description)
See the example at the top of this page.
isEmpty()
public function isEmpty(): bool
Whether nothing was asked for yet.
Returns bool: true when no add method was called with something to do; then there is no need to call Cache\Site::flush()
See the example at the top of this page.