Jido.Agent.Store behaviour (Jido v2.0.0-rc.1)

View Source

Behaviour for agent state persistence.

Implement this behaviour to create custom storage backends for agent hibernate/thaw functionality. The store is used by Jido.Agent.InstanceManager to persist agent state when idle and restore it on demand.

Built-in Adapters

Example Implementation

defmodule MyApp.RedisStore do
  @behaviour Jido.Agent.Store

  @impl true
  def get(key, opts) do
    case Redix.command(:redix, ["GET", serialize_key(key)]) do
      {:ok, nil} -> :not_found
      {:ok, data} -> {:ok, :erlang.binary_to_term(data)}
      {:error, reason} -> {:error, reason}
    end
  end

  @impl true
  def put(key, dump, opts) do
    ttl = Keyword.get(opts, :ttl, 3600)
    data = :erlang.term_to_binary(dump)
    case Redix.command(:redix, ["SETEX", serialize_key(key), ttl, data]) do
      {:ok, "OK"} -> :ok
      {:error, reason} -> {:error, reason}
    end
  end

  @impl true
  def delete(key, opts) do
    case Redix.command(:redix, ["DEL", serialize_key(key)]) do
      {:ok, _} -> :ok
      {:error, reason} -> {:error, reason}
    end
  end

  defp serialize_key({module, id}), do: "agent:#{module}:#{id}"
end

Summary

Callbacks

Deletes persisted agent state by key.

Retrieves a persisted agent state by key.

Persists agent state under the given key.

Types

dump()

@type dump() :: term()

key()

@type key() :: term()

opts()

@type opts() :: keyword()

Callbacks

delete(key, opts)

@callback delete(key(), opts()) :: :ok | {:error, term()}

Deletes persisted agent state by key.

Returns :ok on success (including if key didn't exist) or {:error, reason} on failure.

get(key, opts)

@callback get(key(), opts()) :: {:ok, dump()} | :not_found | {:error, term()}

Retrieves a persisted agent state by key.

Returns {:ok, dump} if found, :not_found if not present, or {:error, reason} on failure.

put(key, dump, opts)

@callback put(key(), dump(), opts()) :: :ok | {:error, term()}

Persists agent state under the given key.

Returns :ok on success or {:error, reason} on failure.