A Bun + TypeScript server that caches GitHub user API responses in Redis to reduce latency and avoid repeated API calls.
- Endpoint:
GET /user/:username
- Cache: Redis, with keys in the format
cache:github:<sha256(url)>
- TTL: 30 seconds (automatic expiration, no manual refresh)
- Error Handling: Error responses (e.g., 404, 403) are not cached
- Logging: Logs each request with HIT/MISS, response time, timestamp, and URL
- A GET request to
/user/:username
is received. - The server checks Redis for a cached response using the key
cache:github:<SHA-256 of the URL>
. - If present (HIT): returns the cached JSON.
- If absent (MISS): fetches from
https://api.github.com/users/:username
.- If the response is 200 OK: stores the raw JSON in Redis (TTL 30s) and returns it.
- If the response is an error: does not cache, returns the error to the client.
- Each request is logged, e.g.:
[2025-05-23T10:30:00Z] MISS - /user/torvalds (312 ms)
bun install
- Redis connection is handled by the
redis
package. - The server listens on the port defined by the
PORT
environment variable or defaults to3000
.
bun run index.ts
index.ts
: Main server logicutils/hash.ts
: SHA-256 hashing utility for cache keysSPECIFICATIONS.md
: Project specifications and requirements
Private project.