Migrate Library’s In-Memory Dictionaries to Redis with Minimal Code Changes #2198
+359
−38
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.
Summary
This pull request replaces the large in-memory dictionaries in
Library
with Redis-backed dictionary wrappers, drastically reducing our Python RAM footprint while preserving the original dictionary syntax in the rest of the codebase.Background
The
Library
class previously stored a large amount of data (indexes, titles, terms, etc.) as standard Python dictionaries. Over time, this led to high memory usage and increased startup times. We want to offload these dictionaries to Redis, but minimize disruptive code changes.Implementation
RedisNestedHash
: Behaves like a “dictionary of dictionaries,” allowing usage likelibrary._index_title_maps["en"]["Genesis"] = {...}
.RedisHashPrefix
: Behaves like a single-level dictionary, storing keys in a Redis hash under a specific prefix._index_title_maps
,_title_node_maps
, and_term_ref_maps
withRedisNestedHash
instances._index_map
with aRedisHashPrefix
instance.library._index_title_maps["en"][...] = ...
) continues to work without modifications.__getitem__
,__setitem__
, etc.), there are no major changes needed in consumer code.Testing & Verification
library._index_title_maps["en"]["Genesis"]
) reads/writes correctly to Redis.Next Steps
_toc
or_topic_toc
).By merging this PR, we ensure that the
Library
class now leverages Redis for data storage without breaking existing code patterns or forcing widespread refactoring.