MemoryQueue

  1. drupal
    1. 8
    2. 7

Static queue implementation.

This allows "undelayed" variants of processes relying on the Queue interface. The queue data resides in memory. It should only be used for items that will be queued and dequeued within a given page request.

Hierarchy

Properties

NameDescription
MemoryQueue::$id_sequenceCounter for item ids.
MemoryQueue::$queueThe queue data.

Functions & methods

NameDescription
MemoryQueue::claimItemClaim an item in the queue for processing. Overrides DrupalQueueInterface::claimItem
MemoryQueue::createItemAdd a queue item and store it directly to the queue. Overrides DrupalQueueInterface::createItem
MemoryQueue::createQueueCreate a queue. Overrides DrupalQueueInterface::createQueue
MemoryQueue::deleteItemDelete a finished item from the queue. Overrides DrupalQueueInterface::deleteItem
MemoryQueue::deleteQueueDelete a queue and every item in the queue. Overrides DrupalQueueInterface::deleteQueue
MemoryQueue::numberOfItemsRetrieve the number of items in the queue. Overrides DrupalQueueInterface::numberOfItems
MemoryQueue::releaseItemRelease an item that the worker could not process, so another worker can come in and process it before the timeout expires. Overrides DrupalQueueInterface::releaseItem
MemoryQueue::__constructStart working with a queue. Overrides DrupalQueueInterface::__construct

modules/system/system.queue.inc, line 303

View source
<?php
class MemoryQueue implements DrupalQueueInterface {
  /**
   * The queue data.
   *
   * @var array
   */
  protected $queue;

  /**
   * Counter for item ids.
   *
   * @var int
   */
  protected $id_sequence;

  public function __construct($name) {
    $this->queue = array();
    $this->id_sequence = 0;
  }

  public function createItem($data) {
    $item = new stdClass();
    $item->item_id = $this->id_sequence++;
    $item->data = $data;
    $item->created = time();
    $item->expire = 0;
    $this->queue[$item->item_id] = $item;
  }

  public function numberOfItems() {
    return count($this->queue);
  }

  public function claimItem($lease_time = 30) {
    foreach ($this->queue as $key => $item) {
      if ($item->expire == 0) {
        $item->expire = time() + $lease_time;
        $this->queue[$key] = $item;
        return $item;
      }
    }
    return FALSE;
  }

  public function deleteItem($item) {
    unset($this->queue[$item->item_id]);
  }

  public function releaseItem($item) {
    if (isset($this->queue[$item->item_id]) && $this->queue[$item->item_id]->expire != 0) {
      $this->queue[$item->item_id]->expire = 0;
      return TRUE;
    }
    return FALSE;
  }

  public function createQueue() {
    // Nothing needed here.
  }

  public function deleteQueue() {
    $this->queue = array();
    $this->id_sequence = 0;
  }
}
?>

Related topics