SuperCache.Struct (SuperCache v1.3.0)

Copy Markdown View Source

In-memory struct store backed by SuperCache ETS partitions.

Works transparently in both local and distributed modes — the mode is determined by the :cluster option passed to SuperCache.start!/1.

Call init/2 once per struct type before using add/1, get/1, etc.

Read modes (distributed)

Pass read_mode: :primary or read_mode: :quorum for stronger consistency.

Example

alias SuperCache.Struct, as: S

defmodule Order do
  defstruct [:id, :customer, :status]
end

S.init(%Order{}, :id)
S.add(%Order{id: "o-1", customer: "Alice", status: :pending})
S.get(%Order{id: "o-1"})
# => {:ok, %Order{id: "o-1", customer: "Alice", status: :pending}}

Summary

Functions

add(struct)

@spec add(map()) :: {:ok, map()} | {:error, any()}

get(struct, opts \\ [])

@spec get(
  map(),
  keyword()
) :: {:ok, map()} | {:error, :not_found | any()}

get_all(struct, opts \\ [])

@spec get_all(
  map(),
  keyword()
) :: {:ok, list()} | {:error, any()}

init(struct, key \\ :id)

@spec init(map(), atom()) :: true | {:error, any()}

remove(struct)

@spec remove(map()) :: {:ok, map()} | {:error, any()}

remove_all(struct)

@spec remove_all(map()) :: {:ok, :removed} | {:error, any()}