diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-01-23 21:13:41 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-07-23 21:19:21 +0200 |
commit | b0428aa9b02b058b72c40b6e8dc2298d55bf692f (patch) | |
tree | c57176b24d76836a73608fb273b1094fcb8de785 /application/render/PageCacheManager.php | |
parent | 485b168a9677d160b0c0426e4f282b9bd0c632c1 (diff) | |
download | Shaarli-b0428aa9b02b058b72c40b6e8dc2298d55bf692f.tar.gz Shaarli-b0428aa9b02b058b72c40b6e8dc2298d55bf692f.tar.zst Shaarli-b0428aa9b02b058b72c40b6e8dc2298d55bf692f.zip |
Migrate cache purge function to a proper class
And update dependencies and tests.
Note that SESSION['tags'] has been removed a log ago
Diffstat (limited to 'application/render/PageCacheManager.php')
-rw-r--r-- | application/render/PageCacheManager.php | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/application/render/PageCacheManager.php b/application/render/PageCacheManager.php new file mode 100644 index 00000000..bd91fe0d --- /dev/null +++ b/application/render/PageCacheManager.php | |||
@@ -0,0 +1,45 @@ | |||
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 | } | ||