You are here

public function Redis_Queue_PhpRedis::createItem in Redis 7.2

Same name and namespace in other branches
  1. 7.3 lib/Redis/Queue/PhpRedis.php \Redis_Queue_PhpRedis::createItem()

Add a queue item and store it directly to the queue.

Parameters

$data: Arbitrary data to be associated with the new task in the queue.

Return value

TRUE if the item was successfully created and was (best effort) added to the queue, otherwise FALSE. We don't guarantee the item was committed to disk etc, but as far as we know, the item is now in the queue.

Overrides DrupalQueueInterface::createItem

File

lib/Redis/Queue/PhpRedis.php, line 10

Class

Redis_Queue_PhpRedis
@todo Set high expire value to the hash for rotation when memory is empty React upon cache clear all and rebuild path list?

Code

public function createItem($data) {
  $client = $this
    ->getClient();
  $dKey = $this
    ->getKeyForData();
  $qKey = $this
    ->getKeyForQueue();

  // Identifier does not not need to be in the transaction,
  // in case of any error we'll just skip a value in the sequence.
  $id = $client
    ->hincrby($dKey, self::QUEUE_HKEY_SEQ, 1);
  $record = new stdClass();
  $record->qid = $id;
  $record->data = $data;
  $record->timestamp = time();
  $pipe = $client
    ->multi(Redis::PIPELINE);

  // Thanks to the redis_queue standalone module maintainers for
  // this piece of code, very effective. Note that we added the
  // pipeline thought.
  $pipe
    ->hsetnx($dKey, $id, serialize($record));
  $pipe
    ->llen($qKey);
  $pipe
    ->lpush($qKey, $id);
  $ret = $pipe
    ->exec();
  if (!($success = $ret[0] && $ret[1] < $ret[2])) {
    if ($ret[0]) {

      // HSETNEX worked but not the PUSH command we therefore
      // need to drop the inserted data. I would have prefered
      // a DISCARD instead but we are in pipelined transaction
      // we cannot actually do a DISCARD here.
      $client
        ->hdel($dKey, $id);
    }
  }
  return $success;
}