+
+ /**
+ * @param string|int $key
+ * @param string|int|null $field [optional]
+ * @return float UNIX timestamp; the age of the given entry or entry field
+ */
+ private function getAge( $key, $field = null ) {
+ if ( $field !== null ) {
+ $mtime = $this->timestamps[$key][self::FIELDS][$field] ?? $this->epoch;
+ } else {
+ $mtime = $this->timestamps[$key][self::SIMPLE] ?? $this->epoch;
+ }
+
+ return ( $this->getCurrentTime() - $mtime );
+ }
+
+ public function serialize() {
+ return serialize( [
+ 'entries' => $this->cache,
+ 'timestamps' => $this->timestamps
+ ] );
+ }
+
+ public function unserialize( $serialized ) {
+ $data = unserialize( $serialized );
+ $this->cache = $data['entries'] ?? [];
+ $this->timestamps = $data['timestamps'] ?? [];
+ $this->epoch = $this->getCurrentTime();
+ }
+
+ /**
+ * @return float UNIX timestamp
+ * @codeCoverageIgnore
+ */
+ protected function getCurrentTime() {
+ return $this->wallClockOverride ?: microtime( true );
+ }
+
+ /**
+ * @param float|null &$time Mock UNIX timestamp for testing
+ * @codeCoverageIgnore
+ */
+ public function setMockTime( &$time ) {
+ $this->wallClockOverride =& $time;
+ }