Skip to content

MaythFall/api-cache-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

API Cache Cemo

A small FastAPI backend demonstrating a cache-aside pattern using Redis and PostgreSQL. The API uses Redis as an optional in-memory cache with TTL and falls back to the database as the main data source. This project is intentionally minimal and was focused on backend architecture and tool/library familiarity rather than frontend work.


Stack

  • Python
  • FastAPI
  • Redis
  • PostgreSQL
  • SQLAlchemy

How it works

  • GET /items/{id}
    • Checks Redis first, falls back to check Postgres on cache miss, then writes to cache with TTL.
  • PUT /items/{id}
    • Writes to Postgres and invalidates the related Redis cache key.
  • Redis is optional - if unavailable, the API still serves data from the database.

Running Locally

python -m venv apiproj
apiproj\Scripts\activate
pip install -r requirements.txt

Create .env file

DATABASE_URL=postgresql+psycopg2://appuser:apppass@localhost:5432/appdb
REDIS_HOST=localhost
REDIS_PORT=6379
CACHE_TTL_SECONDS=60

Start Redis (WSL or local install), then run

uvicorn app:app --reload

Open Link

http://127.0.0.1:8000/ auto redirects to docs

Endpoints

  • GET /items/{id}
  • PUT /items/{id}
  • DELETE /items/{id}
  • GET /health

Notes

  • Cached values are JSON-serialized
  • Cache keys use the format item:{id}
  • TTL is configurable via environment variables

About

A small project experimenting with Redis caching for a PostgreSQL server

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages