MyShaarli/application/feed/CachedPage.php

82 lines
2.2 KiB
PHP
Raw Permalink Normal View History

<?php
2023-05-24 11:35:15 +02:00
declare(strict_types=1);
namespace Shaarli\Feed;
2023-05-24 11:35:15 +02:00
use DatePeriod;
/**
* Simple cache system, mainly for the RSS/ATOM feeds
*/
class CachedPage
{
2023-05-24 11:35:15 +02:00
/** Directory containing page caches */
protected $cacheDir;
/** Should this URL be cached (boolean)? */
protected $shouldBeCached;
2023-05-24 11:35:15 +02:00
/** Name of the cache file for this URL */
protected $filename;
2023-05-24 11:35:15 +02:00
/** @var DatePeriod|null Optionally specify a period of time for cache validity */
protected $validityPeriod;
/**
* Creates a new CachedPage
*
2023-05-24 11:35:15 +02:00
* @param string $cacheDir page cache directory
* @param string $url page URL
* @param bool $shouldBeCached whether this page needs to be cached
* @param ?DatePeriod $validityPeriod Optionally specify a time limit on requested cache
*/
2023-05-24 11:35:15 +02:00
public function __construct($cacheDir, $url, $shouldBeCached, ?DatePeriod $validityPeriod)
{
// TODO: check write access to the cache directory
$this->cacheDir = $cacheDir;
$this->filename = $this->cacheDir . '/' . sha1($url) . '.cache';
$this->shouldBeCached = $shouldBeCached;
2023-05-24 11:35:15 +02:00
$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;
}
2023-05-24 11:35:15 +02:00
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;
}
}
2023-05-24 11:35:15 +02:00
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);
}
}