Commit de03e20c authored by bdragon's avatar bdragon Committed by bdragon
Browse files

Issue #2996621 by bdragon, Fabianx: Create timestamp service, which both cache...

Issue #2996621 by bdragon, Fabianx: Create timestamp service, which both cache tags and deleteAll() can use
parent f30543ba
<?php
namespace Drupal\memcache\Invalidator;
use Drupal\memcache\Driver\MemcacheDriverFactory;
/**
* Class MemcacheTimestampInvalidator.
*/
class MemcacheTimestampInvalidator extends TimestampInvalidatorBase {
/**
* @var \Drupal\memcache\DrupalMemcacheInterface
*/
protected $memcache;
/**
* MemcacheTimestampInvalidator constructor.
*
* @param MemcacheDriverFactory $memcache_factory
* @param string $bin Memcache bin to store the timestamps in.
* @param float $tolerance Allowed clock skew between servers, in decimal seconds.
*/
public function __construct(MemcacheDriverFactory $memcache_factory, $bin = 'memcache_invalidation_timestamps', $tolerance = 0.001) {
parent::__construct($tolerance);
$this->memcache = $memcache_factory->get($bin);
}
/**
* {@inheritdoc}
*/
public function invalidateTimestamp($tag) {
return $this->markAsOutdated($tag);
}
/**
* {@inheritdoc}
*/
public function getLastInvalidationTimestamp($tag) {
return $this->memcache->get($tag);
}
/**
* {@inheritdoc}
*/
public function getLastInvalidationTimestamps($tags) {
return $this->memcache->getMulti($tags);
}
/**
* {@inheritdoc}
*/
protected function writeTimestamp($tag, $timestamp) {
return $this->memcache->set($tag, $timestamp);
}
}
<?php
namespace Drupal\memcache\Invalidator;
/**
* Class TimestampInvalidatorBase
*
* Base class for timestamp-based tag invalidation.
*
* @package Drupal\memcache\Invalidator
*/
abstract class TimestampInvalidatorBase implements TimestampInvalidatorInterface {
/**
* @var float Allowed timestamp slop.
*/
protected $tolerance;
/**
* TimestampInvalidatorBase constructor.
*
* @param float $tolerance Allowed clock skew between servers, in decimal seconds.
*/
function __construct($tolerance = 0.001) {
$this->tolerance = $tolerance;
}
/**
* Mark a tag as outdated.
*
* @param $tag string 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}
*/
abstract public function invalidateTimestamp($tag);
/**
* {@inheritdoc}
*/
abstract public function getLastInvalidationTimestamps($tags);
/**
* Write an updated timestamp for a tag to the backend.
*
* @param $tag string Tag to write.
* @param $timestamp float New timestamp to write.
*
* @return bool Success or failure from backend.
*/
abstract protected function writeTimestamp($tag, $timestamp);
}
<?php
namespace Drupal\memcache\Invalidator;
/**
* Interface TimestampInvalidatorInterface
*
* Defines an interface for timestamp-based tag invalidation.
*
* @package Drupal\memcache\Invalidator
*/
interface TimestampInvalidatorInterface {
/**
* Invalidate the timestamp of a tag.
*
* @param $tag string Tag to invalidate.
*
* @return float New timestamp of tag.
*/
public function invalidateTimestamp($tag);
/**
* Get the last invalidation timestamp of a tag.
*
* @param $tag string Tag to check.
*
* @return float The last invalidation timestamp of the tag.
*/
public function getLastInvalidationTimestamp($tag);
/**
* Get the last invalidation timestamps of a set of tags.
*
* @param $tags array Array of tags to check (keys are ignored.)
*
* @return array | bool The last invalidation timestamps on file, or FALSE
* on failure.
*/
public function getLastInvalidationTimestamps($tags);
/**
* Get the current timestamp, optionally offset by a number.
*
* The standard granularity of the resulting timestamp is three decimal places,
* (1 millisecond).
*
* @param float $offset Offset to apply to timestamp before rounding.
*
* @return float Current timestamp in decimal seconds.
*/
public function getCurrentTimestamp($offset = 0.0);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment