public function add($key, $value, $expire = 0)
{
$ttl = $this->ttl($expire);
/*
* Negative ttl behavior isn't properly documented & doesn't always
* appear to treat the value as non-existing. Let's play safe and not
* even create the value (also saving a request)
*/
if ($ttl < 0) {
return true;
}
if ($ttl === null) {
return $this->client->setnx($key, $value);
}
/*
* I could use Redis 2.6.12-style options array:
* $this->client->set($key, $value, array('xx', 'ex' => $ttl));
* However, this one should be pretty fast already, compared to the
* replace-workaround below.
*/
$this->client->multi();
$this->client->setnx($key, $value);
$this->client->expire($key, $ttl);
/** @var bool[] $return */
$return = (array) $this->client->exec();
return !in_array(false, $return);
}