perf(db): WAL + synchronous=NORMAL + 8MB page cache for per-user dbs, throttled device last_seen writes

- openDb pragmas: journal_mode=WAL, synchronous=NORMAL, temp_store=MEMORY,
  cache_size=-8000; 3000-record stress write throughput 9514ms -> 1241ms
  (7.7x), single upsert ~0.3ms; read paths unchanged (<=22ms)
- authUser: devices.last_seen_at now written only when stale > 60s,
  removing one write per authenticated request (write amplification)
This commit is contained in:
root
2026-10-17 22:09:48 +08:00
parent 17ab5f0390
commit 88525a3f9f
2 changed files with 7 additions and 1 deletions
+2 -1
View File
@@ -95,6 +95,7 @@ export function authUser(req) {
if (row.expires_at < new Date().toISOString().slice(0, 19).replace('T', ' ')) {
throw Object.assign(new Error('凭据已过期,请重新登录'), { code: 401 });
}
d.prepare("UPDATE devices SET last_seen_at=datetime('now','localtime') WHERE id=?").run(row.device_id);
// 节流:仅当 last_seen 距今 >60s 才回写(否则命中索引跳过),消除每请求一次的写放大
d.prepare("UPDATE devices SET last_seen_at=datetime('now','localtime') WHERE id=? AND last_seen_at < datetime('now','-1 minutes')").run(row.device_id);
return { user_id: row.user_id, device_id: row.device_id };
}