# `Malla.Registry`
[🔗](https://github.com/netkubes/malla/blob/main/lib/malla/registry.ex#L21)

Provides process registration utilities.

This module offers a simple way to register and look up processes by name,
supporting both unique and duplicate name registrations. It is useful for
managing worker processes within a service.

See the [Storage and State guide](guides/10-storage.md) for more details.

# `count`

```elixir
@spec count(term(), atom() | tuple(), list()) :: integer()
```

Counts all processes matching a pattern and guards.

Returns the number of processes registered under the given key that match
the provided pattern and guards.

## Examples

    Malla.Registry.count(:my_key)
    #=> 2

    Malla.Registry.count(:my_key, {:_, :specific_value})
    #=> 1

# `lookup`

```elixir
@spec lookup(term()) :: {pid(), term()} | nil
```

Finds the process and value previously registered with `register/2`.

Returns a tuple of `{pid, value}` if found, or `nil` otherwise.

## Examples

    Malla.Registry.lookup(:my_key)
    #=> {#PID<0.123.0>, %{some: :data}}

    Malla.Registry.lookup(:unknown_key)
    #=> nil

# `register`

```elixir
@spec register(term(), term()) ::
  {:ok, pid()} | {:error, {:already_registered, pid()}}
```

Registers the current process with a key and optional value.

Uses `Malla.ProcRegistry` which does not allow duplicate registrations.
If a process is already registered with the given key, returns an error.

## Examples

    Malla.Registry.register(:my_key)
    #=> {:ok, #PID<0.123.0>}

    Malla.Registry.register(:my_key, %{some: :data})
    #=> {:error, {:already_registered, #PID<0.123.0>}}

# `store`

```elixir
@spec store(term(), term()) :: {:ok, pid()}
```

Registers the current process with a key and optional value.

Uses `Malla.ProcStore` which allows duplicate registrations.
Multiple processes can register with the same key.

## Examples

    Malla.Registry.store(:my_key)
    #=> {:ok, #PID<0.123.0>}

    Malla.Registry.store(:my_key, %{some: :data})
    #=> {:ok, #PID<0.124.0>}

# `unstore`

```elixir
@spec unstore(term()) :: :ok
```

Unregisters the current process from a key in `Malla.ProcStore`.

## Examples

    Malla.Registry.unstore(:my_key)
    #=> :ok

# `values`

```elixir
@spec values(term()) :: [{pid(), term()}]
```

Finds all processes and values previously registered with `store/2`.

Returns a list of `{pid, value}` tuples for all processes registered under the given key.

## Examples

    Malla.Registry.values(:my_key)
    #=> [{#PID<0.123.0>, :value1}, {#PID<0.124.0>, :value2}]

# `values`

```elixir
@spec values(term(), atom() | tuple(), list()) :: [{pid(), term()}]
```

Finds all processes and values matching a pattern and guards.

Allows pattern matching on the stored values using Erlang match specifications.

## Examples

    Malla.Registry.values(:my_key, {:_, :_, :"$1"})
    #=> [{#PID<0.123.0>, :value1}]

# `via`

Helper utility to register servers with arbitrary names.

Returns a via tuple that can be used with GenServer and other OTP behaviors
for process registration. Uses `Malla.ProcRegistry` which does not allow duplicates.

## Example

    GenServer.start_link(__MODULE__, [], name: Malla.Registry.via("my_name"))

# `whereis`

```elixir
@spec whereis(term()) :: pid() | nil
```

Finds a process previously registered with `register/2` or `via/1`.

Returns the PID if found, or `nil` otherwise.

## Examples

    Malla.Registry.whereis(:my_key)
    #=> #PID<0.123.0>

    Malla.Registry.whereis(:unknown_key)
    #=> nil

---

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