You are here

class SignalCache in Ultimate Cron 8.2

Hierarchy

Expanded class hierarchy of SignalCache

1 string reference to 'SignalCache'
ultimate_cron.services.yml in ./ultimate_cron.services.yml
ultimate_cron.services.yml
1 service uses SignalCache
ultimate_cron.signal in ./ultimate_cron.services.yml
Drupal\ultimate_cron\Signal\SignalCache

File

src/Signal/SignalCache.php, line 9

Namespace

Drupal\ultimate_cron\Signal
View source
class SignalCache implements SignalInterface {

  /**
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  public $cacheBackend;

  /**
   * @var \Drupal\Core\Lock\LockBackendInterface
   */
  public $lockBackend;
  public function __construct(CacheBackendInterface $cache_backend, LockBackendInterface $lock_backend) {
    $this->cacheBackend = $cache_backend;
    $this->lockBackend = $lock_backend;
  }

  /**
   * Get a signal without claiming it.
   *
   * @param string $job_id
   *   The name of the job.
   * @param string $signal
   *   The name of the signal.
   *
   * @return string
   *   The signal if any.
   */
  public function peek($job_id, $signal) {
    $cache = $this->cacheBackend
      ->get("signal-{$job_id}-{$signal}");
    if ($cache) {
      $flushed = $this->cacheBackend
        ->get("flushed-{$job_id}");
      if (!$flushed || $cache->created > $flushed->created) {
        return $cache->data;
      }
    }
    return FALSE;
  }

  /**
   * Get and claim signal.
   *
   * @param string $name
   *   The name of the job.
   * @param string $signal
   *   The name of the signal.
   *
   * @return string
   *   The signal if any. If a signal is found, it is "claimed" and therefore
   *   cannot be claimed again.
   */
  public function get($job_id, $signal) {
    if ($this->lockBackend
      ->acquire("signal-{$job_id}-{$signal}")) {
      $result = self::peek($job_id, $signal);
      self::clear($job_id, $signal);
      $this->lockBackend
        ->release("signal-{$job_id}-{$signal}");
      return $result;
    }
    return FALSE;
  }

  /**
   * Set signal.
   *
   * @param string $job_id
   *   The name of the job.
   * @param string $signal
   *   The name of the signal.
   *
   * @return boolean
   *   TRUE if the signal was set.
   */
  public function set($job_id, $signal) {
    $this->cacheBackend
      ->set("signal-{$job_id}-{$signal}", TRUE);
  }

  /**
   * Clear signal.
   *
   * @param string $job_id
   *   The name of the job.
   * @param string $signal
   *   The name of the signal.
   */
  public function clear($job_id, $signal) {
    $this->cacheBackend
      ->delete("signal-{$job_id}-{$signal}");
  }

  /**
   * Clear signals.
   *
   * @param string $job_id
   *   The name of the job.
   */
  public function flush($job_id) {
    $this->cacheBackend
      ->set("flushed-{$job_id}", microtime(TRUE));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SignalCache::$cacheBackend public property
SignalCache::$lockBackend public property
SignalCache::clear public function Clear signal. Overrides SignalInterface::clear
SignalCache::flush public function Clear signals. Overrides SignalInterface::flush
SignalCache::get public function Get and claim signal. Overrides SignalInterface::get
SignalCache::peek public function Get a signal without claiming it. Overrides SignalInterface::peek
SignalCache::set public function Set signal. Overrides SignalInterface::set
SignalCache::__construct public function