A simple command-line interface (CLI) tool written in Go that starts a caching proxy server. It forwards requests to an origin server, caches the responses, and returns cached responses for repeated requests. This project is built as a learning project following the specification from roadmap.sh Caching Server Project.
- Request Forwarding: Forwards incoming HTTP requests to a specified target origin server.
- Response Caching: Caches HTTP responses from the origin server so identical subsequent requests are served instantly from cache.
- Cache Status Headers: Adds an
X-CacheHTTP header to responses indicating whether data came from cache (HIT) or origin server (MISS). - Cache Clearing: Includes a CLI flag (
--clear-cache) to easily clear stored cache entries. - Zero External Dependencies: Built entirely using Go's standard library (
net/http,flag,fmt,os).
- Go (version 1.20 or higher recommended)
-
Clone or navigate to the project directory:
cd caching-proxy -
Build the CLI executable:
go build -o caching-proxy main.go
Once built, you can start the server or manage cache using ./caching-proxy (or directly using go run main.go):
caching-proxy --port <number> --origin <url>caching-proxy --port 3000 --origin http://dummyjson.comIf you make a request to http://localhost:3000/products, the proxy server forwards it to http://dummyjson.com/products and caches the response.
-
First Request (Cache Miss):
curl -i http://localhost:3000/products
Header in response:
X-Cache: MISS -
Second Request (Cache Hit):
curl -i http://localhost:3000/products
Header in response:
X-Cache: HIT
To clear the cache:
caching-proxy --clear-cacheThe caching proxy server attaches an X-Cache header to every response to indicate whether it was served from cache or the origin server:
X-Cache: HIT: The response was returned from the local proxy cache.X-Cache: MISS: The response was retrieved from the origin server and saved into cache.
- Project Specification: roadmap.sh Caching Server Project