From 8e3bc9d700b982c733f6bf85b6ea55eb3e5590f0 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Fri, 9 May 2025 13:25:05 -0400 Subject: [PATCH] ssb: Now add back the about cache, and load times are getting good. --- apps/ssb.json | 2 +- apps/ssb/tf-app.js | 106 ++++++++++++++++++++++++++++++++------------- 2 files changed, 76 insertions(+), 32 deletions(-) diff --git a/apps/ssb.json b/apps/ssb.json index ae80dfed..f873b906 100644 --- a/apps/ssb.json +++ b/apps/ssb.json @@ -1,5 +1,5 @@ { "type": "tildefriends-app", "emoji": "🦀", - "previous": "&NajVQD/6DKISEDlnyAVYh3NPO/vxMN7ksNZy3FwI378=.sha256" + "previous": "&VQsPosjAfr008a6UMb8vKmKxE/WS4DRKEspJL8Lewjw=.sha256" } diff --git a/apps/ssb/tf-app.js b/apps/ssb/tf-app.js index 78f34c46..a424aadf 100644 --- a/apps/ssb/tf-app.js +++ b/apps/ssb/tf-app.js @@ -152,39 +152,83 @@ class TfElement extends LitElement { async fetch_about(following, users) { let ids = Object.keys(following).sort(); - console.log('loading about for', ids.length, 'accounts'); - try { - let rows = await tfrpc.rpc.query( - ` - SELECT all_abouts.author, json(json_group_object(all_abouts.key, all_abouts.value)) AS about - FROM ( - SELECT - messages.author, - fields.key, - RANK() OVER (PARTITION BY messages.author, fields.key ORDER BY messages.sequence DESC) AS rank, - fields.value - FROM messages JOIN json_each(messages.content) AS fields - WHERE - messages.content ->> '$.type' = 'about' AND - messages.content ->> '$.about' = messages.author AND - NOT fields.key IN ('about', 'type')) all_abouts - JOIN json_each(?) AS following ON all_abouts.author = following.value - WHERE rank = 1 - GROUP BY all_abouts.author - `, - [JSON.stringify(ids)] - ); - users = users || {}; - for (let row of rows) { - users[row.author] = Object.assign( - users[row.author] || {}, - JSON.parse(row.about) + const k_cache_version = 2; + let cache = await tfrpc.rpc.databaseGet('about'); + let original_cache = cache; + cache = cache ? JSON.parse(cache) : {}; + if (cache.version !== k_cache_version) { + cache = { + version: k_cache_version, + about: {}, + }; + } + + for (let id of Object.keys(cache.about)) { + if (ids.indexOf(id) == -1) { + delete cache.about[id]; + } else { + users[id] = Object.assign( + users[id] || {}, + cache.about[id] ); } - console.log('updated users'); - } catch (e) { - console.log(e); } + + let ids_out_of_date = ids.filter(x => cache.about[x]?.seq === undefined || (users[x]?.seq && users[x]?.seq > cache.about[x].seq)); + + console.log('loading about for', ids.length, 'accounts', ids_out_of_date.length, 'out of date'); + if (ids_out_of_date.length) { + try { + let rows = await tfrpc.rpc.query( + ` + SELECT all_abouts.author, json(json_group_object(all_abouts.key, all_abouts.value)) AS about + FROM ( + SELECT + messages.author, + fields.key, + RANK() OVER (PARTITION BY messages.author, fields.key ORDER BY messages.sequence DESC) AS rank, + fields.value + FROM messages JOIN json_each(messages.content) AS fields + WHERE + messages.content ->> '$.type' = 'about' AND + messages.content ->> '$.about' = messages.author AND + NOT fields.key IN ('about', 'type')) all_abouts + JOIN json_each(?) AS following ON all_abouts.author = following.value + WHERE rank = 1 + GROUP BY all_abouts.author + `, + [JSON.stringify(ids_out_of_date)] + ); + users = users || {}; + for (let row of rows) { + users[row.author] = Object.assign( + users[row.author] || {}, + JSON.parse(row.about) + ); + cache.about[row.author] = Object.assign( + {seq: users[row.author].seq}, + JSON.parse(row.about) + ); + } + } catch (e) { + console.log(e); + } + } + + for (let id of ids_out_of_date) { + if (!cache.about[id]?.seq) { + cache.about[id] = {seq: users[id]?.seq ?? 0}; + } + }; + + let new_cache = JSON.stringify(cache); + if (new_cache != original_cache) { + let start_time = new Date(); + tfrpc.rpc.databaseSet('about', new_cache).then(function() { + console.log('saving about took', (new Date() - start_time) / 1000); + }); + } + return Object.assign({}, users); } @@ -365,7 +409,7 @@ class TfElement extends LitElement { for (let row of info) { users[row.author] = Object.assign( { - seq: row.max_seq, + seq: row.max_sequence, ts: row.max_ts, }, users[row.author]