From b0428aa9b02b058b72c40b6e8dc2298d55bf692f Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 23 Jan 2020 21:13:41 +0100 Subject: Migrate cache purge function to a proper class And update dependencies and tests. Note that SESSION['tags'] has been removed a log ago --- application/bookmark/BookmarkFileService.php | 7 ++- application/bookmark/BookmarkIO.php | 2 - application/feed/Cache.php | 38 ------------ application/legacy/LegacyLinkDB.php | 4 +- application/render/PageCacheManager.php | 45 ++++++++++++++ index.php | 8 ++- tests/bootstrap.php | 1 - tests/feed/CacheTest.php | 92 ---------------------------- tests/legacy/LegacyLinkDBTest.php | 1 - tests/render/PageCacheManagerTest.php | 86 ++++++++++++++++++++++++++ 10 files changed, 146 insertions(+), 138 deletions(-) delete mode 100644 application/feed/Cache.php create mode 100644 application/render/PageCacheManager.php delete mode 100644 tests/feed/CacheTest.php create mode 100644 tests/render/PageCacheManagerTest.php 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; use Shaarli\History; use Shaarli\Legacy\LegacyLinkDB; use Shaarli\Legacy\LegacyUpdater; +use Shaarli\Render\PageCacheManager; use Shaarli\Updater\UpdaterUtils; /** @@ -39,6 +40,9 @@ class BookmarkFileService implements BookmarkServiceInterface /** @var History instance */ protected $history; + /** @var PageCacheManager instance */ + protected $pageCacheManager; + /** @var bool true for logged in users. Default value to retrieve private bookmarks. */ protected $isLoggedIn; @@ -49,6 +53,7 @@ class BookmarkFileService implements BookmarkServiceInterface { $this->conf = $conf; $this->history = $history; + $this->pageCacheManager = new PageCacheManager($this->conf->get('resource.page_cache')); $this->bookmarksIO = new BookmarkIO($this->conf); $this->isLoggedIn = $isLoggedIn; @@ -275,7 +280,7 @@ class BookmarkFileService implements BookmarkServiceInterface } $this->bookmarks->reorder(); $this->bookmarksIO->write($this->bookmarks); - invalidateCaches($this->conf->get('resource.page_cache')); + $this->pageCacheManager->invalidateCaches(); } /** 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 $this->datastore, self::$phpPrefix.base64_encode(gzdeflate(serialize($links))).self::$phpSuffix ); - - invalidateCaches($this->conf->get('resource.page_cache')); } } 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 @@ -write(); - invalidateCaches($pageCacheDir); + $pageCacheManager = new PageCacheManager($pageCacheDir); + $pageCacheManager->invalidateCaches(); } /** 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 @@ +pageCacheDir = $pageCacheDir; + } + + /** + * Purges all cached pages + * + * @return string|null an error string if the directory is missing + */ + public function purgeCachedPages(): ?string + { + if (!is_dir($this->pageCacheDir)) { + $error = sprintf(t('Cannot purge %s: no directory'), $this->pageCacheDir); + error_log($error); + + return $error; + } + + array_map('unlink', glob($this->pageCacheDir . '/*.cache')); + + return null; + } + + /** + * Invalidates caches when the database is changed or the user logs out. + */ + public function invalidateCaches(): void + { + // Purge page cache shared by sessions. + $this->purgeCachedPages(); + } +} diff --git a/index.php b/index.php index c639a3bc..73d9e022 100644 --- a/index.php +++ b/index.php @@ -53,7 +53,6 @@ require_once __DIR__ . '/vendor/autoload.php'; // Shaarli library require_once 'application/bookmark/LinkUtils.php'; require_once 'application/config/ConfigPlugin.php'; -require_once 'application/feed/Cache.php'; require_once 'application/http/HttpUtils.php'; require_once 'application/http/UrlUtils.php'; require_once 'application/updater/UpdaterUtils.php'; @@ -78,6 +77,7 @@ use Shaarli\Languages; use Shaarli\Netscape\NetscapeBookmarkUtils; use Shaarli\Plugin\PluginManager; use Shaarli\Render\PageBuilder; +use Shaarli\Render\PageCacheManager; use Shaarli\Render\ThemeUtils; use Shaarli\Router; use Shaarli\Security\LoginManager; @@ -530,6 +530,7 @@ function showLinkList($PAGE, $linkDb, $conf, $pluginManager, $loginManager) */ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionManager, $loginManager) { + $pageCacheManager = new PageCacheManager($conf->get('resource.page_cache')); $updater = new Updater( UpdaterUtils::read_updates_file($conf->get('resource.updates')), $bookmarkService, @@ -543,6 +544,8 @@ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionM $conf->get('resource.updates'), $updater->getDoneUpdates() ); + + $pageCacheManager->invalidateCaches(); } } catch (Exception $e) { die($e->getMessage()); @@ -1029,7 +1032,7 @@ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionM try { $conf->write($loginManager->isLoggedIn()); $history->updateSettings(); - invalidateCaches($conf->get('resource.page_cache')); + $pageCacheManager->invalidateCaches(); } catch (Exception $e) { error_log( 'ERROR while writing config file after configuration update.' . PHP_EOL . @@ -1914,6 +1917,7 @@ $app->group('/api/v1', function () { $app->group('', function () { $this->get('/login', '\Shaarli\Front\Controller\LoginController:index')->setName('login'); + $this->get('/logout', '\Shaarli\Front\Controller\LogoutController:index')->setName('logout'); $this->get('/picture-wall', '\Shaarli\Front\Controller\PictureWallController:index')->setName('picwall'); })->add('\Shaarli\Front\ShaarliMiddleware'); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 0afbcba6..c80bcb33 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -18,7 +18,6 @@ require_once 'application/bookmark/LinkUtils.php'; require_once 'application/Utils.php'; require_once 'application/http/UrlUtils.php'; require_once 'application/http/HttpUtils.php'; -require_once 'application/feed/Cache.php'; require_once 'tests/utils/ReferenceLinkDB.php'; require_once 'tests/utils/ReferenceHistory.php'; require_once 'tests/utils/FakeBookmarkService.php'; diff --git a/tests/feed/CacheTest.php b/tests/feed/CacheTest.php deleted file mode 100644 index c0a9f26f..00000000 --- a/tests/feed/CacheTest.php +++ /dev/null @@ -1,92 +0,0 @@ -assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache'); - } - - $this->assertFileExists(self::$testCacheDir . '/intru.der'); - } - - /** - * Purge cached pages - missing directory - */ - public function testPurgeCachedPagesMissingDir() - { - $oldlog = ini_get('error_log'); - ini_set('error_log', '/dev/null'); - $this->assertEquals( - 'Cannot purge sandbox/dummycache_missing: no directory', - purgeCachedPages(self::$testCacheDir . '_missing') - ); - ini_set('error_log', $oldlog); - } - - /** - * Purge cached pages and session cache - */ - public function testInvalidateCaches() - { - $this->assertArrayNotHasKey('tags', $_SESSION); - $_SESSION['tags'] = array('goodbye', 'cruel', 'world'); - - invalidateCaches(self::$testCacheDir); - foreach (self::$pages as $page) { - $this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache'); - } - - $this->assertArrayNotHasKey('tags', $_SESSION); - } -} diff --git a/tests/legacy/LegacyLinkDBTest.php b/tests/legacy/LegacyLinkDBTest.php index 17b2b0e6..0884ad03 100644 --- a/tests/legacy/LegacyLinkDBTest.php +++ b/tests/legacy/LegacyLinkDBTest.php @@ -11,7 +11,6 @@ use ReflectionClass; use Shaarli; use Shaarli\Bookmark\Bookmark; -require_once 'application/feed/Cache.php'; require_once 'application/Utils.php'; require_once 'tests/utils/ReferenceLinkDB.php'; diff --git a/tests/render/PageCacheManagerTest.php b/tests/render/PageCacheManagerTest.php new file mode 100644 index 00000000..991515d0 --- /dev/null +++ b/tests/render/PageCacheManagerTest.php @@ -0,0 +1,86 @@ +cacheManager = new PageCacheManager(static::$testCacheDir); + + if (!is_dir(self::$testCacheDir)) { + mkdir(self::$testCacheDir); + } else { + array_map('unlink', glob(self::$testCacheDir . '/*')); + } + + foreach (self::$pages as $page) { + file_put_contents(self::$testCacheDir . '/' . $page . '.cache', $page); + } + file_put_contents(self::$testCacheDir . '/intru.der', 'ShouldNotBeThere'); + } + + /** + * Remove dummycache folder after each tests. + */ + public function tearDown() + { + array_map('unlink', glob(self::$testCacheDir . '/*')); + rmdir(self::$testCacheDir); + } + + /** + * Purge cached pages + */ + public function testPurgeCachedPages() + { + $this->cacheManager->purgeCachedPages(); + foreach (self::$pages as $page) { + $this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache'); + } + + $this->assertFileExists(self::$testCacheDir . '/intru.der'); + } + + /** + * Purge cached pages - missing directory + */ + public function testPurgeCachedPagesMissingDir() + { + $this->cacheManager = new PageCacheManager(self::$testCacheDir . '_missing'); + + $oldlog = ini_get('error_log'); + ini_set('error_log', '/dev/null'); + $this->assertEquals( + 'Cannot purge sandbox/dummycache_missing: no directory', + $this->cacheManager->purgeCachedPages() + ); + ini_set('error_log', $oldlog); + } +} -- cgit v1.2.3