ssb: Now add back the about cache, and load times are getting good.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 31m43s

This commit is contained in:
Cory McWilliams 2025-05-09 13:25:05 -04:00
parent 58c3e6c2ab
commit 8e3bc9d700
2 changed files with 76 additions and 32 deletions

View File

@ -1,5 +1,5 @@
{
"type": "tildefriends-app",
"emoji": "🦀",
"previous": "&NajVQD/6DKISEDlnyAVYh3NPO/vxMN7ksNZy3FwI378=.sha256"
"previous": "&VQsPosjAfr008a6UMb8vKmKxE/WS4DRKEspJL8Lewjw=.sha256"
}

View File

@ -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]