Nebulex v2.0.0-rc.0 Nebulex.Adapter behaviour View Source

This module specifies the adapter API that a Cache adapter is required to implement.

Link to this section Summary

Functions

RExecutes the function fun passing as parameters the adapter and metadata (from the init/1 callback) associated with the given cache name_or_pid.

Callbacks

The callback invoked in case the adapter needs to inject code.

Deletes a single entry from cache.

Returns true if the given key exists and the new ttl was successfully updated, otherwise, false is returned.

Flushes the cache and returns the number of evicted keys.

Gets the value for a specific key in cache.

Gets a collection of entries from the Cache, returning them as Map.t() of the values associated with the set of keys requested.

Returns whether the given key exists in cache.

Increments or decrements the counter mapped to the given key.

Initializes the adapter supervision tree by returning the children.

Puts the given value under key into the cache.

Puts the given entries (key/value pairs) into the cache.

Returns the total number of cached entries.

Returns and removes the entry with key key in the cache.

Returns true if the given key exists and the last access time was successfully updated, otherwise, false is returned.

Returns the TTL (time-to-live) for the given key. If the key does not exist, then nil is returned.

Link to this section Types

Specs

adapter_meta() :: map()

The metadata returned by the adapter init/1.

It must be a map and Nebulex itself will always inject two keys into the meta:

  • :cache - The cache module.
  • :name - The nase of the cache.
  • :pid - The PID returned by the child spec returned in init/1

Specs

cache() :: Nebulex.Cache.t()

Specs

child_spec() :: :supervisor.child_spec() | {module(), term()} | module() | nil

Specs

entries() :: Nebulex.Cache.entries()

Specs

entry() :: Nebulex.Entry.t()

Specs

key() :: Nebulex.Cache.key()

Specs

on_write() :: :put | :put_new | :replace

Specs

opts() :: Nebulex.Cache.opts()

Specs

t() :: module()

Specs

ttl() :: timeout()

Specs

value() :: Nebulex.Cache.value()

Link to this section Functions

Link to this function

with_meta(name_or_pid, fun)

View Source

Specs

with_meta(atom() | pid(), (module(), adapter_meta() -> term())) :: term()

RExecutes the function fun passing as parameters the adapter and metadata (from the init/1 callback) associated with the given cache name_or_pid.

It expects a name or a PID representing the cache.

Link to this section Callbacks

Specs

__before_compile__(term(), env :: Macro.Env.t()) :: Macro.t()

The callback invoked in case the adapter needs to inject code.

Link to this callback

delete(adapter_meta, key, opts)

View Source

Specs

delete(adapter_meta(), key(), opts()) :: :ok

Deletes a single entry from cache.

See Nebulex.Cache.delete/2.

Link to this callback

expire(adapter_meta, key, ttl)

View Source

Specs

expire(adapter_meta(), key(), ttl()) :: boolean()

Returns true if the given key exists and the new ttl was successfully updated, otherwise, false is returned.

See Nebulex.Cache.expire/2.

Specs

flush(adapter_meta()) :: integer()

Flushes the cache and returns the number of evicted keys.

See Nebulex.Cache.flush/0.

Link to this callback

get(adapter_meta, key, opts)

View Source

Specs

get(adapter_meta(), key(), opts()) :: value()

Gets the value for a specific key in cache.

See Nebulex.Cache.get/2.

Link to this callback

get_all(adapter_meta, list, opts)

View Source

Specs

get_all(adapter_meta(), [key()], opts()) :: map()

Gets a collection of entries from the Cache, returning them as Map.t() of the values associated with the set of keys requested.

For every key that does not hold a value or does not exist, that key is simply ignored. Because of this, the operation never fails.

See Nebulex.Cache.get_all/2.

Link to this callback

has_key?(adapter_meta, key)

View Source

Specs

has_key?(adapter_meta(), key()) :: boolean()

Returns whether the given key exists in cache.

See Nebulex.Cache.has_key?/1.

Link to this callback

incr(adapter_meta, key, incr, ttl, opts)

View Source

Specs

incr(adapter_meta(), key(), incr :: integer(), ttl(), opts()) :: integer()

Increments or decrements the counter mapped to the given key.

See Nebulex.Cache.incr/3.

Specs

init(opts()) :: {:ok, child_spec(), adapter_meta()}

Initializes the adapter supervision tree by returning the children.

Link to this callback

put(adapter_meta, key, value, ttl, on_write, opts)

View Source

Specs

put(adapter_meta(), key(), value(), ttl(), on_write(), opts()) :: boolean()

Puts the given value under key into the cache.

Returns true if the value with key key is successfully inserted; otherwise false is returned.

The ttl argument sets the time-to-live for the stored entry. If it is not set, it means the entry hasn't a time-to-live, then it shouldn't expire.

OnWrite

The on_write argument supports the following values:

  • :put - If the key already exists, it is overwritten. Any previous time-to-live associated with the key is discarded on successful write operation.

  • :put_new - It only stores the entry if the key does not already exist, otherwise, false is returned.

  • :replace - Alters the value stored under the given key, but only if the key already exists into the cache, otherwise, false is returned.

See Nebulex.Cache.put/3, Nebulex.Cache.put_new/3, Nebulex.Cache.replace/3.

Link to this callback

put_all(adapter_meta, entries, ttl, on_write, opts)

View Source

Specs

put_all(adapter_meta(), entries(), ttl(), on_write(), opts()) :: boolean()

Puts the given entries (key/value pairs) into the cache.

Returns true if all the keys were inserted. If no key was inserted (at least one key already existed), false is returned.

The ttl argument sets the time-to-live for the stored entry. If it is not set, it means the entry hasn't a time-to-live, then it shouldn't expire. The given ttl is applied to all keys.

OnWrite

The on_write argument supports the following values:

  • :put - If the key already exists, it is overwritten. Any previous time-to-live associated with the key is discarded on successful write operation.

  • :put_new - It only stores the entry if the key does not already exist, otherwise, false is returned.

Ideally, this operation should be atomic, so all given keys are set at once. But it depends purely on the adapter's implementation and the backend used internally by the adapter. Hence, it is recommended to checkout the adapter's documentation.

See Nebulex.Cache.put_all/2.

Specs

size(adapter_meta()) :: integer()

Returns the total number of cached entries.

See Nebulex.Cache.size/0.

Link to this callback

take(adapter_meta, key, opts)

View Source

Specs

take(adapter_meta(), key(), opts()) :: value()

Returns and removes the entry with key key in the cache.

See Nebulex.Cache.take/2.

Link to this callback

touch(adapter_meta, key)

View Source

Specs

touch(adapter_meta(), key()) :: boolean()

Returns true if the given key exists and the last access time was successfully updated, otherwise, false is returned.

See Nebulex.Cache.touch/1.

Specs

ttl(adapter_meta(), key()) :: ttl() | nil

Returns the TTL (time-to-live) for the given key. If the key does not exist, then nil is returned.

See Nebulex.Cache.ttl/1.