protected function combineUpdates($updates)
{
$setMulti = array();
$deleteMulti = array();
foreach ($updates as $i => $update) {
$operation = $update[0];
$args = $update[2];
switch ($operation) {
// all set & delete operations can be grouped into setMulti & deleteMulti
case 'set':
unset($updates[$i]);
// only group sets with same expiration
$setMulti[$args['expire']][$args['key']] = $args['value'];
break;
case 'delete':
unset($updates[$i]);
$deleteMulti[] = $args['key'];
break;
default:
break;
}
}
if (!empty($setMulti)) {
$cache = $this->cache;
/*
* We'll use the return value of all deferred writes to check if they
* should be rolled back.
* commit() expects a single bool, not a per-key array of success bools.
*
* @param mixed[] $items
* @param int $expire
* @return bool
*/
$callback = function ($items, $expire) use($cache) {
$success = $cache->setMulti($items, $expire);
return !in_array(false, $success);
};
foreach ($setMulti as $expire => $items) {
$updates[] = array('setMulti', $callback, array($items, $expire));
}
}
if (!empty($deleteMulti)) {
$cache = $this->cache;
/*
* commit() expected a single bool, not an array of success bools.
* Besides, deleteMulti() is never cause for failure here: if the
* key didn't exist because it has been deleted elsewhere already,
* the data isn't corrupt, it's still as we'd expect it.
*
* @param string[] $keys
* @return bool
*/
$callback = function ($keys) use($cache) {
$cache->deleteMulti($keys);
return true;
};
$updates[] = array('deleteMulti', $callback, array($deleteMulti));
}
return $updates;
}