/** * Read a key from the cache * * @param string $key Identifier for the data * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it */ public function read($key) { if (!$this->_init || $this->_setKey($key) === false) { return false; } if ($this->settings['lock']) { $this->_File->flock(LOCK_SH); } $this->_File->rewind(); $time = time(); $cachetime = intval($this->_File->current()); if ($cachetime !== false && ($cachetime < $time || $time + $this->settings['duration'] < $cachetime)) { if ($this->settings['lock']) { $this->_File->flock(LOCK_UN); } return false; } $data = ''; $this->_File->next(); while ($this->_File->valid()) { $data .= $this->_File->current(); $this->_File->next(); } if ($this->settings['lock']) { $this->_File->flock(LOCK_UN); } $data = trim($data); if ($data !== '' && !empty($this->settings['serialize'])) { if ($this->settings['isWindows']) { $data = str_replace('\\\\\\\\', '\\', $data); } $data = unserialize((string) $data); } return $data; }
/** * Delete all values from the cache * * @param boolean $check Optional - only delete expired cache items * @return boolean True if the cache was successfully cleared, false otherwise */ public function clear($check) { if (!$this->_init) { return false; } $dir = dir($this->settings['path']); if ($check) { $now = time(); $threshold = $now - $this->settings['duration']; } $prefixLength = strlen($this->settings['prefix']); while (($entry = $dir->read()) !== false) { if (substr($entry, 0, $prefixLength) !== $this->settings['prefix']) { continue; } if ($this->_setKey($entry) === false) { continue; } if ($check) { $mtime = $this->_File->getMTime(); if ($mtime > $threshold) { continue; } $expires = (int)$this->_File->current(); if ($expires > $now) { continue; } } $path = $this->_File->getRealPath(); $this->_File = null; if (file_exists($path)) { unlink($path); } } $dir->close(); return true; }
/** * @covers spriebsch\PHPca\File::seekTokenId * @covers spriebsch\PHPca\File::add */ public function testSeekTokenId() { $file = new File('filename', 'sourcecode'); $file->add(new Token(T_OPEN_TAG, '<?php')); $file->add(new Token(T_FUNCTION, 'function')); $file->add(new Token(T_CLASS, 'class')); $file->add(new Token(T_CLOSE_TAG, '?>')); $file->rewind(); $file->seekTokenId(T_CLASS); $this->assertEquals('T_CLASS', $file->current()->getName()); }