Nous.Persistence behaviour (nous v0.13.3)
View SourceBehaviour for pluggable persistence backends.
Implement this behaviour to store and retrieve serialized agent contexts.
See Nous.Persistence.ETS for a reference implementation.
Example
defmodule MyApp.RedisPersistence do
@behaviour Nous.Persistence
@impl true
def save(session_id, data) do
Redix.command(:redix, ["SET", "nous:#{session_id}", JSON.encode!(data)])
:ok
end
@impl true
def load(session_id) do
case Redix.command(:redix, ["GET", "nous:#{session_id}"]) do
{:ok, nil} -> {:error, :not_found}
{:ok, json} -> {:ok, json |> JSON.decode!() |> Map.new(fn {k, v} -> {String.to_existing_atom(k), v} end)}
end
end
@impl true
def delete(session_id) do
Redix.command(:redix, ["DEL", "nous:#{session_id}"])
:ok
end
@impl true
def list do
{:ok, keys} = Redix.command(:redix, ["KEYS", "nous:*"])
{:ok, Enum.map(keys, &String.replace_prefix(&1, "nous:", ""))}
end
end
Summary
Callbacks
Delete persisted data for a session.
List all persisted session IDs.
Load serialized context data for a session.
Save serialized context data for a session.
Callbacks
Delete persisted data for a session.
List all persisted session IDs.
Load serialized context data for a session.
Save serialized context data for a session.