public function cas($originalValue, $key, $value, $expire)
{
/*
* If we made it here, we're sure that logically, the CAS applies with
* respect to other operations in this transaction. That means we don't
* have to verify the token here: whatever has already been set/add/
* replace/cas will have taken care of that and we already know this one
* applies on top op that change. We can just fold it in there & update
* the value we set initially.
*/
if (isset($this->keys[$key]) && in_array($this->keys[$key][0], array('set', 'add', 'replace', 'cas'))) {
$this->keys[$key][2]['value'] = $value;
$this->keys[$key][2]['expire'] = $expire;
return;
}
/*
* @param mixed $originalValue
* @param string $key
* @param mixed $value
* @param int $expire
* @return bool
*/
$cache = $this->cache;
$callback = function ($originalValue, $key, $value, $expire) use($cache) {
// check if given (local) CAS token was known
if ($originalValue === null) {
return false;
}
// fetch data from real cache, getting new valid CAS token
$current = $cache->get($key, $token);
// check if the value we just read from real cache is still the same
// as the one we saved when doing the original fetch
if (serialize($current) === $originalValue) {
// everything still checked out, CAS the value for real now
return $cache->cas($token, $key, $value, $expire);
}
return false;
};
$args = array('token' => $originalValue, 'key' => $key, 'value' => $value, 'expire' => $expire);
$this->keys[$key] = array(__FUNCTION__, $callback, $args);
}