Yac::get
(PECL yac >= 1.0.0)
Retrieve values from cache
Description
Retrieve values from cache
Parameters
keyA
stringkey, or anarrayof keys.defaultThe value to return when the requested key (or keys) is not present in the cache, available as of yac 2.4.0. When omitted, a miss returns false.
NoteBefore yac 2.4.0, this argument slot held a by-reference
$castoken rather than a default value. Code that passed or relied on that token must be updated when upgrading to 2.4.0.
Return Values
For a string key, returns the cached value on a hit, otherwise the default (or false when no default was given).
For an array of keys, returns an array containing the found values indexed by their keys. Keys that are not present in the cache are omitted from the result as of yac 2.4.0, or filled with the default when one was given; before 2.4.0, a false placeholder was inserted for every missing key.
Examples
Yac::get example
<?php
$yac = new Yac();
$yac->set("foo", "bar");
var_dump($yac->get("foo")); // string(3) "bar"
var_dump($yac->get("missing")); // bool(false): a miss
?>Telling a stored false apart from a miss
<?php
$yac = new Yac();
// a miss and a stored false are indistinguishable without a default;
// a sentinel default (available as of yac 2.4.0) tells them apart
$yac->set("flag", false);
var_dump($yac->get("flag")); // bool(false): the stored value
var_dump($yac->get("missing", false)); // bool(false): a miss, same shape
var_dump($yac->get("flag", "__NONE__")); // bool(false): the stored value
var_dump($yac->get("missing", "__NONE__")); // string(8) "__NONE__": a miss
?>Getting multiple keys at once
<?php
$yac = new Yac();
$yac->set("foo", "bar");
$yac->set("foo2", "bar2");
// with an array of keys, only the found keys are present in the result
var_dump($yac->get(array("foo", "foo2", "missing")));
// array(2) { ["foo"]=> string(3) "bar" ["foo2"]=> string(4) "bar2" }
?>See Also
Yac::setYac::__get