ImagePipe.Cache.FileSystem (image_pipe v0.2.0)

Copy Markdown View Source

Stores processed images, or originals, as files on local disk. The cache and input_cache options take its options, and need 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 stored bodies: 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 processed-image and originals caches, see Caching processed images.

Options

  • :root (absolute path (String.t/0)) - Required. Directory that holds the cache files. Must be an absolute path. The cache and input_cache roots must be separate, neither inside the other. Plug initialization fails otherwise.

  • :pool (:input or :output) - Tags the cache's telemetry events with pool, so events from an originals cache can be told apart from the processed-image cache. Defaults to :output, or :input for a cache configured as :input_cache.

  • :max_size_bytes (pos_integer/0) - Required. Cap, in bytes, on the total size of stored bodies. 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 a twentieth of the cap, or 256 MiB, is never stored.

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.