Repository navigation
feat(lru-memo-cache): add serializeCache and hydrateCache for SSR persistence - #1022
Closed
arkhindi12 wants to merge 1 commit into
Closed
arkhindi12 wants to merge 1 commit into
arkhindi12 wants to merge 1 commit into
Conversation
…sistence Cache memory is lost on server-side rendering re-hydration, so every SSR pass re-queries contract metadata that the previous request already had. Adds a versioned JSON snapshot pair: - serializeCache() writes entries oldest -> newest so replaying them restores LRU recency, and stores expiries as absolute epoch-ms deadlines so a snapshot in transit does not hand entries a fresh TTL. - hydrateCache() rebuilds the cache with its original maxEntries/ttlMs, drops entries that expired while the snapshot was in transit, and rejects unknown versions or malformed input with RangeError. Hit/miss counters are not serialized: they describe the process that produced them, and carrying them over would make metrics() report hits this cache never served. Closes conduit-protocol#977
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Cache memory is lost on server-side rendering re-hydration, so each SSR pass re-queries contract metadata the previous request already had.
Adds the two helpers the issue asks for in
src/lru-memo-cache.ts:serializeCache(): string— versioned JSON snapshotLruMemoCache.hydrateCache<K, V>(json): LruMemoCache<K, V>— rebuild from a snapshotThree details that matter for the SSR case
Expiries are stored as absolute epoch-ms deadlines, not remaining TTLs. A snapshot that sits in a transport for ten minutes must not hand every entry a fresh lease on life — otherwise hydration quietly serves stale contract metadata, which is the exact problem this is meant to solve.
Entries are written oldest → newest. Replaying them in that order restores LRU recency, so eviction after hydration still drops the coldest entry instead of an arbitrary one.
Hit/miss counters are not serialized. They describe the process that produced the snapshot; carrying them across would make
metrics()report hits this cache never served. A hydrated cache starts at zero andmeasuredSpeedupPercentback atnull.Robustness
hydrateCachethrowsRangeError— rather than returning a half-built cache — when:versionis notLRU_CACHE_SNAPSHOT_VERSION(currently1)maxEntriesis not a positive integer, orttlMsis present but invalidentriesis missing, or an entry has nokey/ a non-finiteexpiresAtEntries already expired at hydration time are dropped. A snapshot claiming more entries than
maxEntrieskeeps the newest ones, which is what replaying in order would have produced anyway.Tests
src/tests/lru-memo-cache.test.ts— 9 new cases, 17 total in the file, all passing:Covered: round-trip, recency order preserved through eviction, absolute expiry (400ms of a 1000ms TTL remains after a 600ms gap — not a fresh 1000ms), expired-in-transit entries dropped, default
ttlMscarried across, metrics zeroed, unsupported version rejected, malformed snapshots rejected, over-long snapshot truncated tomaxEntries.tsc --noEmitreports no errors inlru-memo-cache.ts.Note: the repo has pre-existing failures in
custom-fetch*.test.ts,soroban-retry-after.test.ts,streams.test.tsandrelayer-stress.test.ts(17 tests). Verified viagit stashthat they fail identically on a cleanmain, so they are unrelated to this change.On assignment
I applied to this issue but haven't been assigned yet. Opening this as a draft so the approach is reviewable and the code is ready if you assign it — happy to close it without ceremony if you give this to someone else. If you'd rather review before any branch exists, say so and I'll take it down.
Closes #977