PHP Conference Ehime 2026

Yac::set

(PECL yac >= 1.0.0)

Yac::setStore a value into the cache

Beschreibung

public function Yac::set(string|array $key, mixed $value, int $ttl = 0): bool
public function 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.

Parameter-Liste

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.

Rückgabewerte

Returns true on success, false on failure.

Beispiele

Beispiel #1 Yac::set() example

<?php
$yac = new Yac();

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

Beispiel #2 Setting an entry with a TTL

<?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
?>

Beispiel #3 Setting multiple entries at once

<?php
$yac = new Yac();

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

Siehe auch

add a note

User Contributed Notes

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