(PECL yac >= 1.0.0)
Yac::add — Store a value without overwriting an existing one
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.
Example #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
?>Example #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
?>Example #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);
?>