# `Nous.Persistence`
[🔗](https://github.com/nyo16/nous/blob/v0.17.1/lib/nous/persistence.ex#L1)

Behaviour 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

# `delete`

```elixir
@callback delete(session_id :: String.t()) :: :ok | {:error, term()}
```

Delete persisted data for a session.

# `list`

```elixir
@callback list() :: {:ok, [String.t()]} | {:error, term()}
```

List all persisted session IDs.

# `load`

```elixir
@callback load(session_id :: String.t()) ::
  {:ok, map()} | {:error, :not_found} | {:error, term()}
```

Load serialized context data for a session.

# `save`

```elixir
@callback save(session_id :: String.t(), data :: map()) :: :ok | {:error, term()}
```

Save serialized context data for a session.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
