Exoplanet.Cache behaviour (Exoplanet v0.5.0)

Copy Markdown View Source

Behaviour for feed HTTP conditional-GET cache adapters.

Implementations are responsible for their own configuration. No path or directory is threaded through the interface — each adapter reads what it needs from its own application environment.

Configure the active adapter in the consuming application:

config :exoplanet, cache_adapter: MyApp.FeedCache

Set to nil (or omit) to disable caching entirely.

Summary

Callbacks

Returns the cached entry for the given URL, or nil if absent.

Called when a fetch fails (non-200/304 response or connection error). status is the HTTP status code, or nil for connection-level failures. reason is a human-readable error string (e.g. "HTTP 404").

Called when a fetch succeeds (200 or 304). status is the HTTP status code.

Stores (or replaces) the cache entry for the given URL. Returns :ok.

Types

entry()

@type entry() :: %{
  etag: String.t() | nil,
  last_modified: String.t() | nil,
  body: String.t()
}

url()

@type url() :: String.t()

Callbacks

get(url)

@callback get(url()) :: entry() | nil

Returns the cached entry for the given URL, or nil if absent.

on_error(url, status, reason)

(optional)
@callback on_error(url(), status :: integer() | nil, reason :: String.t()) :: :ok

Called when a fetch fails (non-200/304 response or connection error). status is the HTTP status code, or nil for connection-level failures. reason is a human-readable error string (e.g. "HTTP 404").

on_success(url, status)

(optional)
@callback on_success(url(), status :: integer()) :: :ok

Called when a fetch succeeds (200 or 304). status is the HTTP status code.

put(url, entry)

@callback put(url(), entry()) :: :ok

Stores (or replaces) the cache entry for the given URL. Returns :ok.