cacheDir = $cacheDir; $this->filename = $this->cacheDir . '/' . sha1($url) . '.cache'; $this->shouldBeCached = $shouldBeCached; $this->validityPeriod = $validityPeriod; } /** * Returns the cached version of a page, if it exists and should be cached * * @return string a cached version of the page if it exists, null otherwise */ public function cachedVersion() { if (!$this->shouldBeCached) { return null; } if (!is_file($this->filename)) { return null; } if ($this->validityPeriod !== null) { $cacheDate = \DateTime::createFromFormat('U', (string) filemtime($this->filename)); if ( $cacheDate < $this->validityPeriod->getStartDate() || $cacheDate > $this->validityPeriod->getEndDate() ) { return null; } } return file_get_contents($this->filename); } /** * Puts a page in the cache * * @param string $pageContent XML content to cache */ public function cache($pageContent) { if (!$this->shouldBeCached) { return; } file_put_contents($this->filename, $pageContent); } }