Yac::add
(PECL yac >= 1.0.0)
Store a value without overwriting an existing one
Description
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.
Parameters
keyA
stringkey, or anarrayofkey => valuepairs to store in one call.valueThe value to store. Every PHP type except
resourcecan be stored. Only used in the single-key form; whenkeyis an array, this argument is instead the optionalttl.ttlTime to live in seconds.
0means the entry never expires by time.
Return Values
Returns true on success, false on failure. A store is also rejected (returning false) when the key already exists and has not expired.
Examples
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
?>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
?>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);
?>See Also
Yac::setYac::getYac::delete