php8.5
Home/ Manual/ yac / yac/ Yac::get

Yac::get

PHP function Edit on GitHub ✎

(PECL yac >= 1.0.0)

Retrieve values from cache

Description

Yac::get(string|array $key, mixed $default = null): mixed

Retrieve values from cache

Parameters

key

A string key, or an array of keys.

default

The 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.

Note

Before yac 2.4.0, this argument slot held a by-reference $cas token 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
<?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
<?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
<?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::set
  • Yac::__get

Source: reference/yac/yac/get.xml · from the official PHP manual (php/doc-en)