Aurinko.Cache (Aurinko v0.2.1)

Copy Markdown View Source

ETS-backed in-memory cache with per-entry TTL and LRU eviction.

Used transparently by the HTTP client for cacheable GET requests. Cache is keyed by {token_hash, path, params} so different accounts never share cached data.

Configuration

config :aurinko,
  cache_enabled: true,
  cache_ttl: 60_000,          # ms; default 60 seconds
  cache_max_size: 5_000,      # max entries before LRU eviction
  cache_cleanup_interval: 30_000   # ms between sweep runs

Usage

Aurinko.Cache.get("my_key")         # => nil | {:ok, value}
Aurinko.Cache.put("my_key", value)
Aurinko.Cache.put("my_key", value, ttl: 5_000)
Aurinko.Cache.delete("my_key")
Aurinko.Cache.invalidate_token(token)   # purge all entries for an account
Aurinko.Cache.stats()

Summary

Functions

Build a deterministic cache key from token + path + params.

Returns a specification to start this module under a supervisor.

Delete a single cache entry.

Clear the entire cache.

Retrieve a value from the cache.

Invalidate all cache entries associated with a specific token.

Store a value in the cache.

Return cache hit/miss/eviction statistics.

Types

cache_key()

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

stats()

@type stats() :: %{
  hits: non_neg_integer(),
  misses: non_neg_integer(),
  evictions: non_neg_integer(),
  size: non_neg_integer()
}

ttl_ms()

@type ttl_ms() :: pos_integer()

Functions

build_key(token, path, params \\ [])

@spec build_key(String.t(), String.t(), keyword() | map()) :: cache_key()

Build a deterministic cache key from token + path + params.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

delete(key)

@spec delete(cache_key()) :: :ok

Delete a single cache entry.

flush()

@spec flush() :: :ok

Clear the entire cache.

get(key)

@spec get(cache_key()) :: {:ok, term()} | nil

Retrieve a value from the cache.

Returns {:ok, value} on a hit, nil on a miss or expired entry.

invalidate_token(token)

@spec invalidate_token(String.t()) :: :ok

Invalidate all cache entries associated with a specific token.

Call this after a token is refreshed or revoked.

put(key, value, opts \\ [])

@spec put(cache_key(), term(), keyword()) :: :ok

Store a value in the cache.

Options

  • :ttl — Time-to-live in milliseconds. Defaults to configured cache_ttl.

start_link(opts \\ [])

stats()

@spec stats() :: stats()

Return cache hit/miss/eviction statistics.