From 4df932723e474c923311c64e6cb11c50add658b7 Mon Sep 17 00:00:00 2001 From: martbost Date: Tue, 20 Oct 2026 05:21:42 -0500 Subject: [PATCH] Speed: member reads cached 15 s (cleared on every new chain event) so line, stats and positions stop repeating RPC round trips Co-Authored-By: Claude Opus 5.5 --- chain.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/chain.js b/chain.js index 6138afa..5318a98 100644 --- a/chain.js +++ b/chain.js @@ -149,7 +149,16 @@ async function memberIdByAccount(addr) { } async function memberCount() { return toNum(word(await call(SEL.memberCount), 0)); } async function productCount() { return toNum(word(await call(SEL.productCount), 0)); } +// member reads are cached 15 s (2026-10-20): dashboards ask for the same members many times a minute and every read is +// an RPC round trip; sponsor never changes and a buyer count up to 15 s old is fine for display +const memberCache = new Map(); async function member(id) { + const hit = memberCache.get(Number(id)); if (hit && Date.now() - hit.at < 15000) return hit.v; + const v = await memberRead(id); memberCache.set(Number(id), { at: Date.now(), v }); + if (memberCache.size > 5000) memberCache.clear(); + return v; +} +async function memberRead(id) { const r = await call(SEL.members, [id]); return { account: toAddr(word(r, 0)), sponsorId: toNum(word(r, 1)), buyerCount: toNum(word(r, 2)), activated: toNum(word(r, 3)) === 1, countedAsBuyer: toNum(word(r, 4)) === 1 }; @@ -243,7 +252,7 @@ async function tail() { const ev = decodeLog(lg); if (!ev) continue; ev.ts = Date.now(); // indexed-at time: powers honest time-series charts - state.events.push(ev); + state.events.push(ev); memberCache.clear(); // a new chain event can change any member: drop the 15 s read cache tally(ev); if (onEvent) { try { onEvent(ev); } catch (e) { console.error('chain onEvent', e.message); } } }