]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/feed/CachedPage.php
3 declare(strict_types
=1);
5 namespace Shaarli\Feed
;
10 * Simple cache system, mainly for the RSS/ATOM feeds
14 /** Directory containing page caches */
17 /** Should this URL be cached (boolean)? */
18 protected $shouldBeCached;
20 /** Name of the cache file for this URL */
23 /** @var DatePeriod|null Optionally specify a period of time for cache validity */
24 protected $validityPeriod;
27 * Creates a new CachedPage
29 * @param string $cacheDir page cache directory
30 * @param string $url page URL
31 * @param bool $shouldBeCached whether this page needs to be cached
32 * @param ?DatePeriod $validityPeriod Optionally specify a time limit on requested cache
34 public function __construct($cacheDir, $url, $shouldBeCached, ?DatePeriod
$validityPeriod)
36 // TODO: check write access to the cache directory
37 $this->cacheDir
= $cacheDir;
38 $this->filename
= $this->cacheDir
. '/' . sha1($url) . '.cache';
39 $this->shouldBeCached
= $shouldBeCached;
40 $this->validityPeriod
= $validityPeriod;
44 * Returns the cached version of a page, if it exists and should be cached
46 * @return string a cached version of the page if it exists, null otherwise
48 public function cachedVersion()
50 if (!$this->shouldBeCached
) {
53 if (!is_file($this->filename
)) {
56 if ($this->validityPeriod
!== null) {
57 $cacheDate = \DateTime
::createFromFormat('U', (string) filemtime($this->filename
));
59 $cacheDate < $this->validityPeriod
->getStartDate()
60 || $cacheDate > $this->validityPeriod
->getEndDate()
66 return file_get_contents($this->filename
);
70 * Puts a page in the cache
72 * @param string $pageContent XML content to cache
74 public function cache($pageContent)
76 if (!$this->shouldBeCached
) {
79 file_put_contents($this->filename
, $pageContent);