Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / memcache / src / Invalidator / TimestampInvalidatorBase.php
1 <?php
2
3 namespace Drupal\memcache\Invalidator;
4
5 /**
6  * Class TimestampInvalidatorBase.
7  *
8  * Base class for timestamp-based tag invalidation.
9  *
10  * @package Drupal\memcache\Invalidator
11  */
12 abstract class TimestampInvalidatorBase implements TimestampInvalidatorInterface {
13
14   /**
15    * Allowed timestamp slop.
16    *
17    * @var float
18    */
19   protected $tolerance;
20
21   /**
22    * TimestampInvalidatorBase constructor.
23    *
24    * @param float $tolerance
25    *   Allowed clock skew between servers, in decimal seconds.
26    */
27   public function __construct($tolerance = 0.001) {
28     $this->tolerance = $tolerance;
29   }
30
31   /**
32    * Mark a tag as outdated.
33    *
34    * @param string $tag
35    *   Tag to mark as outdated.
36    *
37    * @return float
38    *   New timestamp for tag.
39    */
40   protected function markAsOutdated($tag) {
41     $now = $this->getCurrentTimestamp($this->tolerance);
42     $current = $this->getLastInvalidationTimestamp($tag);
43     if ($now > $current) {
44       $this->writeTimestamp($tag, $now);
45       return $now;
46     }
47     else {
48       return $current;
49     }
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function getCurrentTimestamp($offset = 0.0) {
56     // @todo Eventually we might want to use a time service instead of microtime().
57     // Unfortunately, TimeInterface needs a request object and we don't have
58     // that in the bootstrap container.
59     return round(microtime(TRUE) + $offset, 3);
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   abstract public function invalidateTimestamp($tag);
66
67   /**
68    * {@inheritdoc}
69    */
70   abstract public function getLastInvalidationTimestamps(array $tags);
71
72   /**
73    * Write an updated timestamp for a tag to the backend.
74    *
75    * @param string $tag
76    *   Tag to write.
77    * @param float $timestamp
78    *   New timestamp to write.
79    *
80    * @return bool
81    *   Success or failure from backend.
82    */
83   abstract protected function writeTimestamp($tag, $timestamp);
84
85 }