diff --git a/packages/app/src/Cache/FeedCache.ts b/packages/app/src/Cache/FeedCache.ts index 0c53ca1e..6300ebeb 100644 --- a/packages/app/src/Cache/FeedCache.ts +++ b/packages/app/src/Cache/FeedCache.ts @@ -15,6 +15,8 @@ export default abstract class FeedCache { #hooks: Array = []; #snapshot: Readonly> = []; #changed = true; + #hits = 0; + #miss = 0; protected onTable: Set = new Set(); protected cache: Map = new Map(); @@ -23,7 +25,10 @@ export default abstract class FeedCache { this.#table = table; setInterval(() => { console.debug( - `[${this.#name}] ${this.cache.size} loaded, ${this.onTable.size} on-disk, ${this.#hooks.length} hooks` + `[${this.#name}] ${this.cache.size} loaded, ${this.onTable.size} on-disk, ${this.#hooks.length} hooks, ${( + (this.#hits / (this.#hits + this.#miss)) * + 100 + ).toFixed(1)} % hit` ); }, 5_000); } @@ -56,7 +61,13 @@ export default abstract class FeedCache { getFromCache(key?: string) { if (key) { - return this.cache.get(key); + const ret = this.cache.get(key); + if (ret) { + this.#hits++; + } else { + this.#miss++; + } + return ret; } } diff --git a/packages/app/src/Element/ProfilePreview.tsx b/packages/app/src/Element/ProfilePreview.tsx index b7db72f3..0d927bb2 100644 --- a/packages/app/src/Element/ProfilePreview.tsx +++ b/packages/app/src/Element/ProfilePreview.tsx @@ -17,8 +17,8 @@ export interface ProfilePreviewProps { } export default function ProfilePreview(props: ProfilePreviewProps) { const pubkey = props.pubkey; - const user = useUserProfile(pubkey); const { ref, inView } = useInView({ triggerOnce: true }); + const user = useUserProfile(inView ? pubkey : undefined); const options = { about: true, ...props.options, diff --git a/packages/app/src/System/ProfileCache.ts b/packages/app/src/System/ProfileCache.ts index 9add0168..fe96a8e4 100644 --- a/packages/app/src/System/ProfileCache.ts +++ b/packages/app/src/System/ProfileCache.ts @@ -19,11 +19,13 @@ class ProfileLoaderService { * Request profile metadata for a set of pubkeys */ TrackMetadata(pk: HexKey | Array) { + const bufferNow = []; for (const p of Array.isArray(pk) ? pk : [pk]) { - if (p.length > 0) { - this.WantsMetadata.add(p); + if (p.length > 0 && this.WantsMetadata.add(p)) { + bufferNow.push(p); } } + UserCache.buffer(bufferNow); } /** @@ -55,7 +57,7 @@ class ProfileLoaderService { .filter(a => (UserCache.getFromCache(a)?.loaded ?? 0) < expire); const missing = new Set([...missingFromCache, ...expired]); if (missing.size > 0) { - console.debug(`Wants profiles: ${missingFromCache.length} missing, ${expired.length} expired`); + console.debug(`[UserCache] Wants profiles: ${missingFromCache.length} missing, ${expired.length} expired`); const sub = new RequestBuilder(`profiles`); sub