PHP 8.6.0 Beta 3 is available for testing

Yac::add

(PECL yac >= 1.0.0)

Yac::addStore a value without overwriting an existing one

Опис

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

Stores a value in the cache. Unlike Yac::set(), it does not overwrite an existing entry that is still valid; the store is rejected in that case.

Параметри

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.

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

Returns true on success, false on failure. A store is also rejected (returning false) when the key already exists and has not expired.

Приклади

Приклад #1 Yac::add() example

<?php
$yac = new Yac();

var_dump($yac->add("foo", "bar"));          // bool(true)
var_dump($yac->add("foo", "baz"));          // bool(false): "foo" already exists
?>

Приклад #2 Adding an entry with a TTL

<?php
$yac = new Yac();

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

Приклад #3 Adding multiple entries at once

<?php
$yac = new Yac();

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

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

add a note

User Contributed Notes

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