aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-01-23 21:13:41 +0100
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commitb0428aa9b02b058b72c40b6e8dc2298d55bf692f (patch)
treec57176b24d76836a73608fb273b1094fcb8de785 /application
parent485b168a9677d160b0c0426e4f282b9bd0c632c1 (diff)
downloadShaarli-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')
-rw-r--r--application/bookmark/BookmarkFileService.php7
-rw-r--r--application/bookmark/BookmarkIO.php2
-rw-r--r--application/feed/Cache.php38
-rw-r--r--application/legacy/LegacyLinkDB.php4
-rw-r--r--application/render/PageCacheManager.php45
5 files changed, 54 insertions, 42 deletions
diff --git a/application/bookmark/BookmarkFileService.php b/application/bookmark/BookmarkFileService.php
index 9c59e139..fef998fd 100644
--- a/application/bookmark/BookmarkFileService.php
+++ b/application/bookmark/BookmarkFileService.php
@@ -12,6 +12,7 @@ use Shaarli\Formatter\BookmarkMarkdownFormatter;
12use Shaarli\History; 12use Shaarli\History;
13use Shaarli\Legacy\LegacyLinkDB; 13use Shaarli\Legacy\LegacyLinkDB;
14use Shaarli\Legacy\LegacyUpdater; 14use Shaarli\Legacy\LegacyUpdater;
15use Shaarli\Render\PageCacheManager;
15use Shaarli\Updater\UpdaterUtils; 16use Shaarli\Updater\UpdaterUtils;
16 17
17/** 18/**
@@ -39,6 +40,9 @@ class BookmarkFileService implements BookmarkServiceInterface
39 /** @var History instance */ 40 /** @var History instance */
40 protected $history; 41 protected $history;
41 42
43 /** @var PageCacheManager instance */
44 protected $pageCacheManager;
45
42 /** @var bool true for logged in users. Default value to retrieve private bookmarks. */ 46 /** @var bool true for logged in users. Default value to retrieve private bookmarks. */
43 protected $isLoggedIn; 47 protected $isLoggedIn;
44 48
@@ -49,6 +53,7 @@ class BookmarkFileService implements BookmarkServiceInterface
49 { 53 {
50 $this->conf = $conf; 54 $this->conf = $conf;
51 $this->history = $history; 55 $this->history = $history;
56 $this->pageCacheManager = new PageCacheManager($this->conf->get('resource.page_cache'));
52 $this->bookmarksIO = new BookmarkIO($this->conf); 57 $this->bookmarksIO = new BookmarkIO($this->conf);
53 $this->isLoggedIn = $isLoggedIn; 58 $this->isLoggedIn = $isLoggedIn;
54 59
@@ -275,7 +280,7 @@ class BookmarkFileService implements BookmarkServiceInterface
275 } 280 }
276 $this->bookmarks->reorder(); 281 $this->bookmarks->reorder();
277 $this->bookmarksIO->write($this->bookmarks); 282 $this->bookmarksIO->write($this->bookmarks);
278 invalidateCaches($this->conf->get('resource.page_cache')); 283 $this->pageCacheManager->invalidateCaches();
279 } 284 }
280 285
281 /** 286 /**
diff --git a/application/bookmark/BookmarkIO.php b/application/bookmark/BookmarkIO.php
index ae9ffcb4..1026e2f9 100644
--- a/application/bookmark/BookmarkIO.php
+++ b/application/bookmark/BookmarkIO.php
@@ -102,7 +102,5 @@ class BookmarkIO
102 $this->datastore, 102 $this->datastore,
103 self::$phpPrefix.base64_encode(gzdeflate(serialize($links))).self::$phpSuffix 103 self::$phpPrefix.base64_encode(gzdeflate(serialize($links))).self::$phpSuffix
104 ); 104 );
105
106 invalidateCaches($this->conf->get('resource.page_cache'));
107 } 105 }
108} 106}
diff --git a/application/feed/Cache.php b/application/feed/Cache.php
deleted file mode 100644
index e5d43e61..00000000
--- a/application/feed/Cache.php
+++ /dev/null
@@ -1,38 +0,0 @@
1<?php
2/**
3 * Cache utilities
4 */
5
6/**
7 * Purges all cached pages
8 *
9 * @param string $pageCacheDir page cache directory
10 *
11 * @return mixed an error string if the directory is missing
12 */
13function purgeCachedPages($pageCacheDir)
14{
15 if (! is_dir($pageCacheDir)) {
16 $error = sprintf(t('Cannot purge %s: no directory'), $pageCacheDir);
17 error_log($error);
18 return $error;
19 }
20
21 array_map('unlink', glob($pageCacheDir.'/*.cache'));
22}
23
24/**
25 * Invalidates caches when the database is changed or the user logs out.
26 *
27 * @param string $pageCacheDir page cache directory
28 */
29function invalidateCaches($pageCacheDir)
30{
31 // Purge cache attached to session.
32 if (isset($_SESSION['tags'])) {
33 unset($_SESSION['tags']);
34 }
35
36 // Purge page cache shared by sessions.
37 purgeCachedPages($pageCacheDir);
38}
diff --git a/application/legacy/LegacyLinkDB.php b/application/legacy/LegacyLinkDB.php
index 7ccf5e54..947005ad 100644
--- a/application/legacy/LegacyLinkDB.php
+++ b/application/legacy/LegacyLinkDB.php
@@ -9,6 +9,7 @@ use Iterator;
9use Shaarli\Bookmark\Exception\BookmarkNotFoundException; 9use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
10use Shaarli\Exceptions\IOException; 10use Shaarli\Exceptions\IOException;
11use Shaarli\FileUtils; 11use Shaarli\FileUtils;
12use Shaarli\Render\PageCacheManager;
12 13
13/** 14/**
14 * Data storage for bookmarks. 15 * Data storage for bookmarks.
@@ -352,7 +353,8 @@ You use the community supported version of the original Shaarli project, by Seba
352 353
353 $this->write(); 354 $this->write();
354 355
355 invalidateCaches($pageCacheDir); 356 $pageCacheManager = new PageCacheManager($pageCacheDir);
357 $pageCacheManager->invalidateCaches();
356 } 358 }
357 359
358 /** 360 /**
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
3namespace Shaarli\Render;
4
5/**
6 * Cache utilities
7 */
8class 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}