From 8b4b0e79c592a895631d8329a6c0072097383165 Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Wed, 3 Jun 2020 10:44:24 +0700 Subject: [PATCH] Improved albums public page cache and more Removed its dependency towards albums' editedAt property. Editing album's metas (name, description, etc) will no longer update its editedAt property. Instead it will now ONLY be updated when adding/removing files to/from it. Just like how it was meant to be, which was to be used to check whether it's necessary to re-generate their downloadable ZIPs. Albums public page cache will still be properly invalidated when adding/removing files to/from it, as well as after editing their metas. Added views/album-notice.njk to be used to render okay-ish notice when an album's public page is still being generated. I was originally thinking of using it for disabled albums as well, but I refrained from it to reduce the possibility of disabled album IDs from being easily scanned (as it just returns 404 now). Removed invalidatedAt property from stats cache. Instead their caches will immediately be nullified as they should (thus frees up memory slightly as well). Stats cache for albums will now only be cleared when truly necessary. As in, adding/removing files to/from albums will no longer clear them. Updated Nunjucks files to properly use h1, h2, h3 tags in actual hierarchical orders. Elements that don't need to use hX tags will now use P instead. Nothing changes visually, only structurally. Fixed some elements in Nunjucks using single quotes instead of double quotes. They'd have worked the same, but consistency. Added h1 title in FAQ page. Make text for no JS warning a bit bigger, and improved the phrasing a little bit. --- controllers/albumsController.js | 8 ++--- controllers/utilsController.js | 22 ++++++------ routes/album.js | 18 ++++------ views/_partial/noscript.njk | 8 +++-- views/album-notice.njk | 29 ++++++++++++++++ views/album.njk | 6 ++-- views/cookiepolicy.njk | 20 +++++------ views/dashboard.njk | 4 +-- views/faq.njk | 61 ++++++++++++++++++--------------- views/home.njk | 4 +-- views/nojs.njk | 4 +-- 11 files changed, 106 insertions(+), 78 deletions(-) create mode 100644 views/album-notice.njk diff --git a/controllers/albumsController.js b/controllers/albumsController.js index a68c167..47ef741 100644 --- a/controllers/albumsController.js +++ b/controllers/albumsController.js @@ -262,6 +262,7 @@ self.disable = async (req, res, next) => { }) .update('enabled', 0) utils.invalidateAlbumsCache([id]) + utils.invalidateStatsCache('albums') const identifier = await db.table('albums') .select('identifier') @@ -325,7 +326,6 @@ self.edit = async (req, res, next) => { const update = { name, - editedAt: Math.floor(Date.now() / 1000), download: Boolean(req.body.download), public: Boolean(req.body.public), description: typeof req.body.description === 'string' @@ -343,6 +343,7 @@ self.edit = async (req, res, next) => { .where(filter) .update(update) utils.invalidateAlbumsCache([id]) + utils.invalidateStatsCache('albums') if (req.body.requestLink) { self.onHold.delete(update.identifier) @@ -459,10 +460,6 @@ self.generateZip = async (req, res, next) => { if ((isNaN(versionString) || versionString <= 0) && album.editedAt) return res.redirect(`${album.identifier}?v=${album.editedAt}`) - // TODO: editedAt column will now be updated whenever - // a user is simply editing the album's name/description. - // Perhaps add a new timestamp column that will only be updated - // when the files in the album are actually modified? if (album.zipGeneratedAt > album.editedAt) try { const filePath = path.join(paths.zips, `${identifier}.zip`) @@ -607,6 +604,7 @@ self.addFiles = async (req, res, next) => { await db.table('albums') .whereIn('id', albumids) .update('editedAt', Math.floor(Date.now() / 1000)) + utils.invalidateAlbumsCache(albumids) return res.json({ success: true, failed }) } catch (error) { diff --git a/controllers/utilsController.js b/controllers/utilsController.js index fb3fe9a..9aa1683 100644 --- a/controllers/utilsController.js +++ b/controllers/utilsController.js @@ -49,20 +49,17 @@ const statsCache = { albums: { cache: null, generating: false, - generatedAt: 0, - invalidatedAt: 0 + generatedAt: 0 }, users: { cache: null, generating: false, - generatedAt: 0, - invalidatedAt: 0 + generatedAt: 0 }, uploads: { cache: null, generating: false, - generatedAt: 0, - invalidatedAt: 0 + generatedAt: 0 } } @@ -460,11 +457,13 @@ self.bulkDeleteFromDb = async (field, values, user) => { if (unlinkeds.length) { // Update albums if necessary, but do not wait - if (albumids.length) + if (albumids.length) { db.table('albums') .whereIn('id', albumids) .update('editedAt', Math.floor(Date.now() / 1000)) .catch(logger.error) + self.invalidateAlbumsCache(albumids) + } // Purge Cloudflare's cache if necessary, but do not wait if (config.cloudflare.purgeCache) @@ -575,12 +574,11 @@ self.invalidateAlbumsCache = albumids => { delete self.albumsCache[albumid] delete self.albumsCache[`${albumid}-nojs`] } - self.invalidateStatsCache('albums') } self.invalidateStatsCache = type => { if (!['albums', 'users', 'uploads'].includes(type)) return - statsCache[type].invalidatedAt = Date.now() + statsCache[type].cache = null } self.stats = async (req, res, next) => { @@ -777,7 +775,7 @@ self.stats = async (req, res, next) => { // Uploads if (!statsCache.uploads.cache && statsCache.uploads.generating) { stats.uploads = false - } else if ((statsCache.uploads.invalidatedAt < statsCache.uploads.generatedAt) || statsCache.uploads.generating) { + } else if (statsCache.uploads.cache) { stats.uploads = statsCache.uploads.cache } else { statsCache.uploads.generating = true @@ -834,7 +832,7 @@ self.stats = async (req, res, next) => { // Users if (!statsCache.users.cache && statsCache.users.generating) { stats.users = false - } else if ((statsCache.users.invalidatedAt < statsCache.users.generatedAt) || statsCache.users.generating) { + } else if (statsCache.users.cache) { stats.users = statsCache.users.cache } else { statsCache.users.generating = true @@ -877,7 +875,7 @@ self.stats = async (req, res, next) => { // Albums if (!statsCache.albums.cache && statsCache.albums.generating) { stats.albums = false - } else if ((statsCache.albums.invalidatedAt < statsCache.albums.generatedAt) || statsCache.albums.generating) { + } else if (statsCache.albums.cache) { stats.albums = statsCache.albums.cache } else { statsCache.albums.generating = true diff --git a/routes/album.js b/routes/album.js index d9dcf8a..aec8e8f 100644 --- a/routes/album.js +++ b/routes/album.js @@ -31,24 +31,20 @@ routes.get('/a/:identifier', async (req, res, next) => { if (!utils.albumsCache[cacheid]) utils.albumsCache[cacheid] = { cache: null, - generating: false, - // Cache will actually be deleted after the album has been updated, - // so storing this timestamp may be redundant, but just in case. - generatedAt: 0 + generating: false } if (!utils.albumsCache[cacheid].cache && utils.albumsCache[cacheid].generating) - return res.json({ - success: false, - description: 'This album is still generating its public page.' + return res.render('album-notice', { + config, + versions: utils.versionStrings, + album, + notice: 'This album\'s public page is still being generated. Please try again later.' }) - else if ((album.editedAt < utils.albumsCache[cacheid].generatedAt) || utils.albumsCache[cacheid].generating) + else if (utils.albumsCache[cacheid].cache) return res.send(utils.albumsCache[cacheid].cache) - // Use current timestamp to make sure cache is invalidated - // when an album is edited during this generation process. utils.albumsCache[cacheid].generating = true - utils.albumsCache[cacheid].generatedAt = Math.floor(Date.now() / 1000) } const files = await db.table('files') diff --git a/views/_partial/noscript.njk b/views/_partial/noscript.njk index 123aeb8..298630d 100644 --- a/views/_partial/noscript.njk +++ b/views/_partial/noscript.njk @@ -1,13 +1,15 @@