class Memory

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Queue/Memory.php \Drupal\Core\Queue\Memory
  2. 10 core/lib/Drupal/Core/Queue/Memory.php \Drupal\Core\Queue\Memory
  3. 8.9.x core/lib/Drupal/Core/Queue/Memory.php \Drupal\Core\Queue\Memory
  4. main core/lib/Drupal/Core/Queue/Memory.php \Drupal\Core\Queue\Memory

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

Expanded class hierarchy of Memory

Related topics

3 files declare their use of Memory
CronQueueTest.php in core/modules/system/tests/src/Kernel/System/CronQueueTest.php
CronTest.php in core/tests/Drupal/Tests/Core/CronTest.php
QueueTest.php in core/tests/Drupal/KernelTests/Core/Queue/QueueTest.php

File

core/lib/Drupal/Core/Queue/Memory.php, line 14

Namespace

Drupal\Core\Queue
View source
class Memory implements QueueInterface {
  
  /**
   * The queue data.
   *
   * @var array
   */
  protected $queue;
  
  /**
   * Counter for item ids.
   *
   * @var int
   */
  protected $idSequence;
  
  /**
   * Constructs a Memory object.
   *
   * @param string $name
   *   An arbitrary string. The name of the queue to work with.
   */
  public function __construct($name) {
    $this->queue = [];
    $this->idSequence = 0;
  }
  
  /**
   * {@inheritdoc}
   */
  public function createItem($data) {
    $item = new \stdClass();
    $item->item_id = $this->idSequence++;
    $item->data = $data;
    $item->created = \Drupal::time()->getCurrentTime();
    $item->expire = 0;
    $this->queue[$item->item_id] = $item;
    return $item->item_id;
  }
  
  /**
   * {@inheritdoc}
   */
  public function numberOfItems() {
    return count($this->queue);
  }
  
  /**
   * {@inheritdoc}
   */
  public function claimItem($lease_time = 30) {
    foreach ($this->queue as $key => $item) {
      if ($item->expire == 0) {
        $item->expire = \Drupal::time()->getCurrentTime() + $lease_time;
        $this->queue[$key] = $item;
        return $item;
      }
    }
    return FALSE;
  }
  
  /**
   * {@inheritdoc}
   */
  public function deleteItem($item) {
    unset($this->queue[$item->item_id]);
  }
  
  /**
   * {@inheritdoc}
   */
  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;
  }
  
  /**
   * {@inheritdoc}
   */
  public function createQueue() {
    // Nothing needed here.
  }
  
  /**
   * {@inheritdoc}
   */
  public function deleteQueue() {
    $this->queue = [];
    $this->idSequence = 0;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
Memory::$idSequence protected property Counter for item ids.
Memory::$queue protected property The queue data.
Memory::claimItem public function Overrides QueueInterface::claimItem 1
Memory::createItem public function Overrides QueueInterface::createItem
Memory::createQueue public function Overrides QueueInterface::createQueue
Memory::deleteItem public function Overrides QueueInterface::deleteItem
Memory::deleteQueue public function Overrides QueueInterface::deleteQueue
Memory::numberOfItems public function Overrides QueueInterface::numberOfItems
Memory::releaseItem public function Overrides QueueInterface::releaseItem
Memory::__construct public function Constructs a Memory object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.