Chuyển tới nội dung chính

Caching Overview

PreviewProxy uses a two-level cache to avoid redundant fetches and transforms. Once an image has been fetched and processed, subsequent requests for the same image and parameters are served from cache.

Two-level cache architecture

L1: In-memory cache

The L1 cache stores transformed images in RAM using Moka, a high-performance concurrent cache library. It is the fastest cache level - a cache hit here avoids both disk I/O and recomputation.

The L1 cache is lost when the process restarts.

L2: Disk cache

The L2 cache stores transformed images on disk. It is slower than L1 but survives restarts, making it suitable as a durable cache layer.

Cache key

The cache key is derived from the combination of:

  • Source image URL - the full upstream URL
  • Transform parameters - all request parameters except sig

Two requests with the same URL and parameters always resolve to the same cache key, regardless of parameter order.

Request flow

Inflight deduplication

If multiple requests for the same image and parameters arrive simultaneously (before the first request has finished), PreviewProxy ensures that only one fetch and transform is performed. All other concurrent requests wait for the result and then receive it. This prevents thundering herd problems under high concurrency.

Configuration reference

L1 (memory) settings

VariableDefaultDescription
CACHE_MEMORY_MAX_MB256Maximum size of the in-memory cache in megabytes
CACHE_MEMORY_TTL_SECS3600How long entries remain in the memory cache (seconds)

L2 (disk) settings

VariableDefaultDescription
CACHE_DIR/tmp/previewproxyDirectory where cached files are written
CACHE_DISK_TTL_SECS86400How long entries remain on disk before expiry (seconds)
CACHE_DISK_MAX_MBunlimitedMaximum total size of the disk cache in megabytes

Maintenance

VariableDefaultDescription
CACHE_CLEANUP_INTERVAL_SECS600How often the background job scans for and removes expired disk cache entries (seconds)
thông tin

CACHE_DISK_MAX_MB is not set by default, meaning disk usage is unbounded. In production it is strongly recommended to set this to prevent disk exhaustion. See Cache Tuning for guidance.