ImagePipe.Cache.FileSystem (image_pipe v0.5.0)

Copy Markdown View Source

Stores processed images as files on local disk. The cache option takes its options, and needs an instance, which starts the processes that track the cache's size (see ImagePipe.child_spec/1):

children = [
  {ImagePipe,
   name: MyApp.Images,
   sources: sources,
   cache: [root: "/var/cache/image_pipe/processed", max_size_bytes: 5_000_000_000]},
  MyAppWeb.Endpoint
]

:max_size_bytes caps the disk the stored entries take: a write evicts entries to make room before it's stored, or is rejected. The cache keeps the entries that are requested often and took the most work to make for their size.

To set up the cache, see Caching processed images.

Options

  • :root (absolute path (String.t/0)) - Required. Directory that holds the cache files. Must be an absolute path. An instance whose root another instance in the VM already uses raises ArgumentError when it starts.

  • :max_size_bytes (pos_integer/0) - Required. Cap, in bytes, on the disk the stored entries take. Each entry counts its body rounded up to whole 4 KiB blocks, plus a block for its metadata file. Writes evict the entries least worth keeping to stay under it: those requested least often and quickest to make for their size. An entry larger than 256 MiB, or a twentieth of the cap when that is smaller, is stored only when no other write is in progress.

  • :max_remembered_entries (pos_integer/0) - How many recently served entries the cache keeps in memory, so a repeated hit reads only the body file. Defaults to one for each 50 KB of max_size_bytes, at least 1,000 and at most 50,000.

The processed-image cache also accepts :max_body_bytes, a non-negative integer or nil. Responses larger than :max_body_bytes are delivered but not stored. The default nil stores responses of any size.

Summary

Functions

Reads a cached response and materializes its complete body as a binary.

Functions

get(key, opts)

Reads a cached response and materializes its complete body as a binary.

Returns {:hit, entry}, :miss, or {:error, reason}. The cached file's size is checked before reading, and its descriptor is closed before returning.