Box.Cache (box v0.15.1)

View Source

Cache related function. They all depends on a Box.Cache server as this process will be the owner of the :ets table that holds the cached values.

Summary

Functions

Gets all record from a cache

Deletes a record by its key from the cache

Gets a record by its key

Inserts one or multiple records in the database with the provided options.

Memoizes a function in the given cache and under the given key. The function will be applied then cached under the key if the value matches the cache_match option. By default, the value won't be cached if it's nil, but one can override cache_match to alter this and use, for instance &Box.Result.succeeded?/1 instead to only cache records that matches {:ok, any()} tuples.

Select record from a cache using a match spec

Types

cache()

@type cache() :: atom()

input_record()

@type input_record() :: {key(), value()}

insert_option()

@type insert_option() :: {:ttl, non_neg_integer() | :infinity}

key()

@type key() :: atom() | tuple()

match()

@type match() :: [tuple()]

memoize_option()

@type memoize_option() :: {:cache_match, (any() -> boolean())}

record()

@type record() :: {key(), value(), [record_option()]}

record_option()

@type record_option() :: {:expiration, integer() | :never}

value()

@type value() :: any()

Functions

all(cache)

@spec all(cache()) :: [record()]

Gets all record from a cache

delete(cache, key)

@spec delete(cache(), key()) :: :ok

Deletes a record by its key from the cache

get(cache, key)

@spec get(cache(), key()) :: {:ok, value()} | {:error, :not_found}

Gets a record by its key

insert(cache, record_or_records, options \\ [])

@spec insert(cache(), input_record() | [input_record()], [insert_option()]) :: :ok

Inserts one or multiple records in the database with the provided options.

Available options

  • ttl: Time to live of the cache value.

memoize(cache, key, options \\ [], function)

@spec memoize(cache(), key(), [insert_option() | memoize_option()], (-> value())) ::
  value()

Memoizes a function in the given cache and under the given key. The function will be applied then cached under the key if the value matches the cache_match option. By default, the value won't be cached if it's nil, but one can override cache_match to alter this and use, for instance &Box.Result.succeeded?/1 instead to only cache records that matches {:ok, any()} tuples.

select(cache, match_spec)

@spec select(cache(), match()) :: [any()]

Select record from a cache using a match spec