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