Skip to content

perf(api): cache the live link item lookup for public item pages - #1083

Merged
Zach Dunn (zachdunn) merged 2 commits into
mainfrom
claude/live-link-item-lookup-cache
Oct 5, 2026
Merged

Zach Dunn (zachdunn) merged 2 commits into
mainfrom
claude/live-link-item-lookup-cache

Conversation

@zachdunn

Copy link
Copy Markdown
Member

In plain terms

A public live link item page (/c/<id>/<item>) used to find its item by listing the live link's whole scope and hashing every object key until one matched the item id. That ran on every request, with no sign-in and no cache. PR comments now link every file to its item page, so this path gets more traffic. This change keeps each live link's hashed scope list in a short-lived server-side cache, so most item page requests skip the scan.

What it does / what it is not

  • Caches each live link's hashed scope list (item id, object key, timestamp) in a named Workers Cache API cache for 60 seconds. The cache key is the live link id plus its scope, so no list is shared across live links. The cache is server-side only and is never served to clients.
  • The cached list is a hint, not the authority:
    • The route still resolves the live link in D1 first, so a revoked link 404s even while its list is cached.
    • An id found in the cached list is served only after a one-row D1 check that its key is still in scope. A deleted or re-scoped file fails the check, and the list is rebuilt.
    • An id not in the cached list runs one query for scope rows stamped at or after the list's newest row. The list is rebuilt only when one of those rows matches the id. A random or stale id gets a 404 without a full scan.
    • Hydration is unchanged, so private objects are still withheld, and missing objects are still missing.
  • With no Cache API (unit tests, workers.dev previews), every lookup scans as before.
  • Not changed: the public cursor shape {v,u,h}, the private count cap and its null semantics, title privacy, and hydrateFeedItems.
  • Not done: the public cursor decode for GET /public/feeds/:id?cursor= already narrows its lookup to rows that share one exact timestamp (an indexed equality), so it does not do the 2,000-row scan. I left it as is.
  • Not done: per-IP rate limiting. The web worker calls this endpoint over the API service binding with no client IP, so a per-IP key would put all SSR traffic in one bucket. A rate limit belongs at the edge on /c/* if one is needed.
  • Known gap: a file that joins the scope without a new gh.repo timestamp (for example, a path row added later to an existing file) can 404 on its item page until the cached list expires (60 seconds at most).

Technical notes

  • New module apps/api/src/live-link-index.ts (findLiveLinkItem). publicFeedItemPage now takes prev/next ids from the list instead of hashing them again.
  • pr-scope.ts: scanScopeKeys takes since (updated_at >= ?, served by file_metadata_gh_repo_recent_idx), and the new scopeHasKey checks a single key.
  • A stored entry carries its build time, and reads reject anything older than the TTL, even when the Cache API would still return it.
  • No migration, no new binding, and no changeset (@uploads/api is ignored by changesets).

Test plan

  • pnpm --filter @uploads/api test: new cases in routes-feeds.test.ts with an in-memory caches fake. They count scope queries to cover: a cached hit with no rescan, a 404 for an unknown id with one bounded query, a file uploaded after caching, a deleted file never served from the cache, a file made private after caching (withheld), list isolation between two live links, a revoked link, and TTL expiry.
  • I confirmed that 5 of the new cases fail with the cache disabled. The isolation and TTL cases pass either way by design.
  • Existing pager and cursor tests pass unchanged on the uncached path.
  • pnpm --filter @uploads/api typecheck, pnpm check
  • After deploy: open a few /c/<id>/<item> pages from a PR comment and confirm the neighbours and the withheld items look right.

Closes #1072

Public item pages (/c/<id>/<item>) resolved an item id by scanning the live
link's scope and hashing every key until one matched, on every request.
Keep each live link's hashed scope list in a named Cache API cache for 60
seconds. A cached hit is served only after a one-row check that its key is
still in scope; an id missing from the list costs one query for rows newer
than the list, and only rebuilds the list when one of them matches.

Closes #1072
@changeset-bot

changeset-bot Bot commented Oct 5, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 13e3357

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai

coderabbitai Bot commented Oct 5, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are limited based on label configuration.

🏷️ Required labels (at least one) (2)
  • coderabbit:review
  • review
🚫 Excluded labels (none allowed) (1)
  • wip

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 37486e0d-d563-4930-bcab-9cd9b4720183

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
uploads-api 13e3357 Commit Preview URL

Branch Preview URL
Oct 05 2026, 11:45 AM

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
uploads-web 13e3357 Commit Preview URL

Branch Preview URL
Oct 05 2026, 11:45 AM

@zachdunn
Zach Dunn (zachdunn) merged commit 5a7de30 into main Oct 5, 2026
5 checks passed
@zachdunn
Zach Dunn (zachdunn) deleted the claude/live-link-item-lookup-cache branch October 5, 2026 11:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cache or index the public live link item lookup

1 participant