Skip to content

A Bun + TypeScript server that caches GitHub user API responses in Redis to reduce latency and avoid repeated API calls.

Notifications You must be signed in to change notification settings

jlg-formation/redis-cache-github

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

redis-cache

A Bun + TypeScript server that caches GitHub user API responses in Redis to reduce latency and avoid repeated API calls.

Features

  • 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

How It Works

  1. A GET request to /user/:username is received.
  2. The server checks Redis for a cached response using the key cache:github:<SHA-256 of the URL>.
  3. If present (HIT): returns the cached JSON.
  4. 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.
  5. Each request is logged, e.g.:
    [2025-05-23T10:30:00Z] MISS - /user/torvalds (312 ms)
    

Getting Started

Prerequisites

  • Bun
  • Redis
  • Node.js (for Redis client compatibility)

Installation

bun install

Configuration

  • Redis connection is handled by the redis package.
  • The server listens on the port defined by the PORT environment variable or defaults to 3000.

Running the Server

bun run index.ts

Project Structure

License

Private project.

About

A Bun + TypeScript server that caches GitHub user API responses in Redis to reduce latency and avoid repeated API calls.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published