You are here

abstract class TimestampInvalidatorBase in Memcache API and Integration 8.2

Class TimestampInvalidatorBase.

Base class for timestamp-based tag invalidation.

@package Drupal\memcache\Invalidator

Hierarchy

Expanded class hierarchy of TimestampInvalidatorBase

File

src/Invalidator/TimestampInvalidatorBase.php, line 12

Namespace

Drupal\memcache\Invalidator
View source
abstract class TimestampInvalidatorBase implements TimestampInvalidatorInterface {

  /**
   * Allowed timestamp slop.
   *
   * @var float
   */
  protected $tolerance;

  /**
   * TimestampInvalidatorBase constructor.
   *
   * @param float $tolerance
   *   Allowed clock skew between servers, in decimal seconds.
   */
  public function __construct($tolerance = 0.001) {
    $this->tolerance = $tolerance;
  }

  /**
   * Mark a tag as outdated.
   *
   * @param string $tag
   *   Tag to mark as outdated.
   *
   * @return float
   *   New timestamp for tag.
   */
  protected function markAsOutdated($tag) {
    $now = $this
      ->getCurrentTimestamp($this->tolerance);
    $current = $this
      ->getLastInvalidationTimestamp($tag);
    if ($now > $current) {
      $this
        ->writeTimestamp($tag, $now);
      return $now;
    }
    else {
      return $current;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getCurrentTimestamp($offset = 0.0) {

    // @todo Eventually we might want to use a time service instead of microtime().
    // Unfortunately, TimeInterface needs a request object and we don't have
    // that in the bootstrap container.
    return round(microtime(TRUE) + $offset, 3);
  }

  /**
   * {@inheritdoc}
   */
  public abstract function invalidateTimestamp($tag);

  /**
   * {@inheritdoc}
   */
  public abstract function getLastInvalidationTimestamps(array $tags);

  /**
   * Write an updated timestamp for a tag to the backend.
   *
   * @param string $tag
   *   Tag to write.
   * @param float $timestamp
   *   New timestamp to write.
   *
   * @return bool
   *   Success or failure from backend.
   */
  protected abstract function writeTimestamp($tag, $timestamp);

}

Members

Namesort descending Modifiers Type Description Overrides
TimestampInvalidatorBase::$tolerance protected property Allowed timestamp slop.
TimestampInvalidatorBase::getCurrentTimestamp public function Get the current timestamp, optionally offset by a number. Overrides TimestampInvalidatorInterface::getCurrentTimestamp
TimestampInvalidatorBase::getLastInvalidationTimestamps abstract public function Get the last invalidation timestamps of a set of tags. Overrides TimestampInvalidatorInterface::getLastInvalidationTimestamps 1
TimestampInvalidatorBase::invalidateTimestamp abstract public function Invalidate the timestamp of a tag. Overrides TimestampInvalidatorInterface::invalidateTimestamp 1
TimestampInvalidatorBase::markAsOutdated protected function Mark a tag as outdated.
TimestampInvalidatorBase::writeTimestamp abstract protected function Write an updated timestamp for a tag to the backend. 1
TimestampInvalidatorBase::__construct public function TimestampInvalidatorBase constructor. 1
TimestampInvalidatorInterface::getLastInvalidationTimestamp public function Get the last invalidation timestamp of a tag. 1