diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-12-17 15:43:33 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-12-17 15:48:03 +0100 |
commit | f00600a283617286c813dc902fe3a2d66938b5fc (patch) | |
tree | 9dc783ade453f26b315c681607caa00fe1eade14 /application/feed | |
parent | ab4c170672c0679c5b8ebc6065e3ca2b13165f24 (diff) | |
download | Shaarli-f00600a283617286c813dc902fe3a2d66938b5fc.tar.gz Shaarli-f00600a283617286c813dc902fe3a2d66938b5fc.tar.zst Shaarli-f00600a283617286c813dc902fe3a2d66938b5fc.zip |
Daily RSS Cache: invalidate cache base on the date
Currently the cache is only invalidated when the datastore changes, while it should rely on selected period of time.
Fixes #1659
Diffstat (limited to 'application/feed')
-rw-r--r-- | application/feed/CachedPage.php | 45 |
1 files changed, 32 insertions, 13 deletions
diff --git a/application/feed/CachedPage.php b/application/feed/CachedPage.php index d809bdd9..c23c200f 100644 --- a/application/feed/CachedPage.php +++ b/application/feed/CachedPage.php | |||
@@ -1,34 +1,43 @@ | |||
1 | <?php | 1 | <?php |
2 | 2 | ||
3 | declare(strict_types=1); | ||
4 | |||
3 | namespace Shaarli\Feed; | 5 | namespace Shaarli\Feed; |
4 | 6 | ||
7 | use DatePeriod; | ||
8 | |||
5 | /** | 9 | /** |
6 | * Simple cache system, mainly for the RSS/ATOM feeds | 10 | * Simple cache system, mainly for the RSS/ATOM feeds |
7 | */ | 11 | */ |
8 | class CachedPage | 12 | class CachedPage |
9 | { | 13 | { |
10 | // Directory containing page caches | 14 | /** Directory containing page caches */ |
11 | private $cacheDir; | 15 | protected $cacheDir; |
16 | |||
17 | /** Should this URL be cached (boolean)? */ | ||
18 | protected $shouldBeCached; | ||
12 | 19 | ||
13 | // Should this URL be cached (boolean)? | 20 | /** Name of the cache file for this URL */ |
14 | private $shouldBeCached; | 21 | protected $filename; |
15 | 22 | ||
16 | // Name of the cache file for this URL | 23 | /** @var DatePeriod|null Optionally specify a period of time for cache validity */ |
17 | private $filename; | 24 | protected $validityPeriod; |
18 | 25 | ||
19 | /** | 26 | /** |
20 | * Creates a new CachedPage | 27 | * Creates a new CachedPage |
21 | * | 28 | * |
22 | * @param string $cacheDir page cache directory | 29 | * @param string $cacheDir page cache directory |
23 | * @param string $url page URL | 30 | * @param string $url page URL |
24 | * @param bool $shouldBeCached whether this page needs to be cached | 31 | * @param bool $shouldBeCached whether this page needs to be cached |
32 | * @param ?DatePeriod $validityPeriod Optionally specify a time limit on requested cache | ||
25 | */ | 33 | */ |
26 | public function __construct($cacheDir, $url, $shouldBeCached) | 34 | public function __construct($cacheDir, $url, $shouldBeCached, ?DatePeriod $validityPeriod) |
27 | { | 35 | { |
28 | // TODO: check write access to the cache directory | 36 | // TODO: check write access to the cache directory |
29 | $this->cacheDir = $cacheDir; | 37 | $this->cacheDir = $cacheDir; |
30 | $this->filename = $this->cacheDir . '/' . sha1($url) . '.cache'; | 38 | $this->filename = $this->cacheDir . '/' . sha1($url) . '.cache'; |
31 | $this->shouldBeCached = $shouldBeCached; | 39 | $this->shouldBeCached = $shouldBeCached; |
40 | $this->validityPeriod = $validityPeriod; | ||
32 | } | 41 | } |
33 | 42 | ||
34 | /** | 43 | /** |
@@ -41,10 +50,20 @@ class CachedPage | |||
41 | if (!$this->shouldBeCached) { | 50 | if (!$this->shouldBeCached) { |
42 | return null; | 51 | return null; |
43 | } | 52 | } |
44 | if (is_file($this->filename)) { | 53 | if (!is_file($this->filename)) { |
45 | return file_get_contents($this->filename); | 54 | return null; |
55 | } | ||
56 | if ($this->validityPeriod !== null) { | ||
57 | $cacheDate = \DateTime::createFromFormat('U', (string) filemtime($this->filename)); | ||
58 | if ( | ||
59 | $cacheDate < $this->validityPeriod->getStartDate() | ||
60 | || $cacheDate > $this->validityPeriod->getEndDate() | ||
61 | ) { | ||
62 | return null; | ||
63 | } | ||
46 | } | 64 | } |
47 | return null; | 65 | |
66 | return file_get_contents($this->filename); | ||
48 | } | 67 | } |
49 | 68 | ||
50 | /** | 69 | /** |