Flask and FastAPI based caching proxy server with TTL, clear cache and X-Cache headers
This project is a simple proxy server with caching support using Flask and FastAPI.
It forwards requests to an origin server and caches responses, adding headers to indicate cache status (X-Cache: HIT
/ MISS
).
caching_proxy.py
: Flask-based versionproxy_fastapi.py
: FastAPI version
It acts as a middle layer between the client and an origin server, caching GET responses and returning X-Cache: HIT
or MISS
.
✅ Caching for GET requests
✅ Auto-expiry using TTL
✅ X-Cache
response header
✅ Clear Cache with /clear-cache
endpoint
✅ Persistent caching using pickle
✅ Support for POST/PUT/DELETE passthrough (FastAPI)
git clone https://github.com/YOUR_USERNAME/caching-proxy-python.git
cd caching-proxy-python
python -m venv venv
source venv/bin/activate # macOS/Linux
venv\Scripts\activate # Windows
(Separate installations) For Flask version:
pip install flask requests
For FastAPI version:
pip install fastapi uvicorn httpx
Install dependencies using:
pip install flask fastapi uvicorn requests
Flask Proxy version:
python caching_proxy.py --origin http://dummyjson.com --port 3000 --ttl 60
Then open in browser:
http://localhost:3000/products
You’ll get:
X-Cache: MISS on first call
X-Cache: HIT on subsequent calls
X-Cache: MISS again after TTL expires
To clear cache:
http://localhost:3000/clear-cache
FastAPI Proxy version:
python proxy_fastapi.py --origin http://dummyjson.com --port 3000 --ttl 60
Open in browser:
http://localhost:3000/products
FastAPI also supports:
POST/PUT/DELETE passthrough (with X-Cache: BYPASS)
Built-in docs: http://localhost:3000/docs
GET /products → fetch product data (cached)
POST /posts → add a new post (forwarded)
PUT /posts/1 → update a post (forwarded)
DELETE /posts/1 → delete a post (forwarded)
GET /clear-cache → clears the cache manually
GET (cached):
curl http://127.0.0.1:3000/products
First time: X-Cache: MISS Next time: X-Cache: HIT
POST:
curl -X POST http://127.0.0.1:3000/posts -H "Content-Type: application/json" -d "{\"title\": \"New Post\"}"
PUT:
curl -X PUT http://127.0.0.1:3000/posts/1 -H "Content-Type: application/json" -d "{\"title\": \"Updated Post\"}"
DELETE:
curl -X DELETE http://127.0.0.1:3000/posts/1
Clear cache:
curl http://127.0.0.1:3000/clear-cache
Auto Expiry (TTL) The server automatically expires cache entries after 60 seconds (FastAPI version).
https://github.com/rhakia/caching-proxy-python
Roadmap.sh's Caching Proxy Python project idea.