Skip to content

feat: add persistent on-disk HTTP response cache#61

Open
mkarim1378 wants to merge 1 commit into
masterking32:python_testingfrom
mkarim1378:feature/response-disk-cache
Open

feat: add persistent on-disk HTTP response cache#61
mkarim1378 wants to merge 1 commit into
masterking32:python_testingfrom
mkarim1378:feature/response-disk-cache

Conversation

@mkarim1378
Copy link
Copy Markdown

Adds a two-tier caching system: the existing in-memory ResponseCache (L1, fast, lost on restart) is now backed by a new on-disk DiskCache (L2, persistent, survives restarts).

How it works:

  • Cache lookup: check memory first; on miss check disk; on disk hit promote the entry back into memory for future requests.
  • Cache write: write to both memory and disk simultaneously.
  • Each disk entry is a single binary file: 8-byte expiry timestamp (little-endian double) followed by the raw HTTP response bytes. Atomic rename guarantees no partial writes.
  • Eviction: expired files removed on every put(); oldest-first deletion enforces the max_mb size cap.
  • Existing TTL and cacheability logic (Cache-Control, no-store, Set-Cookie, static-asset heuristics) is reused unchanged.

New files:

  • src/core/disk_cache.py: DiskCache class (get/put/evict/stats)

Changed files:

  • src/proxy/proxy_server.py: initialise DiskCache from config; L2 lookup and write at both HTTPS-MITM and plain-HTTP paths
  • config.example.json: disk_cache section (enabled, max_mb, dir)
  • .gitignore: exclude http_cache/ directory

Config (off by default):
"disk_cache": { "enabled": true, "max_mb": 200, "dir": "" }

Adds a two-tier caching system: the existing in-memory ResponseCache
(L1, fast, lost on restart) is now backed by a new on-disk DiskCache
(L2, persistent, survives restarts).

How it works:
- Cache lookup: check memory first; on miss check disk; on disk hit
  promote the entry back into memory for future requests.
- Cache write: write to both memory and disk simultaneously.
- Each disk entry is a single binary file: 8-byte expiry timestamp
  (little-endian double) followed by the raw HTTP response bytes.
  Atomic rename guarantees no partial writes.
- Eviction: expired files removed on every put(); oldest-first
  deletion enforces the max_mb size cap.
- Existing TTL and cacheability logic (Cache-Control, no-store,
  Set-Cookie, static-asset heuristics) is reused unchanged.

New files:
- src/core/disk_cache.py: DiskCache class (get/put/evict/stats)

Changed files:
- src/proxy/proxy_server.py: initialise DiskCache from config;
  L2 lookup and write at both HTTPS-MITM and plain-HTTP paths
- config.example.json: disk_cache section (enabled, max_mb, dir)
- .gitignore: exclude http_cache/ directory

Config (off by default):
  "disk_cache": { "enabled": true, "max_mb": 200, "dir": "" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant