View Source Nebulex.Entry (Nebulex v2.6.4)

Defines a Cache Entry.

This is the structure used by the caches for representing cache entries.

Summary

Types

t()

Defines a generic struct for a cache entry.

Functions

Decodes a previously encoded entry.

Encodes a cache entry.

Returns whether the given entry has expired or not.

Returns the remaining time-to-live.

Types

@type t() :: %Nebulex.Entry{
  key: any(),
  time_unit: System.time_unit(),
  touched: integer(),
  ttl: timeout(),
  value: any()
}

Defines a generic struct for a cache entry.

The entry depends on the adapter completely, this struct/type aims to define the common fields.

Functions

Link to this function

decode(data, opts \\ [])

View Source
@spec decode(binary(), [term()]) :: term()

Decodes a previously encoded entry.

Example

iex> "hello"
...> |> Nebulex.Entry.encode()
...> |> Nebulex.Entry.decode()
"hello"
Link to this function

encode(data, opts \\ [])

View Source
@spec encode(term(), [term()]) :: binary()

Encodes a cache entry.

Example

iex> "hello"
...> |> Nebulex.Entry.encode()
...> |> Nebulex.Entry.decode()
"hello"
@spec expired?(t()) :: boolean()

Returns whether the given entry has expired or not.

Example

iex> Nebulex.Entry.expired?(%Nebulex.Entry{})
false

iex> Nebulex.Entry.expired?(
...>   %Nebulex.Entry{touched: Nebulex.Time.now() - 10, ttl: 1}
...> )
true
@spec ttl(t()) :: timeout()

Returns the remaining time-to-live.

Example

iex> Nebulex.Entry.ttl(%Nebulex.Entry{})
:infinity

iex> ttl =
...>   Nebulex.Entry.ttl(
...>     %Nebulex.Entry{touched: Nebulex.Time.now(), ttl: 100}
...>   )
iex> ttl > 0
true