Skip to content

Core\Cache\Store::get

$store->get(string $key): mixed$store->get(
    string $key,
): mixed

Reads a value back out of a cache store.

get returns what put wrote under the key. The value is copied out of the store, so a change to what you got back does not change the entry, and two reads of one key give you two values.

A key that nothing wrote returns null. So does a key whose lifetime has run out, and a key the store had to forget to make room. Any key may return null at any time, so your program must be able to build the value again.

The return type is mixed, because a store holds whatever was written into it. Write as after the call to get the type you need, for example as string. Write ?? after the call to give a value to work with when the key is missing.

The examples below show a read and a missing key first, then reading values back with their types, then the pattern most programs use: read, build when the value is missing, and store.

Parameters

  • $keystringThe name to read. A tainted string is allowed, as on put.

Return value

Returns mixed. The value as it was copied in, or null when there is no entry. An entry can be missing at any time, so your program has to handle null.

Errors

  • ParseErrorThe entry contains a class that this program does not have. Core\Serialize::decode throws the same error. This happens when a new version of the program changes its classes but the store keeps old entries.
  • IOErrorOnly on a shared cache: the store cannot be reached. A missing entry returns null on both caches and is not an error.

Examples

Reading a value

<?nvs
// Shows how to read a value out of a cache store, and what a missing key
// returns.
var $cache = Core\Cache::memory();
$cache->put('greeting', 'hello');
// `get` returns what was written under the key. This prints hello.
var $greeting = $cache->get('greeting') as string;
echo $greeting, "\n";
// Reading does not remove the value. The second read returns the same text,
// so this prints hello again.
var $again = $cache->get('greeting') as string;
echo $again, "\n";
// A key that nothing wrote returns `null`. `??` supplies a value to use
// instead, so this prints no colour yet.
var $colour = $cache->get('colour') ?? 'no colour yet';
echo $colour as string, "\n";
// You can also test for the missing value yourself. This prints the key is
// missing.
var $held = $cache->get('colour');
if ($held == null) {
echo "the key is missing\n";
}
Output
hello
hello
no colour yet
the key is missing

Reading a value with its type

<?nvs
// Shows how to read a stored value back with the type your program needs.
// `get` returns `mixed`, because a store holds whatever was written into it.
class Article
{
public string $title = '';
}
var $cache = Core\Cache::memory();
array<string> $written = ['food', 'recipe'];
var $article = new Article();
$article->title = 'Bread and salt';
$cache->put('views', 42);
$cache->put('tags', $written);
$cache->put('article', $article);
// `as int` gives a whole number. This prints 42.
var $views = $cache->get('views') as int;
echo $views, "\n";
// `as array<string>` gives the list of texts. This prints 2.
var $tags = $cache->get('tags') as array<string>;
echo Core\Arr::count($tags), "\n";
// `as Article` gives the object back. This prints Bread and salt.
var $stored = $cache->get('article') as Article;
echo $stored->title, "\n";
Output
42
2
Bread and salt

Building the value when it is missing

<?nvs
// Shows the way most programs use a cache: read first, build the value only
// when it is missing, and store what was built.
class Menu
{
public static function links(): array<tainted string>
{
var $cache = Core\Cache::memory();
// A value that is there is used as it is, and the work below is
// skipped.
var $held = $cache->get('menu');
if ($held != null) {
return $held as array<string>;
}
// This is the work the cache saves. A real program reads a database
// here.
array<string> $built = [
'home' => '/',
'shop' => '/shop',
'contact' => '/contact',
];
// The menu is good for one hour. After that the next call builds it
// again.
$cache->put('menu', $built, { ttl: 1h });
return $built;
}
}
// The first call builds the menu. This prints /shop.
array<tainted string> $first = Menu::links();
echo $first['shop'], "\n";
// The second call reads it from the store. This prints /contact.
array<tainted string> $second = Menu::links();
echo $second['contact'], "\n";
Output
/shop
/contact