Cache server snapshots for 5 minutes and profiles for an hour
This commit is contained in:
parent
9ab145f816
commit
7883e6df8a
1 changed files with 17 additions and 6 deletions
|
|
@ -8,12 +8,17 @@ import type { Query } from "$lib/forum/query";
|
|||
// Anonymous relay access for server rendering. The server has no signer, so
|
||||
// it only ever sees what the relay serves the public; members get their
|
||||
// private rooms once the client takes over. Every query is bounded so a slow
|
||||
// relay degrades to an empty page, never a hung request, and results are
|
||||
// cached briefly to absorb crawler bursts.
|
||||
// relay degrades to an empty page, never a hung request.
|
||||
//
|
||||
// Results are cached in memory. Freshness only matters to crawlers and cold
|
||||
// refreshes — the browser always refetches live data after hydration — so
|
||||
// forum data lives as long as the edge cache (CACHE_CONTROL's s-maxage) and
|
||||
// profiles, which rarely change, for an hour.
|
||||
const QUERY_TIMEOUT = 2500;
|
||||
const PROFILE_TIMEOUT = 1500;
|
||||
const CONNECT_TIMEOUT = 1500;
|
||||
const CACHE_TTL = 30_000;
|
||||
const FORUM_TTL = 5 * 60_000;
|
||||
const PROFILE_TTL = 60 * 60_000;
|
||||
const DEAD_RELAY_TTL = 5 * 60_000;
|
||||
const CACHE_MAX = 2000;
|
||||
|
||||
|
|
@ -89,11 +94,12 @@ function cachedQuery(
|
|||
relays: string[],
|
||||
filter: Filter,
|
||||
maxWait: number,
|
||||
ttl: number,
|
||||
): Promise<Event[]> {
|
||||
const key = relays.join(",") + "|" + JSON.stringify(filter);
|
||||
const now = Date.now();
|
||||
const hit = cache.get(key);
|
||||
if (hit && now - hit.at < CACHE_TTL) return hit.result;
|
||||
if (hit && now - hit.at < ttl) return hit.result;
|
||||
if (cache.size >= CACHE_MAX) cache.clear();
|
||||
const result = boundedQuery(relays, filter, maxWait);
|
||||
cache.set(key, { at: now, result });
|
||||
|
|
@ -101,9 +107,14 @@ function cachedQuery(
|
|||
}
|
||||
|
||||
export const forumQuery: Query = (filter) =>
|
||||
cachedQuery([RELAY_URL], filter, QUERY_TIMEOUT);
|
||||
cachedQuery([RELAY_URL], filter, QUERY_TIMEOUT, FORUM_TTL);
|
||||
|
||||
// Profiles mostly live off the forum relay; ask it too for members who only
|
||||
// published there.
|
||||
export const profileQuery: Query = (filter) =>
|
||||
cachedQuery([RELAY_URL, ...PROFILE_RELAYS], filter, PROFILE_TIMEOUT);
|
||||
cachedQuery(
|
||||
[RELAY_URL, ...PROFILE_RELAYS],
|
||||
filter,
|
||||
PROFILE_TIMEOUT,
|
||||
PROFILE_TTL,
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue