ipregistry_cache (ipregistry v1.0.0)

View Source

Optional in-memory cache for Ipregistry lookups, with time-based expiration and a bounded size using insertion-order (FIFO) eviction.

Start one under your supervision tree with child_spec/1 or standalone with start_link/1, then reference its name in the cache option of ipregistry:new/2:

  {ok, _} = ipregistry_cache:start_link(#{name => my_ip_cache, ttl => 600000}),
  Client = ipregistry:new(<<"YOUR_API_KEY">>, #{cache => my_ip_cache}).

Reads are served directly from a protected ETS table owned by the cache process, so cache hits never block on the server. Writes go through the server and are best-effort: if the cache process is down, lookups keep working without caching rather than failing.

Summary

Types

A cache identified by its registered name (preferred) or its pid. Direct ETS reads require the registered name; when a pid is used, reads go through the server process.

name registers the cache process and its ETS table (default ipregistry_cache). ttl is how long entries stay valid, in milliseconds (default 10 minutes). max_size bounds the number of entries (default 4096); when full, the oldest entry is evicted. sweep_interval controls how often expired entries are purged, in milliseconds (default 1 minute).

Functions

Returns a supervisor child spec for embedding the cache in your supervision tree.

Returns the cached value for Key when present and unexpired.

Removes the entry for Key, if present.

Removes every entry.

Stores Value under Key, refreshing its expiration. Best-effort: returns ok even when the cache process is unavailable.

Returns the current number of entries, including expired entries not yet swept. Primarily useful in tests.

Equivalent to start_link(#{}).

Starts a cache registered under the configured name and links it to the calling process.

Stops the cache process.

Types

cache/0

-type cache() :: atom() | pid().

A cache identified by its registered name (preferred) or its pid. Direct ETS reads require the registered name; when a pid is used, reads go through the server process.

options/0

-type options() ::
          #{name => atom(),
            max_size => pos_integer(),
            ttl => pos_integer(),
            sweep_interval => pos_integer()}.

name registers the cache process and its ETS table (default ipregistry_cache). ttl is how long entries stay valid, in milliseconds (default 10 minutes). max_size bounds the number of entries (default 4096); when full, the oldest entry is evicted. sweep_interval controls how often expired entries are purged, in milliseconds (default 1 minute).

Functions

child_spec(Options)

-spec child_spec(options()) -> supervisor:child_spec().

Returns a supervisor child spec for embedding the cache in your supervision tree.

get(Cache, Key)

-spec get(cache(), term()) -> {ok, term()} | miss.

Returns the cached value for Key when present and unexpired.

invalidate(Cache, Key)

-spec invalidate(cache(), term()) -> ok.

Removes the entry for Key, if present.

invalidate_all(Cache)

-spec invalidate_all(cache()) -> ok.

Removes every entry.

put(Cache, Key, Value)

-spec put(cache(), term(), term()) -> ok.

Stores Value under Key, refreshing its expiration. Best-effort: returns ok even when the cache process is unavailable.

size(Cache)

-spec size(cache()) -> non_neg_integer().

Returns the current number of entries, including expired entries not yet swept. Primarily useful in tests.

start_link()

-spec start_link() -> {ok, pid()} | {error, term()}.

Equivalent to start_link(#{}).

Starts a cache with default options.

start_link(Options)

-spec start_link(options()) -> {ok, pid()} | {error, term()}.

Starts a cache registered under the configured name and links it to the calling process.

stop(Cache)

-spec stop(cache()) -> ok.

Stops the cache process.