http_cache_store_disk

http_cache_store_disk is an disk LRU cache that can be used as a backend for http_cache. It implements the http_cache_store behaviour.

It supports:

Under the hood, it simply saves the HTTP response body on disk as a file (other HTTP caches use other methods) and related libraries (such as plug_http_cache) use the sendfile system call when available. This enables sending files extremely fast because:

Stored responses are nuked as soon as a configurable disk space occupation threshold is reached. It does not support configuration of a fixed amount of bytes for disk usage, mainly because:

Support

OTP26+

Usage

This is an OTP application, and automatically starts.

Setting the right thresholds

Metadata about HTTP responses written on disk are stored in-memory. The overhead is about 1kb per stored response.

Therefore, 1 million stored responses will occupy around 1GB or memory.

When using this store as a backend for library that uses the sendfile system call, such as plug_http_cache, you should take into consideration that the kernel caches responses in memory. In other words, if caching metadata takes a huge amount of memory (say 99%), then you will not benefit from the automatic caching from the kernel and files will be read from the disk every time they are sent, resulting in slower sending operations.

Memory limit is set to 0.7 for this purpose. If you use very rapid disk (SSD), you might want to reconsider this default.

Configuration parameters

The following options can be modified at runtime:

Installation

Erlang (rebar3):

{deps, [{http_cache_store_disk, "~> 0.2.1"}]}.

Elixir:

{:http_cache_store_disk, "~> 0.2.1"}

Telemetry