Skip to content

Core\Cache\SecretEntry::of

Builds the entry that a Core\Cache\Store::getSecret fill hands back.

A fill is a function you give to getSecret. It runs when the cache has no secret under the name you asked for, and it returns one of these entries. The entry carries two things: the secret the fill fetched, and how long that secret stays good.

The lifetime belongs in the entry because the place you fetched from is the only one that knows it. A token endpoint says how long its token lasts, so pass that answer on instead of guessing a lifetime in your own code.

The entry returns nothing itself. Your program builds one for a fill to hand back, and reads the secret out of getSecret.

The examples below show a fill that builds one entry, then a lifetime for each of two sources, then a token endpoint whose own answer becomes the lifetime.

Parameters

  • $valuesecret stringThe secret to store. A plain string is also accepted. The call does not change whether a value is secret. Only the type of the value decides that.
  • $ttlCore\Time\DurationHow long the entry stays readable, counted from the moment it is stored. Use the lifetime that the source of the secret gives, such as a token's expiry.

Return value

Returns Core\Cache\SecretEntry. The entry. It has no methods to read the secret or the lifetime. A fill returns it, and your program reads the secret with getSecret.

Examples

The entry a fill returns

<?nvs
// Shows the entry a fill hands back: the secret it fetched, and how long that
// secret stays good.
secret bytes $key = Core\Crypto::generateKey();
array<secret bytes> $keys = [$key];
var $cache = Core\Cache::memory();
// The fill runs when the cache has no secret under "api-token". It returns one
// entry, and the store seals what the entry carries.
var $fetch = fn(): Core\Cache\SecretEntry => {
echo "asking the service\n";
return Core\Cache\SecretEntry::of('at-7f3c9b21', 15m);
};
// This prints asking the service, then the fill supplied the token.
var $token = $cache->getSecret('api-token', $keys, { fill: $fetch });
echo $token == null ? 'the fill supplied nothing' : 'the fill supplied the token', "\n";
// You read the secret out of `getSecret` and never out of the entry. This
// prints the same token came back.
var $again = $cache->getSecret('api-token', $keys, { fill: $fetch });
echo ($again ?? '') == ($token ?? '-')
? 'the same token came back'
: 'another token came back', "\n";
Output
asking the service
the fill supplied the token
the same token came back

A lifetime for each source

<?nvs
// Shows that every source gets the lifetime it deserves, because the entry
// carries the lifetime and not the store.
secret bytes $key = Core\Crypto::generateKey();
array<secret bytes> $keys = [$key];
var $cache = Core\Cache::memory();
// A short-lived access token. The service replaces it every few minutes, so
// the entry may only be used for two.
var $accessToken = fn(): Core\Cache\SecretEntry =>
Core\Cache\SecretEntry::of('at-7f3c9b21', 2m);
// A signing key from the same service. It is changed twice a year, so this
// entry may be used for a day.
var $signingKey = fn(): Core\Cache\SecretEntry =>
Core\Cache\SecretEntry::of('sk-0a51d7', 1d);
// Both entries live in the same store under names of their own. This prints
// both secrets are in the store.
var $token = $cache->getSecret('access-token', $keys, { fill: $accessToken });
var $signer = $cache->getSecret('signing-key', $keys, { fill: $signingKey });
echo $token != null && $signer != null
? 'both secrets are in the store'
: 'one of the secrets is missing', "\n";
// A lifetime of zero is already over, so nothing is stored. The next read is
// a miss, and this prints nothing was kept.
var $once = fn(): Core\Cache\SecretEntry => Core\Cache\SecretEntry::of('one-time', 0s);
$cache->getSecret('one-time-code', $keys, { fill: $once });
echo $cache->getSecret('one-time-code', $keys) == null ? 'nothing was kept' : 'it was kept', "\n";
Output
both secrets are in the store
nothing was kept

The lifetime the endpoint said

<?nvs
// Shows how the lifetime a token endpoint reports becomes the lifetime of the
// entry, so the program never guesses how long its token lasts.
class Login
{
public secret string $token = '';
// The number of seconds the endpoint said the token is good for.
public uint $expiresIn = 0;
}
class Service
{
// A real program sends a request here and reads the answer. An OAuth token
// endpoint reports the lifetime in a field called `expires_in`.
public static function login(): Login
{
var $answer = new Login();
$answer->token = 'at-7f3c9b21';
$answer->expiresIn = 3600;
return $answer;
}
public static function entry(): Core\Cache\SecretEntry
{
var $answer = Service::login();
// Ninety seconds are taken off, so the token is replaced before it runs
// out rather than during a request that is using it.
uint $safe = $answer->expiresIn - 90;
return Core\Cache\SecretEntry::of($answer->token, Core\Time\Duration::seconds($safe as int));
}
}
secret bytes $key = Core\Crypto::generateKey();
array<secret bytes> $keys = [$key];
var $cache = Core\Cache::memory();
var $fill = fn(): Core\Cache\SecretEntry => Service::entry();
// The first call logs in and stores the token for the time the endpoint
// reported. This prints the token is ready.
var $token = $cache->getSecret('service-token', $keys, { fill: $fill });
echo $token == null ? 'the token is missing' : 'the token is ready', "\n";
// Every later call in this process reads that entry. This prints the same
// token came back.
var $again = $cache->getSecret('service-token', $keys, { fill: $fill });
echo ($again ?? '') == ($token ?? '-')
? 'the same token came back'
: 'another token came back', "\n";
Output
the token is ready
the same token came back