# Caching Proxy Server
This project is a simple caching proxy server implemented in Go. It forwards client requests to an origin server and caches the responses to improve performance and reduce load on the origin server. Cached responses are served for a specified duration, after which they are automatically cleaned up.
- HTTP Proxy: Forwards client requests to the origin server.
- Response Caching: Stores responses for a configurable time-to-live (TTL).
- Cache Expiry and Cleanup: Automatically cleans up expired cache entries at regular intervals.
- Manual Cache Clearing: Option to manually clear the cache.
- Cache Hits and Misses: Logs cache hits, misses, and expirations for better observability.
-
Install Go on your system.
-
Clone this repository:
git clone <repository-url> cd <repository-name>
-
Build the project:
go build -o caching-proxy .
To start the proxy server, use the following flags:
--port
: The port on which the proxy server will run.--origin
: The origin server URL to which requests will be forwarded.
Example:
./caching-proxy --port=8080 --origin=http://example.com
The proxy server will listen on http://localhost:8080
and forward requests to http://example.com
.
To clear the cache manually, use the --clear-cache
flag:
./caching-proxy --clear-cache
This command clears the cache and exits.
The cache time-to-live (TTL) is set to 1 minute. You can modify the cacheTTL
constant in the code to adjust this duration:
const cacheTTL = 1 * time.Minute
The interval for cleaning up expired cache entries is set to 30 seconds. You can modify the CleanupInterval
constant in the code to adjust this duration:
const CleanupInterval = 30 * time.Second
The NewProxy
function initializes a new ProxyObject
with the provided origin URL:
func NewProxy(origin string) *ProxyObject {
return &ProxyObject{
Origin: origin,
Cache: make(map[string]*CacheObject),
}
}
The ServeHTTP
method handles incoming HTTP requests, checks the cache for a response, and forwards the request to the origin server if necessary:
func (p *ProxyObject) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Cache lookup, forward request, and caching logic
}
The StartCacheCleanUp
method periodically removes expired cache entries:
func (p *ProxyObject) StartCacheCleanUp() {
go func() {
for {
time.Sleep(CleanupInterval)
p.Mutex.Lock()
for key, cacheObj := range p.Cache {
if time.Since(cacheObj.Created) > cacheTTL {
delete(p.Cache, key)
}
}
p.Mutex.Unlock()
}
}()
}
The server logs important events:
- Cache HIT: Logs when a response is served from the cache.
- Cache MISS: Logs when a request is forwarded to the origin server.
- Cache Expiration: Logs when an expired cache entry is removed.
[2025-01-09T10:00:00Z] Cache MISS for key: GET:/path
[2025-01-09T10:01:00Z] Cache HIT for key: GET:/path
[2025-01-09T10:02:00Z] Cache Expired for key: GET:/path
[2025-01-09T10:02:30Z] Cleaning expired cache for key: GET:/path
Feel free to submit issues or pull requests for enhancements and bug fixes. Contributions are welcome!
This project is licensed under the MIT License. See the LICENSE file for details.