Apero.Cache (Apero v1.0.0)

Copy Markdown View Source

Unified cache interface — ETS, Redis, Memcached.

Provides a consistent API for different cache backends via the Apero.Cache.Adapter behaviour.

Supported backends

  • Apero.Cache.ETS — in-memory ETS with TTL (built-in, no deps)
  • Apero.Cache.Redis — Redis via redix (optional dep)
  • Apero.Cache.Memcached — Memcached via memcache (optional dep)

Usage

# ETS (built-in)
{:ok, pid} = Apero.Cache.start_link(Apero.Cache.ETS, name: :my_cache)
Apero.Cache.put(:my_cache, "key", "value", ttl: 3600)
Apero.Cache.get(:my_cache, "key") # => {:ok, "value"}

# Redis
Apero.Cache.put(:redis_cache, "key", "value")

Summary

Functions

Deletes a key.

Fetches or computes a value (cache-aside pattern).

Clears all keys.

Retrieves a value. Returns {:ok, value} or {:error, :not_found}.

Returns true if the key exists.

Stores a value. Optional :ttl in seconds.

Returns the number of keys.

Starts a cache backend. Returns {:ok, pid}.

Types

cache_name()

@type cache_name() :: atom() | pid()

Functions

delete(cache, key)

@spec delete(cache_name(), term()) :: :ok | {:error, term()}

Deletes a key.

fetch(cache, key, fun, opts \\ [])

@spec fetch(cache_name(), term(), (-> term()), keyword()) ::
  {:ok, term()} | {:error, term()}

Fetches or computes a value (cache-aside pattern).

flush(cache)

@spec flush(cache_name()) :: :ok | {:error, term()}

Clears all keys.

get(cache, key)

@spec get(cache_name(), term()) :: {:ok, term()} | {:error, :not_found}

Retrieves a value. Returns {:ok, value} or {:error, :not_found}.

member?(cache, key)

@spec member?(cache_name(), term()) :: boolean()

Returns true if the key exists.

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

@spec put(cache_name(), term(), term(), keyword()) :: :ok | {:error, term()}

Stores a value. Optional :ttl in seconds.

size(cache)

@spec size(cache_name()) :: {:ok, non_neg_integer()} | {:error, term()}

Returns the number of keys.

start_link(adapter, opts \\ [])

@spec start_link(
  module(),
  keyword()
) :: GenServer.on_start()

Starts a cache backend. Returns {:ok, pid}.