Skip to main content

A reverse proxy with redis cahing

Project description

CacheProxy

CacheProxy is a simple reverse proxy that caches HTTP GET responses in Redis. The goal of the project is to reduce response times for repeated requests while keeping the proxy transparent to the client.

I built this project to learn how reverse proxies work, how HTTP caching is implemented, and how Redis can be used to improve API performance.


Features

  • Caches successful GET requests in Redis
  • Adds Cache: HIT and Cache: MISS headers to every response
  • Stores binary responses safely using Base64 encoding
  • /refresh endpoint to clear the cache
  • Configurable through CLI arguments and environment variables
  • Handles response headers correctly to avoid browser decoding issues

Installation

pip install cacheproxy-byiambaka-v2

Requirements

  • Python 3.10+
  • Redis

By default CacheProxy looks for Redis on:

Host : localhost
Port : 6379
DB   : 0

If your Redis server is running somewhere else, configure it using environment variables.

Linux / macOS

export REDIS_HOST=127.0.0.1
export REDIS_PORT=6379
export REDIS_DB=0

Windows

set REDIS_HOST=127.0.0.1
set REDIS_PORT=6379
set REDIS_DB=0

Running the Proxy

Start the proxy by providing the origin server.

cacheproxy --origin https://dummyjson.com --port 3000

The proxy will start on

http://localhost:3000

Any request sent to the proxy will be forwarded to the origin server.


Example

Request

GET http://localhost:3000/products/1

First request

Cache: MISS

The proxy fetches the response from the origin server and stores it in Redis.

Second request

Cache: HIT

This time the response is served directly from Redis.


Clearing the Cache

To clear all cached responses:

curl -X POST http://localhost:3000/refresh

Response:

{
  "message": "cache cleared"
}

Performance

I measured the proxy using 500 requests against the DummyJSON API.

Request Average Time
Cache MISS 244 ms
Cache HIT 1.09 ms

Repeated requests were approximately 223× faster when served from Redis.


Tech Stack

  • Python
  • FastAPI
  • Typer
  • HTTPX
  • Redis
  • Uvicorn
  • Pydantic

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cacheproxy_byiambaka_v2-0.1.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cacheproxy_byiambaka_v2-0.1.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file cacheproxy_byiambaka_v2-0.1.1.tar.gz.

File metadata

  • Download URL: cacheproxy_byiambaka_v2-0.1.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for cacheproxy_byiambaka_v2-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f66c395d29253e1fe234d8cc20b13e097a94d812c599f12bf7fbbb91099f3527
MD5 785b3ed8660dfcbdbc7a3bd420e944ac
BLAKE2b-256 db57787b4e1f47e6fe53145187005044679aa5410b34652e53c96aaf64fd2dca

See more details on using hashes here.

File details

Details for the file cacheproxy_byiambaka_v2-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for cacheproxy_byiambaka_v2-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4341d89db4ac9c6ffedddd3c3eb80220e7e5c2ddbb7b9724f4b1202f1cd1902d
MD5 a0e978af10abc8fd12263408f4f79fb7
BLAKE2b-256 7f0706bae1dd84af35dd307efaacc8b609e5b5d4d7305d20553da92842bea87c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page