From ab3efd2829098c8b7f1022500bb32346165ad614 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Thu, 25 Jan 2018 00:01:56 -0800 Subject: [PATCH] add a cache for accounts too --- routes/_components/Timeline.html | 4 +-- routes/_utils/database/databaseCore.js | 34 +++++++++++++++++---- routes/_utils/database/databaseLifecycle.js | 4 +++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/routes/_components/Timeline.html b/routes/_components/Timeline.html index 9613fa4d..7f111b5e 100644 --- a/routes/_components/Timeline.html +++ b/routes/_components/Timeline.html @@ -108,13 +108,13 @@ if (process.env.NODE_ENV !== 'production') { console.log('addStatuses()') } - let instanceName = this.store.get('instanceName') + let instanceName = this.store.get('currentInstance') let timeline = this.get('timeline') - /* no await */ database.insertStatuses(instanceName, timeline, newStatuses) let statusIds = this.get('statusIds') if (!statusIds) { return } + /* no await */ database.insertStatuses(instanceName, timeline, newStatuses) let newStatusIds = newStatuses.map(status => status.id) let merged = mergeStatuses(statusIds, newStatusIds) this.set({ statusIds: merged }) diff --git a/routes/_utils/database/databaseCore.js b/routes/_utils/database/databaseCore.js index 7a07bc29..a1ddb8d4 100644 --- a/routes/_utils/database/databaseCore.js +++ b/routes/_utils/database/databaseCore.js @@ -16,12 +16,20 @@ import { import QuickLRU from 'quick-lru' const statusesCache = new QuickLRU({maxSize: 100}) +const accountsCache = new QuickLRU({maxSize: 50}) if (process.browser && process.env.NODE_ENV !== 'production') { window.cacheStats = { - cache: statusesCache, - cacheHits: 0, - cacheMisses: 0 + statuses: { + cache: statusesCache, + hits: 0, + misses: 0 + }, + accounts: { + cache: accountsCache, + hits: 0, + misses: 0 + } } } @@ -51,6 +59,10 @@ export async function getTimeline(instanceName, timeline, maxId = null, limit = export async function insertStatuses(instanceName, timeline, statuses) { for (let status of statuses) { statusesCache.set(status.id, status) + accountsCache.set(status.account.id, status.account) + if (status.reblog) { + accountsCache.set(status.reblog.account.id, status.reblog.account) + } } const db = await getDatabase(instanceName, timeline) await dbPromise(db, [TIMELINE_STORE, STATUSES_STORE, ACCOUNTS_STORE], 'readwrite', (stores) => { @@ -90,12 +102,22 @@ export async function setInstanceVerifyCredentials(instanceName, verifyCredentia } export async function getAccount(instanceName, accountId) { + if (accountsCache.has(accountId)) { + if (process.browser && process.env.NODE_ENV !== 'production') { + window.cacheStats.accounts.hits++ + } + return accountsCache.get(accountId) + } const db = await getDatabase(instanceName) - return await dbPromise(db, ACCOUNTS_STORE, 'readonly', (store, callback) => { + let result = await dbPromise(db, ACCOUNTS_STORE, 'readonly', (store, callback) => { store.get(accountId).onsuccess = (e) => { callback(e.target.result && e.target.result) } }) + if (process.browser && process.env.NODE_ENV !== 'production') { + window.cacheStats.accounts.misses++ + } + return result } export async function clearDatabaseForInstance(instanceName) { @@ -105,7 +127,7 @@ export async function clearDatabaseForInstance(instanceName) { export async function getStatus(instanceName, statusId) { if (statusesCache.has(statusId)) { if (process.browser && process.env.NODE_ENV !== 'production') { - window.cacheStats.cacheHits++ + window.cacheStats.statuses.hits++ } return statusesCache.get(statusId) } @@ -117,7 +139,7 @@ export async function getStatus(instanceName, statusId) { }) statusesCache.set(statusId, result) if (process.browser && process.env.NODE_ENV !== 'production') { - window.cacheStats.cacheMisses++ + window.cacheStats.statuses.misses++ } return result } \ No newline at end of file diff --git a/routes/_utils/database/databaseLifecycle.js b/routes/_utils/database/databaseLifecycle.js index 9af324a2..81f73afd 100644 --- a/routes/_utils/database/databaseLifecycle.js +++ b/routes/_utils/database/databaseLifecycle.js @@ -9,6 +9,10 @@ import { } from './constants' export function getDatabase(instanceName) { + if (!instanceName && process.env.NODE_ENV !== 'production') { + console.trace() + throw new Error('instanceName is undefined in getDatabase()') + } if (databaseCache[instanceName]) { return Promise.resolve(databaseCache[instanceName]) }