Nebulex.Adapters.Mnesia.Entry (nebulex_mnesia_adapter v2.6.5)

View Source

This module defines a struct that represents a cache entry in the Mnesia adapter for Nebulex. It includes fields for the table name, key, value, last touched timestamp, and time-to-live (TTL) information.

Summary

Functions

Returns the key of the cache entry.

Returns the remaining TTL of the cache entry.

Returns the status of the cache entry.

Returns the TTL (time-to-live) of the cache entry.

Returns the value of the cache entry.

Types

t()

@type t() ::
  {table :: atom(), key :: any(), value :: any(), touched :: integer(),
   ttl :: non_neg_integer() | :infinity}

Functions

key(arg)

@spec key(entry :: t()) :: any()

Returns the key of the cache entry.

Parameters

  • entry: A tuple representing the cache entry.

Examples

iex> entry = {:table, :key, "value", 1759420681791, 3600}
iex> Nebulex.Adapters.Mnesia.Entry.key(entry)
:key

remaining_ttl(arg)

@spec remaining_ttl(entry :: t()) :: integer() | :infinity

Returns the remaining TTL of the cache entry.

Parameters

  • entry: A tuple representing the cache entry.

Returns

  • The remaining TTL in seconds, or :infinity if the TTL is infinite.
  • Calculates remaining TTL as ttl - (current_time - touched).

Examples

iex> entry = {:table, :key, "value", 1759420681791, 3600}
iex> Nebulex.Adapters.Mnesia.Entry.remaining_ttl(entry)
3595 # (assuming 5 seconds have passed since touched time)

iex> entry = {:table, :key, "value", 1759420681791, :infinity}
iex> Nebulex.Adapters.Mnesia.Entry.remaining_ttl(entry)
:infinity

status(entry)

@spec status(entry :: t()) :: {:ok, :active} | {:error, :expired}

Returns the status of the cache entry.

Parameters

  • entry: A tuple representing the cache entry.

Returns

  • {:ok, :active} if the entry is valid.
  • {:error, :expired} if the entry has expired.

Examples

iex> entry = {:table, :key, "value", 1759420681791, 3600}
iex> Nebulex.Adapters.Mnesia.Entry.status(entry)
{:ok, :active}

iex> entry = {:table, :key, "value", 1759420681791, 1}
iex> :timer.sleep(2000) # wait for 2 seconds
iex> Nebulex.Adapters.Mnesia.Entry.status(entry)
{:error, :expired}

ttl(arg)

@spec ttl(entry :: t()) :: non_neg_integer() | :infinity

Returns the TTL (time-to-live) of the cache entry.

Parameters

  • entry: A tuple representing the cache entry.

Examples

iex> entry = {:table, :key, "value", 1759420681791, 3600}
iex> Nebulex.Adapters.Mnesia.Entry.ttl(entry)
3600

value(arg)

@spec value(entry :: t()) :: any()

Returns the value of the cache entry.

Parameters

  • entry: A tuple representing the cache entry.

Examples

iex> entry = {:table, :key, "value", 1759420681791, 3600}
iex> Nebulex.Adapters.Mnesia.Entry.value(entry)
"value"