Optimize: Cache navigation bar queries (TASK-20260202)#3
Optimize: Cache navigation bar queries (TASK-20260202)#3
Conversation
- Added judge/utils/caching.py with get_cached_navbar and get_cached_nav_tab. - Updated judge/template_context.py to use cached functions. - Added signal handlers in judge/signals.py to invalidate cache on NavigationBar changes. - Added judge/tests/test_navbar_cache.py for regression testing.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e8d0f79b90
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| cache.delete(NAVBAR_CACHE_KEY) | ||
| bump_nav_tab_version() |
There was a problem hiding this comment.
Defer navbar cache invalidation until updates are committed
Invalidating navbar_tree directly in the post_save/post_delete signal can cache an inconsistent tree: NavigationBarAdmin.changelist_view saves items with disable_mptt_updates() and only rebuilds tree fields afterward (judge/admin/interface.py), so a concurrent request can repopulate navbar_tree from pre-rebuild rows and keep that stale hierarchy for the full TTL. Triggering invalidation on commit (or after the rebuild path completes) avoids persisting an inconsistent navbar.
Useful? React with 👍 / 👎.
| version = cache.get(NAV_TAB_VERSION_KEY) | ||
| if version is None: | ||
| version = 1 | ||
| cache.set(NAV_TAB_VERSION_KEY, version + 1, NAV_TAB_CACHE_TTL) |
There was a problem hiding this comment.
Increment nav-tab cache version atomically
bump_nav_tab_version() does a get then set, which is not atomic under concurrent updates. If two NavigationBar saves/deletes run at the same time, both can read the same old version and write the same incremented value, so only one logical invalidation happens; a request between those writes can cache nav-tab data that remains stale after the second update because the version did not advance again.
Useful? React with 👍 / 👎.
Automated fix for TASK-20260202. Moved from master commit e8d0f79.