Skip to content

ajayv26/cache-proxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

# 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.


Features

  • 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.

Installation

  1. Install Go on your system.

  2. Clone this repository:

    git clone <repository-url>
    cd <repository-name>
  3. Build the project:

    go build -o caching-proxy .

Usage

Starting the Server

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.

Clearing the Cache

To clear the cache manually, use the --clear-cache flag:

./caching-proxy --clear-cache

This command clears the cache and exits.


Configuration

Cache TTL

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

Cleanup Interval

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

Code Overview

Proxy Initialization

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),
    }
}

Request Handling

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
}

Cache Cleanup

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()
        }
    }()
}

Logs

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.

Example Log Output

[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

Contributing

Feel free to submit issues or pull requests for enhancements and bug fixes. Contributions are welcome!


License

This project is licensed under the MIT License. See the LICENSE file for details.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages