Core\Cache\Store::put
$store->put(string $key, mixed $value, {ttl?: Core\Time\Duration}): void$store->put( string $key, mixed $value, {ttl?: Core\Time\Duration}, ): void
Writes a value into a cache store under a key.
put copies the value into the store. The copy is complete, so a change to your own variable after the write does not change what is stored. A key that already has a value is replaced, and the replacement costs the store the room of one value.
You can give the value a lifetime with the ttl option, for example { ttl: 10m }. After that time the key returns null again. Without a lifetime the value stays until the store needs the room or your program removes it.
Every store has a size limit. When a new value does not fit, the store forgets the value written longest ago. So any key may return null at any time, and your program must be able to build the value again.
The examples below show a write and a replacement first, then a value with a lifetime, then one user's data stored under a key of its own.
Parameters
$keystringThe name to store under. Ataintedstring is allowed, because the key is never part of a returned value.$valuemixedThe value to copy in. It cannot be a callable or asecret. The same rule applies to a value sent to an isolate.
Options
ttlCore\Time\DurationoptionalHow long the entry stays readable, counted from this call. Without it, the entry stays until the cache deletes it to stay under its size limit, or until anotherputreplaces it. Attlof zero or less deletes the entry under the key and stores nothing.
Return value
Returns void. Nothing. A later get can still return null, because an entry can be deleted at any time.
Errors
LogicErrorThe value cannot be stored. It is or contains a callable, or an object with asecretproperty that was not revealed.IOErrorOnly on asharedcache: the store cannot be reached, or it does not accept the write.
Examples
Writing a value
<?nvs// Shows how to write a value into a cache store, and what a second write// under the same key does.
var $cache = Core\Cache::memory();
// `put` writes the value 3 under the key "visits".$cache->put('visits', 3);
// `get` returns what was written. This prints 3.var $visits = $cache->get('visits') as int;echo $visits, "\n";
// A second `put` under the same key replaces the value. This prints 4.$cache->put('visits', 4);var $updated = $cache->get('visits') as int;echo $updated, "\n";
// You can write any value the store accepts, including a list. This prints 2.array<string> $tags = ['new', 'sale'];$cache->put('tags', $tags);var $stored = $cache->get('tags') as array<string>;echo Core\Arr::count($stored), "\n";342A value with a lifetime
<?nvs// Shows how to store a value that should only be used for a short time.
var $cache = Core\Cache::memory();
// The `ttl` option is the lifetime of the entry. This value may be used for// ten minutes.$cache->put('rate-eur-usd', '1.08', { ttl: 10m });
// Inside that time `get` returns the value. This prints 1.08.var $rate = $cache->get('rate-eur-usd') as string;echo $rate, "\n";
// Without a `ttl` the value has no lifetime. It stays until the store needs// the room, or until your program removes it. This prints 1.09.$cache->put('rate-eur-chf', '1.09');var $other = $cache->get('rate-eur-chf') as string;echo $other, "\n";
// A lifetime of zero is already over, so the store does not keep the value.// The read after it returns `null`, and this prints no rate yet.$cache->put('rate-eur-jpy', '170.5', { ttl: 0s });var $expired = $cache->get('rate-eur-jpy') ?? 'no rate yet';echo $expired as string, "\n";1.081.09no rate yetOne users data
<?nvs// Shows how a program stores one user's data under a key of its own. The next// request for that user then does not build it again.
class Profile{ public string $name = ''; public string $city = '';}
class Profiles{ // The work the cache saves. A real program reads a database here. public static function build(string $userId): Profile { var $profile = new Profile(); $profile->name = 'User ' . $userId; $profile->city = 'Berlin'; return $profile; }
public static function of(string $userId): Profile { var $cache = Core\Cache::memory();
// The user id is part of the key, so every user has an entry of their // own. A key may contain values that came from the request. string $key = 'profile-' . $userId;
var $held = $cache->get($key); if ($held != null) { return $held as Profile; }
// The entry is good for five minutes. After that the next call builds // it again, so a change reaches the program without a restart. var $built = Profiles::build($userId); $cache->put($key, $built, { ttl: 5m }); return $built; }}
// The first call builds the profile and stores it. This prints User 4711.var $first = Profiles::of('4711');echo $first->name, "\n";
// The second call finds it in the store. This prints Berlin.var $second = Profiles::of('4711');echo $second->city, "\n";User 4711Berlin