# `Nebulex.Adapters.Local.Generation`
[🔗](https://github.com/elixir-nebulex/nebulex_local/blob/v3.1.0/lib/nebulex/adapters/local/generation.ex#L1)

This module implements the generation manager and garbage collector.

The generation manager creates cache generations and schedules the deletion
of retired generation tables. The adapter stores new entries in the newer
generation and promotes entries from the older generation when they are
fetched.

Generations rotate on the configured `:gc_interval`, when a size or memory
check reaches its threshold, or when a caller explicitly requests a new
generation.

When a new generation is created, the oldest one is removed from the active
generations at once, so new operations no longer see it. Its tables are
deleted after `:gc_cleanup_delay`, which gives operations that already hold
a reference to them time to finish.

# `opts`

```elixir
@type opts() :: Nebulex.Cache.opts()
```

# `server_ref`

```elixir
@type server_ref() :: pid() | atom() | :ets.tid()
```

# `t`

```elixir
@type t() :: %Nebulex.Adapters.Local.Generation{
  allocated_memory: term(),
  backend: term(),
  backend_opts: term(),
  cache: term(),
  gc_cleanup_delay: term(),
  gc_healthcheck_ref: term(),
  gc_heartbeat_ref: term(),
  gc_interval: term(),
  gc_memory_check_interval: term(),
  max_size: term(),
  meta_tab: term(),
  name: term(),
  stats_counter: term(),
  telemetry: term(),
  telemetry_prefix: term()
}
```

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `delete_all`

```elixir
@spec delete_all(server_ref()) :: :ok
```

Removes or flushes all entries from the cache (including all its generations).

## Example

    Nebulex.Adapters.Local.Generation.delete_all(MyCache)

# `get_state`

```elixir
@spec get_state(server_ref()) :: t()
```

A convenience function for retrieving the state.

# `list`

```elixir
@spec list(server_ref()) :: [:ets.tid()]
```

Returns the list of the generations in the form `[newer, older]`.

## Example

    Nebulex.Adapters.Local.Generation.list(MyCache)

# `memory_info`

```elixir
@spec memory_info(server_ref()) ::
  {used_mem :: non_neg_integer(), total_mem :: non_neg_integer()}
```

Returns the memory info in a tuple form `{used_mem, total_mem}`.

## Example

    Nebulex.Adapters.Local.Generation.memory_info(MyCache)

# `new`

```elixir
@spec new(server_ref(), opts()) :: :ok
```

Creates a new cache generation. Once the max number of generations
is reached, when a new generation is created, the oldest one is
deleted.

## Options

* `:gc_interval_reset` (`t:boolean/0`) - Whether the `:gc_interval` should be reset or not. The default value is `true`.

## Example

    Nebulex.Adapters.Local.Generation.new(MyCache)

    Nebulex.Adapters.Local.Generation.new(MyCache, gc_interval_reset: false)

# `newer`

```elixir
@spec newer(server_ref()) :: :ets.tid()
```

Returns the newer generation.

## Example

    Nebulex.Adapters.Local.Generation.newer(MyCache)

# `realloc`

```elixir
@spec realloc(server_ref(), pos_integer()) :: :ok
```

Reallocates the block of memory that was previously allocated for the given
`server_ref` with the new `size`. In other words, reallocates the max memory
size for a cache generation.

## Example

    Nebulex.Adapters.Local.Generation.realloc(MyCache, 1_000_000)

# `reset_gc_interval`

```elixir
@spec reset_gc_interval(server_ref()) :: :ok
```

Resets the GC interval.

## Example

    Nebulex.Adapters.Local.Generation.reset_gc_interval(MyCache)

# `server`

```elixir
@spec server(server_ref()) :: pid()
```

Returns the PID of the GC server for the given `server_ref`.

## Example

    Nebulex.Adapters.Local.Generation.server(MyCache)

# `start_link`

```elixir
@spec start_link(opts()) :: GenServer.on_start()
```

Starts the garbage collector for the built-in local cache adapter.

---

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