Skip to content

Add in-memory caching for authenticated user info - #8

Merged
xingyaoww merged 4 commits into
mainfrom
auth-cache-user-info
Mar 23, 2026
Merged

xingyaoww merged 4 commits into
mainfrom
auth-cache-user-info

Conversation

@malhotra5

Copy link
Copy Markdown
Member

Summary

Adds a 20-second in-memory cache for authenticated user information in the auth middleware using cachetools.TTLCache.

Changes

  • Added cachetools dependency for TTLCache functionality
  • Created TTLCache with 20-second TTL and max 1024 entries
  • Updated authenticate_request to check cache before making OpenHands API calls
  • Added clear_auth_cache() helper for testing
  • Added comprehensive tests for caching behavior

Rationale

Why 20 seconds TTL?

  • Short enough that revoked API keys stop working within 20 seconds (security)
  • Long enough for integration tests to proceed smoothly without hitting rate limits
  • Reduces load on the OpenHands API for repeated requests

Why cachetools instead of Redis?

  • No external infrastructure required for prototyping
  • Lightweight pure-Python library (~15KB)
  • Simple API that works like a dict with automatic TTL expiration

How it works

  1. First request with an API key → validates against OpenHands API → caches result
  2. Subsequent requests within 20 seconds → returns cached user (no API call)
  3. After 20 seconds → cache entry auto-expires → next request hits API again

- Add cachetools TTLCache with 20-second TTL for auth responses
- Cache authenticated user info to reduce OpenHands API calls
- 20s TTL ensures revoked keys stop working within 20 seconds
- Supports smooth integration test execution without rate limits
- No external infrastructure (Redis) required for prototyping

Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions

github-actions Bot commented Mar 20, 2026 •

Copy link
Copy Markdown
Contributor

@malhotra5
malhotra5 marked this pull request as ready for review March 20, 2026 21:40
Co-authored-by: openhands <openhands@all-hands.dev>

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Acceptable - Pragmatic caching solution with solid fundamentals. One important testing gap to address.

KEY INSIGHT: The core logic is clean and solves a real problem (rate limits), but the TTL expiration test doesn't actually verify the mechanism it claims to test.

Comment thread tests/test_auth.py Resolved
Comment thread tests/test_auth.py Resolved
Comment thread automation/auth.py Resolved
Comment thread automation/auth.py Resolved
- Change AUTH_CACHE_TTL_SECONDS to float (20.0) for consistency
- Add debug log for cache misses to match cache hit logging
- Fix TTL test to actually test TTL expiration using short TTL cache

Co-authored-by: openhands <openhands@all-hands.dev>
@malhotra5
malhotra5 requested a review from all-hands-bot March 20, 2026 21:46

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Good taste - Pragmatic caching solution that solves a real problem simply.

KEY INSIGHT: This is exactly what prototype infrastructure should look like: transparent caching with a sensible TTL, clean logic flow with no special cases, and tests that verify the actual mechanisms (TTL expiration, failure handling). Previous review feedback has been properly addressed. The 20-second security trade-off is documented and reasonable. Ship it.

@xingyaoww xingyaoww left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try this

@xingyaoww
xingyaoww merged commit 4b1b238 into main Mar 23, 2026
3 checks passed
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.

4 participants