Skip to content

Core\Cache::memory

Stores values in the memory of this server process, so a later request can use what an earlier one built.

You write a value with put and read it back with get. Every request this server process runs sees the same values. The store needs no configuration and no network, so it is fast. Its size limit is 32 MB by default. When the store is full, it deletes the value whose last write is the oldest.

Any value may be gone at any time, and a key that nothing wrote returns null. So write here only what your program can build again.

Good to know: a restart empties the store. A second server process has a store of its own. If another server must find the value, or your program needs it to be correct, use Core\Cache::shared. In a test with #[Test(at: ...)], a value's lifetime uses the test clock, so Core\Test::advance ends it.

The examples below show a write and a read, a value with a lifetime, and a slow result that one request fetches and later requests find ready.

Return value

Returns Core\Cache\Store. A Core\Cache\Store for the entries of this process. Every request this process serves can read them. An entry can be missing at any time. The cache deletes old entries to stay under its size limit, and another server process has its own entries. If your program is wrong when get returns null, use shared instead.

Changelog

VersionChange
0.0.5In a test with #[Test(at:)], Core\Time::sleep ends at once and moves the test clock forward. A timeout of Core\Task::all and the time to live of a Core\Cache::memory entry use the same clock. Without at:, a sleep waits in real time.

Examples

Writing and reading a value

<?nvs
// Shows how to write a value into the memory cache of this server, and read it back.
var $cache = Core\Cache::memory();
// `put` writes the value under the key "greeting". Every core of this server
// sees the same store, so every request on this server can read this value.
$cache->put('greeting', 'hello');
// `get` returns what was written. This prints hello.
var $greeting = $cache->get('greeting') as string;
echo $greeting, "\n";
// A key nothing wrote returns `null`. `??` gives a value to use instead,
// so this prints no colour yet.
var $colour = $cache->get('colour') ?? 'no colour yet';
echo $colour as string, "\n";
// `forget` removes one key for the whole server. This prints gone.
$cache->forget('greeting');
var $after = $cache->get('greeting') ?? 'gone';
echo $after as string, "\n";
Output
hello
no colour yet
gone

A value that expires

<?nvs
// Shows how to give a cached value a lifetime. When the lifetime is over, the
// key returns `null` again.
var $cache = Core\Cache::memory();
// `ttl` is how long the value stays. Write `30s` for thirty seconds, `5m` for
// five minutes, `1h` for one hour.
$cache->put('exchange-rate', '1.0874', { ttl: 1h });
// The lifetime is not over yet, so `get` returns the value. This prints 1.0874.
var $rate = $cache->get('exchange-rate') as string;
echo $rate, "\n";
// A lifetime of `0s` is over when the write finishes. `get` returns `null`,
// so this prints expired.
$cache->put('one-time-code', '845192', { ttl: 0s });
var $code = $cache->get('one-time-code') ?? 'expired';
echo $code as string, "\n";
// Without a `ttl` the value stays until the store is full and needs the room.
// This prints still here.
$cache->put('country', 'still here');
var $country = $cache->get('country') ?? 'gone';
echo $country as string, "\n";
Output
1.0874
expired
still here

A price list fetched once

<?nvs
// Shows a slow result that one request fetches and every later request on this
// server reads from the cache.
class Prices
{
public static function all(): array<int>
{
var $cache = Core\Cache::memory();
// If the list is in the cache, the function returns it here and skips
// the slow work below.
var $saved = $cache->get('prices');
if ($saved != null) {
return $saved as array<int>;
}
// This is the slow work. A real program calls another service or reads
// a database here. The prices are in cents.
array<int> $fetched = ['cup' => 450, 'poster' => 1200, 'sticker' => 199];
// The list stays for fifteen minutes. After that, the next request
// fetches it again, so a new price is used within fifteen minutes.
$cache->put('prices', $fetched, { ttl: 15m });
return $fetched;
}
}
// The first request fetches the list. This prints 450.
array<int> $first = Prices::all();
echo $first['cup'], "\n";
// The next request reads the list from the cache. This prints 1200.
array<int> $second = Prices::all();
echo $second['poster'], "\n";
Output
450
1200