Yac::set
Store a value into the cache
Description
public bool Yac::set(stringarray $key, mixed $value, int $ttl = 0)
public bool Yac::set(array $values, int $ttl = 0)
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
Example #1 Yac::set example
<?php
$yac = new Yac();
$yac->set("foo", "bar"); // store a single value
$yac->set("foo", "baz"); // overwrite the existing entry
?>
Example #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
?>
Example #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));
?>
See Also
- Yac::add
- Yac::get
- Yac::__set