fix: implement thread-safe in-memory cache in data_loader.py#272
Draft
anshul23102 wants to merge 1 commit into
Draft
fix: implement thread-safe in-memory cache in data_loader.py#272anshul23102 wants to merge 1 commit into
anshul23102 wants to merge 1 commit into
Conversation
|
@anshul23102 is attempting to deploy a commit to the komalsony234-1530's projects Team on Vercel. A member of the Team first needs to authorize it. |
Author
|
Note: Converting this to a draft PR until the issue is assigned to me. The fix is ready and all 29 tests pass, will mark as ready for review once assigned. |
_projects_cache was declared but load_all_projects() never read or wrote it, causing a redundant disk read on every request. Added double-checked locking with threading.Lock so the JSON file is read once and reused safely across concurrent requests. clear_cache() now acquires the same lock before resetting.
9b1012b to
d087353
Compare
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.
Closes #271
What was wrong
utils/data_loader.pydeclared_projects_cache = Noneandclear_cache(), butload_all_projects()never read or wrote the cache. Every HTTP request to/,/api/recommend, and/project/<id>caused at least one redundantopen()+json.load()on the same static file.What changed
load_all_projects()now populates_projects_cacheon the first call and returns the cached list on every subsequent call.threading.Lockwith double-checked locking prevents a race condition where two concurrent requests during cold start could both read the file.clear_cache()now acquires the same lock before resetting, making it safe in threaded test environments.get_project_stats()into one pass, no behaviour change.Tests
All 29 existing tests pass with no changes required:
Type of change