Cache

Use the cache when a value takes work to compute or fetch and several requests can reuse it. For example, a catalogue can cache the available stock for each SKU. The stock store remains the source of truth, and the cache holds a result that can be rebuilt.

Usage

Ask for CacheInterface in your service constructor. Atom's built-in cache service supplies the file-backed implementation. In this example, StockRepository is an application class that reads and updates inventory.

app/Catalog/StockService.php
<?php

namespace App\Catalog;

use Atom\Cache\CacheInterface;

final readonly class StockService
{
    public function __construct(
        private CacheInterface $cache,
        private StockRepository $stock,
    ) {
    }

    public function availableUnits(string $sku): int
    {
        return $this->cache->remember(
            "catalog.stock:{$sku}",
            300,
            fn (): int => $this->stock->availableUnits($sku),
        );
    }

    public function receiveDelivery(string $sku, int $units): void
    {
        $this->stock->addUnits($sku, $units);
        $this->cache->delete("catalog.stock:{$sku}");
    }
}

Read the total twice

On the first read for a SKU, remember() calls the repository and caches the returned value for 300 seconds. A second read within that window returns the stored value. If several workers miss the same key at once, the file cache holds a per-key lock while it computes and stores the result.

Refresh after a delivery

After a successful stock update, delete the matching key. The next read fetches the new total. The SKU is part of the key, so updating notebooks leaves the pencil case entry available.

Method When to use it
remember($key, $ttl, $factory) Compute a value on demand and reuse it on later reads.
set($key, $value, $ttl) Store a value you already have.
get($key, $default) Read a value and use a fallback when the entry is missing.
has($key) Check whether an unexpired entry exists.
delete($key) Invalidate a single entry after a change.
add($key, $value, $ttl) Store a value only when the key has no current entry. Returns whether it was stored.
increment($key, $amount, $ttl) Add to an integer counter and get the new count.
clear() Remove all entries in the active cache namespace.

Values must be serializable. A stored null or false is returned as stored. Use has() when you need to distinguish a cached null from a miss.

Configuration

CACHE_DIRECTORY=@root/storage/cache
CACHE_PREFIX=atom
CACHE_DEFAULT_TTL=0

TTL values are in seconds. Pass null to use the configured default, 0 for no expiry, or a DateInterval for a duration. A negative TTL removes or declines to store the value. Cache instances using different prefixes have separate namespaces even when they share a directory.

Atomic operations

The file cache uses per-key locks for remember(), add(), and increment(). Workers sharing the same cache directory and prefix can use these methods when a read and write need to happen together.

$wasFirst = $cache->add("catalog.banner.seen", true, 30);
$views = $cache->increment("catalog.views:notebook", amount: 1, ttl: 10);

Count catalogue views in a fixed window

Call increment() when a product page is viewed. The first call for the notebook returns 1 and starts a ten-second window. Another view within that window returns 2 and keeps the original expiration. After it expires, the next view starts a new window at 1. Use the SKU in the key to count products independently.

Maintenance commands

Prune expired previews and keep published prices

Suppose a storefront caches a draft price briefly but keeps its published price until the next update.

$cache->set("price.preview", "12.50", 1);
$cache->set("price.published", "14.00", 0);

Once the preview has expired, run php atom cache:prune to remove expired files even when no request reads their keys. The published price has no expiry and stays available. Use php atom cache:clear when you intend to remove every entry under the configured prefix.