Clear featured-list caches when featured learning paths are edited - #3757
Clear featured-list caches when featured learning paths are edited#3757mbertrand wants to merge 12 commits into
Conversation
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…tion Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The browser query client's 30-minute staleTime (matched to the CDN TTL) means the editor's own session never refetches the featured list after they edit a learning path — the backend purge only helps on a full page reload. Invalidate the featured query key in the mutations that map to the backend's cache-clearing write hooks so the editor sees their change immediately on client-side navigation too. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Hoist the tasks import to module level (tasks was already imported at module scope, so the function-local import bought nothing), shrink the bulk-action id set (featured_list_id__in coerces strings), and delete the tautological retry-config test. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…rize Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
OpenAPI ChangesNo changes detected Unexpected changes? Ensure your branch is up-to-date with |
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The cache-clear task landed on the default Celery queue, which also runs ETL backpopulates and bulk indexing, so a purge could sit behind minutes of queued work -- defeating the point of the feature (editor sees changes within seconds). The purge work is cheap (tuned Redis pattern delete plus two Fastly calls with 5s timeouts) and only triggered by rare human saves, so run it best-effort in the request via transaction.on_commit. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Adds cache invalidation when featured learning paths are edited.
Changes:
- Adds Fastly soft-purge support and backend invalidation hooks.
- Invalidates featured React Query caches after mutations.
- Adds backend/frontend tests and null-safe item counts.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
main/utils.py |
Adds soft-purge headers. |
main/utils_test.py |
Tests soft-purge behavior. |
learning_resources/views.py |
Adds featured-cache invalidation hooks. |
learning_resources/views_learningpath_test.py |
Tests backend invalidation paths. |
frontends/ol-components/src/components/LearningResourceCard/LearningResourceListCard.tsx |
Handles missing item counts. |
frontends/api/src/hooks/learningResources/index.ts |
Invalidates featured queries after bulk edits. |
frontends/api/src/hooks/learningResources/index.test.ts |
Tests bulk-edit invalidation. |
frontends/api/src/hooks/learningPaths/index.ts |
Invalidates featured queries after path mutations. |
frontends/api/src/hooks/learningPaths/index.test.ts |
Tests path-mutation invalidation. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| Runs synchronously in the request (not via Celery) so the purge is done | ||
| by the time the editor's save returns, regardless of worker backlog. |
There was a problem hiding this comment.
Intentional — the celery version sat on the default queue behind ETL backpopulates and bulk indexing, so a purge could wait minutes, which defeats the point of the issue (editor confirms their change within seconds). The work on the request path is small: a tuned Redis pattern delete plus a couple of Fastly purges that normally return in tens of ms. Worst case with Fastly down is ~5s per purge before the save returns (still succeeds, just logs). The PR description was stale from the earlier approach — updated it to match.
A failed channel-page purge no longer aborts the homepage purge (or other channel purges); each failure is logged and the loop continues. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.
Suppressed comments (1)
learning_resources/views.py:495
- If Redis raises here,
clear_featured_cachesexits before any Fastly purge runs; the outer callback catches the exception, but all channel and homepage objects remain stale. Handle/log the Redis failure locally so the independent best-effort Fastly purges still execute.
clear_views_cache(key_prefix="featured_resources")
What are the relevant tickets?
Closes https://github.com/mitodl/hq/issues/11979
Description (What does it do?)
Channel managers couldn't see featured-list edits for up to 24h (Redis view cache) plus 30min (Fastly page cache). Now:
featured_resourcesRedis cache, hard-purges/c/unit/<name>in Fastly, and soft-purges the homepage, once the change commits.call_fastly_purge_apigains asoft=Truekwarg (sendsFastly-Soft-Purge: 1).How can this be tested?
Seed featured lists if you haven't already:
In a private/incognito window (anonymous), load a unit channel page — e.g. http://open.odl.local:8062/c/unit/mitx — and note the courses in the featured carousel. Load the homepage too. This primes the anonymous Redis cache for
/api/v1/featured/.In a window where you're logged in as an admin user, go to http://open.odl.local:8062/learningpaths, open "MITx Featured Resources", and remove a course from the list (or add one via a course card's add-to-list dialog).
As soon as the edit saves you should see:
The Fastly purges are skipped locally (no
FASTLY_API_KEY); on RC/prod they become real PURGE requests against/c/unit/<name>(hard) and/(soft).Reload the incognito channel page and homepage: the featured carousel reflects the edit. Without this PR, the anonymous featured API response stays cached for
REDIS_VIEW_CACHE_DURATION(24h default in deployed envs) and the pages keep rendering the old list.Additional Context
?topic=...) stay cached up to s-maxage (30 min). The base URL the editor refreshes is hard-purged. Per-channel surrogate keys are the upgrade path if that ever matters.