/** * Remove stale elements at the top of the queue and return the first real entry * * When data expires, it still leaves a queue entry linking to its * correlation ID. Clear any of these stale entries at the head of * the queue. * * Note that we run this from inside a transaction, to make it less * likely that we'll hit a race condition. * * @param MultiExec $tx transaction we're working within. * * @return string|null Top element's key, or null if the queue is empty. */ public function peekWithCleanup(MultiExec $tx) { for (;;) { // Look up the first element in the FIFO ordering. $values = $tx->zrange(Predis::FIFO_INDEX, 0, 0); if ($values) { // Use that value as a key into the key-value block. $key = $values[0]; $exists = $tx->exists($key); if (!$exists) { // If the data is missing, then remove from the FIFO index. $tx->zrem(Predis::FIFO_INDEX, $key); } else { return $key; } } else { break; } } return null; }