A lightweight and efficient caching proxy server built with Go. This server forwards requests to a specified origin server, caches the responses, and serves subsequent identical requests from the cache. It includes a CLI for starting the server and clearing the cache.
- Proxy Requests: Forwards HTTP requests to an origin server.
- Response Caching: Caches responses to identical requests for faster subsequent retrievals.
- Custom Headers:
X-Cache: HITif the response is served from the cache.X-Cache: MISSif the response is forwarded from the origin server.
- CLI Options:
- Start the server with custom port and origin.
- Clear the cache directly from the CLI.
- Concurrency: Supports multiple simultaneous requests with thread-safe caching.
- Go (1.19 or later)
-
Clone the repository:
git clone https://github.com/your-username/caching-proxy.git cd caching-proxy -
Build the project:
go build -o caching-proxy ./cmd/caching-proxy
-
Verify the build:
./caching-proxy --help
Run the server with a custom port and origin URL:
./caching-proxy --port <number> --origin <url>Example: Start the server on port 3000 and forward requests to http://dummyjson.com:
./caching-proxy --port 3000 --origin http://dummyjson.comBehavior:
-
A request to http://localhost:3000/products will forward to http://dummyjson.com/products and cache the response.
-
Subsequent requests to http://localhost:3000/products will return the cached response.
Clear all cached responses without starting the server:
./caching-proxy --clear-cachecaching-proxy/
├── cmd/
│ └── caching-proxy/
│ └── main.go # CLI entry point
├── internal/
│ ├── cache/
│ │ └── cache.go # Cache implementation
│ ├── proxy/
│ │ └── server.go # Proxy server logic
├── go.mod # Go module file
├── README.md # Project documentation-
Request Handling:
- The server listens on the specified port and intercepts HTTP requests.
- For each request:
- Cache Hit: Return the cached response with
X-Cache: HIT. - Cache Miss: Forward the request to the origin server, cache the response, and return it with
X-Cache: MISS.
- Cache Hit: Return the cached response with
-
Cache Storage:
- Responses are cached in-memory using a thread-safe mechanism (
sync.Map). - The cache key is constructed using the HTTP method and request URL.
- Responses are cached in-memory using a thread-safe mechanism (
-
Cache Management:
- A
ClearCachefunction is provided to reset the cache.
- A
- Time-to-Live (TTL): Add support for cache expiration.
- Persistent Cache: Save cache to disk for persistence across restarts.
- Configurable Options: Allow users to configure cache size and TTL.
- HTTPS Support: Add support for forwarding secure HTTPS requests.
Contributions are welcome! Feel free to submit issues or pull requests to improve the project.