Cache\Site

Netson\Platform\Cache\Site · All classes

The site's page cache: ask for pages to be cleared (and re-warmed) after a change.

Get it from NetsonCloud::cache(). Fails open: when the site has no page cache, available() is false and flush() returns unavailable without doing anything. Nothing throws.

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();
}

Methods

available()

public function available(): bool

Whether the page cache is available for this site (feature on and paths configured).

Returns bool: false on a site without a page cache, on a machine that is not a platform server (your laptop, CI) and when the platform is in trouble; flush() then does nothing, so checking first is optional

See the example at the top of this page.

reason()

public function reason(): string

Why the page cache is not available here; empty when it is.

Returns string: one line for your own log saying why the feature is off here; empty when available() is true. Meant for people reading logs: the wording may change, so never compare it in code

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})";
}

flush()

public function flush(Cache\Intents $intents, string $source): Result

Hand everything collected in $intents to the platform.

Call it once per change, after collecting. It only hands the request over: the platform clears and re-warms the pages within seconds, after this call has returned. Calling it with nothing collected is harmless.

  • $intents (Cache\Intents): what to clear, re-warm or re-read, collected with the add methods of Cache\Intents
  • $source (string): '' (usual) keeps the labels given to the add methods; a label such as 'import:nightly' replaces them all for this flush

Returns Result: ok: handed over. unavailable: this site has no page cache (or this is not a platform server); nothing was done. failed: the request could not be handed over; reason says why. Your app carries on either way: pages then simply expire on their own schedule

See the example at the top of this page.

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})";
}