Skip to content

Cache negative proxy results to reduce battery drain#12

Open
greenart7c3 wants to merge 1 commit into
masterfrom
claude/intelligent-meitner-6zzrnf
Open

Cache negative proxy results to reduce battery drain#12
greenart7c3 wants to merge 1 commit into
masterfrom
claude/intelligent-meitner-6zzrnf

Conversation

@greenart7c3

Copy link
Copy Markdown
Owner

Summary

This PR implements a negative result cache for blob proxy requests to significantly reduce battery consumption when clients repeatedly request unavailable blobs. Previously, each request for a missing blob would trigger a full crawl across all hint servers and relay lookups, even if the blob was already confirmed as unavailable moments earlier.

Key Changes

  • Added notFoundCache (ConcurrentHashMap) to store recently failed blob lookups with a 60-second TTL and 256-entry limit
  • Implemented proxyCacheKey() to generate cache keys from blob hash, xs servers, and author pubkeys (sorted for consistency)
  • Added isRecentlyNotFound() to check cache and lazily remove expired entries
  • Added markNotFound() to record failed lookups with opportunistic cleanup of expired entries when cache reaches capacity
  • Integrated cache checks in both GET and HEAD request handlers to skip proxy crawls for recently failed lookups
  • Updated version to 0.0.4 and added changelog entry

Implementation Details

  • Cache key includes sorted lists of hint servers and author pubkeys to ensure identical requests hit the cache while different hints trigger fresh crawls
  • TTL of 60 seconds balances battery savings with responsiveness to newly available blobs
  • Max 256 entries prevents unbounded memory growth from one-off requests
  • Expired entries are opportunistically cleaned up when the cache reaches capacity, avoiding periodic background cleanup
  • Both GET and HEAD endpoints now check and update the cache consistently

https://claude.ai/code/session_014cykkDSZo6C4qTruWg3cko

A request for a blob that is not cached locally triggers a full proxy
crawl: a connection attempt to every xs hint server plus a nostr relay
lookup per author pubkey, frequently routed through Tor. Nothing about a
"not found" outcome was remembered, so a client that keeps asking for an
unavailable blob (retries, re-scrolling a feed) re-ran that entire crawl
on every request, which is the main battery drain while proxying.

Cache the negative result briefly, keyed by the blob hash plus the exact
xs/as hints supplied, so repeated identical requests answer 404 without
touching the network. A request carrying different hints still gets a
fresh crawl, and the short TTL lets a blob that becomes available be
picked up soon after.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014cykkDSZo6C4qTruWg3cko
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.

2 participants