]>
Commit | Line | Data |
---|---|---|
b0428aa9 A |
1 | <?php |
2 | ||
3 | namespace Shaarli\Render; | |
4 | ||
c4d5be53 A |
5 | use Shaarli\Feed\CachedPage; |
6 | ||
b0428aa9 A |
7 | /** |
8 | * Cache utilities | |
9 | */ | |
10 | class PageCacheManager | |
11 | { | |
12 | /** @var string Cache directory */ | |
13 | protected $pageCacheDir; | |
14 | ||
c4d5be53 A |
15 | /** @var bool */ |
16 | protected $isLoggedIn; | |
17 | ||
18 | public function __construct(string $pageCacheDir, bool $isLoggedIn) | |
b0428aa9 A |
19 | { |
20 | $this->pageCacheDir = $pageCacheDir; | |
c4d5be53 | 21 | $this->isLoggedIn = $isLoggedIn; |
b0428aa9 A |
22 | } |
23 | ||
24 | /** | |
25 | * Purges all cached pages | |
26 | * | |
27 | * @return string|null an error string if the directory is missing | |
28 | */ | |
29 | public function purgeCachedPages(): ?string | |
30 | { | |
31 | if (!is_dir($this->pageCacheDir)) { | |
32 | $error = sprintf(t('Cannot purge %s: no directory'), $this->pageCacheDir); | |
33 | error_log($error); | |
34 | ||
35 | return $error; | |
36 | } | |
37 | ||
38 | array_map('unlink', glob($this->pageCacheDir . '/*.cache')); | |
39 | ||
40 | return null; | |
41 | } | |
42 | ||
43 | /** | |
44 | * Invalidates caches when the database is changed or the user logs out. | |
45 | */ | |
46 | public function invalidateCaches(): void | |
47 | { | |
48 | // Purge page cache shared by sessions. | |
49 | $this->purgeCachedPages(); | |
50 | } | |
c4d5be53 A |
51 | |
52 | public function getCachePage(string $pageUrl): CachedPage | |
53 | { | |
54 | return new CachedPage( | |
55 | $this->pageCacheDir, | |
56 | $pageUrl, | |
57 | false === $this->isLoggedIn | |
58 | ); | |
59 | } | |
b0428aa9 | 60 | } |