Core\Cache\Store::getSecret
$store->getSecret(
string $key,
array<secret bytes> $keys,
{fill?: callable(): Core\Cache\SecretEntry, wait?: Core\Time\Duration},
): ?secret stringReads a secret out of a cache store, and can fetch it when it is not there.
getSecret opens the entry that Core\Cache\Store::putSecret sealed. Every key of $keys is tried, so an entry an older key sealed still opens after you put a new key at the front of the list.
The result is null when nothing opens: no entry under that name, an entry whose lifetime is over, or an entry none of your keys opens. Each of these is a miss and not an error, so a program that replaced its keys simply fetches the value again.
The fill option is a function that fetches the value. It runs on a miss, and only one caller in this process runs it while the others wait. It returns a Core\Cache\SecretEntry, which carries the secret and how long that secret stays good. The wait option is how long this caller waits.
The examples below show a read and a miss, then a fill that fetches on a miss, then an access token that every request needs.
Parameters
$keystringThe name to read. Use the same name as inputSecret, because the name is part of the encrypted entry.$keysarray<secret bytes>The list of keys, newest first. Every key is tried. After you add a new key, an entry encrypted with an older key in the list can still be read.
Options
fillcallable(): Core\Cache\SecretEntryoptionalA callable that runs when the entry is missing. It returns aCore\Cache\SecretEntrywith the secret and how long it stays valid. Only one caller in this process runs it, while the others wait. It runs in that caller's request, with that caller's capabilities. Iffillthrows, the waiting callers get no value, and the next caller runsfillagain. An error in one request never reaches another request.waitCore\Time\DurationoptionalHow long this caller waits for another caller'sfill. The default is[cache.memory] fill_wait. You cannot wait forever.
Return value
Returns ?secret string. The secret, or null. An entry that no key in $keys can decrypt returns null. An expired entry also returns null. Neither is an error, so after a key change the secret is fetched again. A plain get on the same name also returns null, because getSecret is the only way to read a secret.
Errors
LogicError$keysis empty, or one of its keys does not have the required length, as onputSecret. It is also thrown whenfillreads the same key it is filling, because that call would wait for itself.TimeoutErrorAnother caller in this process was still runningfillwhen this call'swaitwas up, which defaults to[cache.memory] fill_wait.IOErrorOnly on asharedcache: the store cannot be reached. A missing entry returnsnull, as onget.
Examples
Reading a secret
<?nvs// Shows how to read a secret back out of a cache store, and what a read// answers when there is nothing to read.
secret bytes $key = Core\Crypto::generateKey();array<secret bytes> $keys = [$key];
var $cache = Core\Cache::memory();secret string $token = 'sk-8f2c41';$cache->putSecret('api-token', $token, 1h, $keys);
// The same key list opens the entry. This prints the token is back.echo ($cache->getSecret('api-token', $keys) ?? '') == $token ? 'the token is back' : 'the token is gone', "\n";
// A name nothing was stored under is `null`. This prints null.echo $cache->getSecret('no-such-name', $keys) == null ? 'null' : 'a value', "\n";
// A lifetime that is already over is `null` too. This prints null.$cache->putSecret('expired', $token, 0s, $keys);echo $cache->getSecret('expired', $keys) == null ? 'null' : 'a value', "\n";
// So is an entry that none of your keys opens. Each of these is a miss, and// none of them is an error. This prints null.array<secret bytes> $other = [Core\Crypto::generateKey()];echo $cache->getSecret('api-token', $other) == null ? 'null' : 'a value', "\n";the token is backnullnullnullFetching on a miss
<?nvs// Shows how the `fill` option fetches a secret that is not in the store. Your// program then has one line for the hit and the miss together.
secret bytes $key = Core\Crypto::generateKey();array<secret bytes> $keys = [$key];
var $cache = Core\Cache::memory();
// The function runs only on a miss. It returns a `Core\Cache\SecretEntry`: the// secret it fetched, and how long that secret stays good.var $fetch = fn(): Core\Cache\SecretEntry => { echo "asking the service\n"; return Core\Cache\SecretEntry::of('at-7f3c9b21', 15m);};
// The store is empty, so this call runs the function. It prints asking the// service, then the first read got it.var $first = $cache->getSecret('service-token', $keys, { fill: $fetch });echo $first == null ? 'the first read got nothing' : 'the first read got it', "\n";
// The entry is in the store now, so this call does not run the function again.// It prints the second read got the same token.var $second = $cache->getSecret('service-token', $keys, { fill: $fetch });echo ($second ?? '') == ($first ?? '-') ? 'the second read got the same token' : 'the second read got another token', "\n";
// `fill` is an option. A read without it answers `null` on a miss and fetches// nothing. This prints null.echo $cache->getSecret('another-token', $keys) == null ? 'null' : 'a value', "\n";asking the servicethe first read got itthe second read got the same tokennullA token every request needs
<?nvs// Shows how a program gets the access token that every outbound call needs.// One request fetches the token and every other request reads it.
class Api{ // A real program calls the token endpoint here. The endpoint says how long // the token lasts, and that is the lifetime of the entry. public static function login(): Core\Cache\SecretEntry { echo "logging in\n"; return Core\Cache\SecretEntry::of('at-7f3c9b21', 30m); }
public static function token(array<secret bytes> $keys): ?secret string { var $cache = Core\Cache::memory();
// `wait` is how long this request waits while another request logs in. // Without it the setting `[cache.memory] fill_wait` decides. return $cache->getSecret('api-token', $keys, { fill: fn(): Core\Cache\SecretEntry => Api::login(), wait: 3s, }); }}
secret bytes $key = Core\Crypto::generateKey();array<secret bytes> $keys = [$key];
// The first request logs in. This prints logging in, then request 1 has a// token.echo Api::token($keys) == null ? 'request 1 has no token' : 'request 1 has a token', "\n";
// The next two requests read the token out of the store, so the program logs// in once for all three. Each of them prints that it has a token.echo Api::token($keys) == null ? 'request 2 has no token' : 'request 2 has a token', "\n";echo Api::token($keys) == null ? 'request 3 has no token' : 'request 3 has a token', "\n";logging inrequest 1 has a tokenrequest 2 has a tokenrequest 3 has a token