GameServer.KV (GameServer v1.0.509)
Generic key/value storage.
This is intentionally minimal and un-opinionated.
If you want namespacing, encode it in key (e.g. "polyglot_pirates:key1").
If you want per-user values, pass user_id: ... to get/2, put/4, and delete/2.
This module uses the app cache (GameServer.Cache) as a best-effort read cache.
Writes update the cache and deletes evict it.
Summary
Types
Attributes used when creating or updating entries.
Options accepted by list_entries/1 and count_entries/1.
Metadata stored alongside a value. Typically a small map with auxiliary fields.
Value stored for a key. This is an arbitrary map and should contain JSON-serializable data.
Functions
Count the number of entries that match the optional filter.
Create a new Entry from attrs (expecting key, optional user_id, value, metadata).
Returns {:ok, entry} or {:error, changeset}.
Delete the entry at key.
Delete an entry by its id.
Retrieve the value and metadata stored for key.
Fetch an Entry by its numeric id.
Returns the Entry struct or nil if not found.
List key/value entries with optional pagination and filtering.
Store value with optional metadata at key.
Update an existing entry by id with attrs.
Returns {:ok, entry}, {:error, :not_found} if missing, or {:error, changeset} on validation error.
Types
@type attrs() :: %{ :key => String.t(), optional(:user_id) => pos_integer(), :value => value(), optional(:metadata) => metadata() }
Attributes used when creating or updating entries.
Expected keys (atom keys recommended):
:key— the entry key (String.t()):user_id— optional user id (pos_integer()):value— the stored value (value()):metadata— optional metadata (metadata())
@type list_opts() :: [ page: pos_integer(), page_size: pos_integer(), user_id: pos_integer(), global_only: boolean(), key: String.t() ]
Options accepted by list_entries/1 and count_entries/1.
Keys (all optional):
:page— page number (pos_integer(), defaults to1):page_size— page size (pos_integer(), defaults to50):user_id— filter by user id (pos_integer()):global_only— when true, only return global entries (whereuser_idisnil) (boolean()):key— substring filter (String.t())
@type metadata() :: map()
Metadata stored alongside a value. Typically a small map with auxiliary fields.
@type value() :: map()
Value stored for a key. This is an arbitrary map and should contain JSON-serializable data.
Functions
@spec count_entries(list_opts()) :: non_neg_integer()
Count the number of entries that match the optional filter.
Accepts the same options as list_entries/1 (see list_opts/0). Returns a non-negative integer.
@spec create_entry(attrs()) :: {:ok, GameServer.KV.Entry.t()} | {:error, Ecto.Changeset.t()}
Create a new Entry from attrs (expecting key, optional user_id, value, metadata).
Returns {:ok, entry} or {:error, changeset}.
Delete the entry at key.
Pass user_id: id in opts to delete a per-user key. Returns :ok.
@spec delete_entry(pos_integer()) :: :ok
Delete an entry by its id.
Returns :ok whether or not the entry existed.
Retrieve the value and metadata stored for key.
Pass user_id: id in opts to scope the lookup to a specific user.
Returns {:ok, %{value: map(), metadata: map()}} when found, or :error when not present.
@spec get_entry(pos_integer()) :: GameServer.KV.Entry.t() | nil
Fetch an Entry by its numeric id.
Returns the Entry struct or nil if not found.
@spec list_entries(list_opts()) :: [GameServer.KV.Entry.t()]
List key/value entries with optional pagination and filtering.
Supported options: :page, :page_size, :user_id, :global_only, and :key (substring filter).
See list_opts/0 for the expected option types.
Returns a list of Entry structs ordered by most recently updated.
@spec put(String.t(), value(), metadata()) :: {:ok, GameServer.KV.Entry.t()} | {:error, Ecto.Changeset.t()}
@spec put(String.t(), value(), metadata(), list_opts()) :: {:ok, GameServer.KV.Entry.t()} | {:error, Ecto.Changeset.t()}
Store value with optional metadata at key.
When using the 4-arity, supported options include user_id: id to scope the entry to a user.
Returns {:ok, entry} on success or {:error, changeset} on validation failure.
@spec update_entry(pos_integer(), attrs()) :: {:ok, GameServer.KV.Entry.t()} | {:error, :not_found} | {:error, Ecto.Changeset.t()}
Update an existing entry by id with attrs.
Returns {:ok, entry}, {:error, :not_found} if missing, or {:error, changeset} on validation error.