JMCP: Papers - jmcp/cache-readdemostate-per-request-to-avoid-aNWlu7w3#10
Closed
landigf wants to merge 1 commit into
Closed
JMCP: Papers - jmcp/cache-readdemostate-per-request-to-avoid-aNWlu7w3#10landigf wants to merge 1 commit into
landigf wants to merge 1 commit into
Conversation
Owner
Author
|
Closing: will integrate valuable features directly on main to avoid cascade merge conflicts. |
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.
Review: Cache
readDemoStateper request viaAsyncLocalStorageChange summary
AsyncLocalStorage<{ state: DemoState | null }>indemo-store.tsto cache the parsed JSON state within a single async call treereadDemoState()checks the ALS store first; on miss, reads disk and populates the cachewriteDemoState()is write-through — updates the ALS cache so subsequent reads in the same request see the mutationDemoRepository.cached()helper ensures every public method runs inside an ALS context (creates one if none exists, reuses if nested)DemoRepositorymethod body is wrapped inthis.cached(async () => { ... })— purely mechanical indentation change, no logic alterationsValidation confidence: High
The approach is correct.
AsyncLocalStorageis the standard Node.js primitive for request-scoped state. Key properties hold:demoCache.run()call creates an isolated storewriteDemoStateupdates the store, so read-after-write within a request is correctgetDailyDigestcallsgetFeed,listTrendingPapers,getOpportunities, andgetViewerinternally; they all share the same ALS context and hit disk only oncetoggleStar) modify the in-memory state object then write it back, and the cache holds the same referenceRisks that remain
External concurrent writes are invisible within a request — if another process or request writes the JSON file mid-flight, the cached state won't reflect it. Acceptable for a single-user demo store, but worth noting if this ever runs multi-tenant.
getDemoCacheStoreis exported but only consumed internally byDemoRepository.cached(). Minor API surface leak — could be unexported if nothing outsideindex.tsneeds it.Large mechanical diff (~600 lines of indentation change) makes it easy to miss a subtle brace or logic error buried in the noise. I didn't spot any, but this is the kind of diff where a typo hides well. The type checker is the real safety net here — recommend confirming
npm run checkpasses cleanly.No per-method result caching — if the same repo method (e.g.,
getViewer) is called multiple times within a request with the same arguments, it re-runs the in-memory filtering each time (just skips the disk read). This is fine for the current scale but is the next optimisation if needed.