PHP 8.6.0 Beta 3 is available for testing

Yac::get

(PECL yac >= 1.0.0)

Yac::getRetrieve values from cache

Опис

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

Retrieve values from cache

Параметри

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.

Зауваження:

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.

Значення, що повертаються

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.

Приклади

Приклад #1 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
?>

Приклад #2 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
?>

Приклад #3 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" }
?>

Прогляньте також

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top