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

Yac::set

PHP function Edit on GitHub ✎

(PECL yac >= 1.0.0)

Store a value into the cache

Description

Yac::set(string|array $key, mixed $value, int $ttl = 0): bool
Yac::set(array $values, int $ttl = 0): bool

Stores a value in the cache. If the key already exists, the existing entry is overwritten, regardless of whether it has expired.

Parameters

key

A string key, or an array of key => value pairs to store in one call.

value

The value to store. Every PHP type except resource can be stored. Only used in the single-key form; when key is an array, this argument is instead the optional ttl.

ttl

Time to live in seconds. 0 means the entry never expires by time.

Return Values

Returns true on success, false on failure.

Examples

Yac::set example

php
<?php
$yac = new Yac();

$yac->set("foo", "bar");                    // store a single value
$yac->set("foo", "baz");                    // overwrite the existing entry
?>

Setting an entry with a TTL

php
<?php
$yac = new Yac();

// ttl in seconds: the entry expires after 5 seconds
$yac->set("short-lived", "value", 5);
sleep(6);
var_dump($yac->get("short-lived"));        // bool(false): expired
?>

Setting multiple entries at once

php
<?php
$yac = new Yac();

// store several key => value pairs with one call
$yac->set(array("a" => 1, "b" => 2));
?>

See Also

  • Yac::add
  • Yac::get
  • Yac::__set

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