Trying some app-side caching in the SSB app.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3725 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2021-12-31 18:42:37 +00:00
parent 4f1b31bce0
commit 4e359c3f5c
4 changed files with 33 additions and 37 deletions

View File

@ -3,7 +3,14 @@
const k_posts_max = 20;
const k_votes_max = 20;
var g_following_cache = {};
var g_followers_cache = {};
var g_following_deep_cache = {};
async function following(db, id) {
if (g_following_cache[id]) {
return g_following_cache[id];
}
var o = await db.get(id + ":following");
const k_version = 5;
var f = o ? JSON.parse(o) : o;
@ -37,6 +44,7 @@ async function following(db, id) {
if (o != j) {
await db.set(id + ":following", j);
}
g_following_cache[id] = f.users;
return f.users;
}
@ -44,48 +52,33 @@ async function followingDeep(db, seed_ids, depth) {
if (depth <= 0) {
return seed_ids;
}
var key = JSON.stringify([seed_ids, depth]);
if (g_following_deep_cache[key]) {
return g_following_deep_cache[key];
}
var f = await Promise.all(seed_ids.map(x => following(db, x)));
var ids = [].concat(...f);
var x = await followingDeep(db, [...new Set(ids)].sort(), depth - 1);
x = [].concat(...x, ...seed_ids);
g_following_deep_cache[key] = x;
return x;
}
async function followers(db, id) {
var o = await db.get(id + ":followers");
const k_version = 3;
var f = o ? JSON.parse(o) : o;
if (!f || f.version != k_version) {
f = {users: [], rowid: 0, version: k_version};
if (g_followers_cache[id]) {
return g_followers_cache[id];
}
f.users = new Set(f.users);
await ssb.sqlStream(
"SELECT "+
" rowid, "+
" author AS contact, "+
" json_extract(content, '$.following') AS following "+
"FROM messages "+
"WHERE "+
" rowid > $1 AND "+
" json_extract(content, '$.type') = 'contact' AND "+
" json_extract(content, '$.contact') = $2 "+
"UNION SELECT MAX(rowid) as rowid, NULL, NULL FROM messages "+
"ORDER BY rowid",
[f.rowid, id],
function(row) {
if (row.following) {
f.users.add(row.contact);
} else {
f.users.delete(row.contact);
}
f.rowid = row.rowid;
});
f.users = Array.from(f.users);
var j = JSON.stringify(f);
if (o != j) {
await db.set(id + ":followers", j);
var results = [];
var me = await ssb.whoami();
var visible_users = await followingDeep(db, [me], 2);
for (let user of visible_users) {
var followed = await following(db, user);
if (followed.indexOf(id)) {
results.push(user);
}
}
return f.users;
g_followers_cache[id] = results;
return results;
}
async function sendUser(db, id) {
@ -151,7 +144,7 @@ function fnv32a(value)
async function getRecentPostIds(db, id, ids, limit) {
const k_version = 11;
const k_batch_max = 8;
const k_batch_max = 16;
var o = await db.get(id + ':recent_posts');
var recent = [];
var f = o ? JSON.parse(o) : o;
@ -178,7 +171,7 @@ async function getRecentPostIds(db, id, ids, limit) {
" rowid > ? AND "+
" rowid <= ? AND "+
" author IN (" + ids_batch.map(x => '?').join(", ") + ") AND "+
" json_extract(content, '$.type') IN ('post', 'tildefriends-app') "+
" json_extract(content, '$.type') = 'post' "+
"ORDER BY timestamp DESC LIMIT ?",
[].concat([f.rowid, row_id_max], ids_batch, [limit]),
function(row) {
@ -262,6 +255,9 @@ core.register('onConnectionsChanged', async function() {
});
async function refresh() {
g_following_cache = {};
g_followers_cache = {};
g_following_deep_cache = {};
await app.postMessage({clear: true});
var whoami = await ssb.whoami();
var db = await database("ssb");