Skip to content

Core\Cache::shared

Stores values in a store on the network, where every server process and every machine sees the same values.

The person who runs the server sets up the store in nvs.toml and grants the capability cache.shared. A program cannot choose the store for itself. You write a value with put and read it back with get. These are the same methods Core\Cache::memory has.

Use this cache when another server must find the value, such as a login session. Use it also when your program would be wrong if the value were missing. A key that nothing wrote returns null. If the server cannot reach the store, the call throws an error. So your program can tell the two cases apart.

Good to know: every read and every write goes over the network, so this cache is slower than Core\Cache::memory. For a value that this server can build again, use Core\Cache::memory.

The examples below show the settings a shared store needs first, then what happens when the server cannot reach the store, then a request that uses the memory cache in that case.

Return value

Returns Core\Cache\Store. A Core\Cache\Store for the configured shared store. Every server process that uses the store reads the same entries.

Errors

  • RuntimeErrorThe program does not have the capability cache.shared. Or no [cache.shared] url is set, or the client cannot read that URL. Each of these is a missing setting, not a failed store.
  • IOErrorThe configured store cannot be reached. The call throws this error. It does not return null, because a missing entry and a failed store mean different things.

Examples

What a shared store needs

<?nvs
// Shows what `Core\Cache::shared` needs before a program can use it. This
// server granted nothing, so the call below throws and the message names the
// setting that is missing.
// The person who runs the server writes both of these blocks in `nvs.toml`.
// A program cannot grant them to itself.
//
// [capabilities.cache]
// shared = true
//
// [cache.shared]
// url = "redis://cache.internal:6379"
try {
var $store = Core\Cache::shared();
echo "the store is ready\n";
} catch (RuntimeError $error) {
echo $error->message, "\n";
}
Output
Core\Cache::shared() needs the capability `cache.shared`, which is not granted
help: add `shared = true` under `[capabilities.cache]` in nvs.toml

A store that is away throws

<?nvs
// Shows how a program tells a missing value apart from a shared store that the
// server cannot reach.
var $memory = Core\Cache::memory();
// Every cache returns `null` for a key that nothing wrote. This prints null.
var $missing = $memory->get('never-written') ?? 'null';
echo $missing as string, "\n";
// A shared store that the server cannot reach throws an error. Your program
// needs this when a wrong value is worse than no value.
try {
Core\Cache::shared();
echo "the store is ready\n";
} catch (Throwable $error) {
echo "the shared store threw instead of answering null\n";
}
Output
null
the shared store threw instead of answering null

Falling back when the store is away

<?nvs
// Shows a request that uses the shared store when the server can reach it, and
// the memory cache of this server when it cannot.
class Sessions
{
public static function store(): Core\Cache\Store
{
try {
return Core\Cache::shared();
} catch (Throwable $away) {
// The value is then in this server process only. A program that
// must not lose the value should let the error through instead.
return Core\Cache::memory();
}
}
}
var $store = Sessions::store();
$store->put('session:8412', 'kim', { ttl: 30m });
// This prints kim, from whichever of the two stores the function returned.
var $who = $store->get('session:8412') ?? 'unknown';
echo $who as string, "\n";
Output
kim