From 29b9da64d4f937a10f7c3f39a80988ebeded955f Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sat, 27 Jan 2018 14:55:46 -0800 Subject: [PATCH] properly delete cache when logging out --- routes/_utils/database/databaseCore.js | 39 ++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/routes/_utils/database/databaseCore.js b/routes/_utils/database/databaseCore.js index b3fd22b0..5728254c 100644 --- a/routes/_utils/database/databaseCore.js +++ b/routes/_utils/database/databaseCore.js @@ -15,9 +15,18 @@ import { import QuickLRU from 'quick-lru' -const statusesCache = new QuickLRU({maxSize: 100}) -const accountsCache = new QuickLRU({maxSize: 50}) -const metaCache = new QuickLRU({maxSize: 20}) +const statusesCache = { + maxSize: 100, + caches: {} +} +const accountsCache = { + maxSize: 50, + caches: {} +} +const metaCache = { + maxSize: 20, + caches: {} +} if (process.browser && process.env.NODE_ENV !== 'production') { window.cacheStats = { @@ -39,16 +48,31 @@ if (process.browser && process.env.NODE_ENV !== 'production') { } } +function clearCache(cache, instanceName) { + delete cache.caches[instanceName] +} + +function getOrCreateInstanceCache(cache, instanceName) { + let cached = cache.caches[instanceName] + if (!cached) { + cached = cache.caches[instanceName] = new QuickLRU({maxSize: cache.maxSize}) + } + return cached +} + function setInCache(cache, instanceName, key, value) { - return cache.set(`${instanceName}/${key}`, value) + let instanceCache = getOrCreateInstanceCache(cache, instanceName) + return instanceCache.set(key, value) } function getInCache(cache, instanceName, key) { - return cache.get(`${instanceName}/${key}`) + let instanceCache = getOrCreateInstanceCache(cache, instanceName) + return instanceCache.get(key) } function hasInCache(cache, instanceName, key) { - return cache.has(`${instanceName}/${key}`) + let instanceCache = getOrCreateInstanceCache(cache, instanceName) + return instanceCache.has(key) } // @@ -203,5 +227,8 @@ export async function getAccount(instanceName, accountId) { // export async function clearDatabaseForInstance(instanceName) { + clearCache(statusesCache, instanceName) + clearCache(accountsCache, instanceName) + clearCache(metaCache, instanceName) await deleteDatabase(instanceName) } \ No newline at end of file