public function cas($token, $key, $value, $expire = 0)
{
$this->client->watch($key);
// check if the value still matches CAS token
$comparison = $this->client->get($key);
if (serialize($comparison) !== $token) {
/*
* HHVM Redis only got unwatch recently
* @see https://github.com/asgrim/hhvm/commit/bf5a259cece5df8a7617133c85043608d1ad5316
*/
if (method_exists($this->client, 'unwatch')) {
$this->client->unwatch();
} else {
// this should also kill the watch...
$this->client->multi()->discard();
}
return false;
}
$ttl = $this->ttl($expire);
// since we're watching the key, this will fail should it change in the
// meantime
$this->client->multi();
/*
* Negative ttl behavior isn't properly documented & doesn't always
* appear to treat the value as non-existing. Let's play safe and just
* delete it right away!
*/
if ($ttl < 0) {
$this->client->del($key);
} else {
$this->client->set($key, $value, $ttl);
}
/** @var bool[] $return */
$return = (array) $this->client->exec();
return !in_array(false, $return);
}