View Source Cacha (Cacha v0.2.1)

A simple caching agent.

Emulates the most basic REDIS APIs.

Keys can either be of binary or atom types. Values can be of any type.

Summary

Functions

Cacha.del deletes a given key. It always returns :ok.

Cacha.flush_all deletes all keys from the Cacha.

Cacha.get returns the value of a given key or nil if it does not exist.

Cacha.incr increments the value of a given key. It expects the key to be of integer type. If successful returns {:ok, value} with the new value. Returns {:error, reason} otherwise.

Cacha.set creates or updates the value for a given key.

Functions

del(key)

@spec del(String.t()) :: :ok

Cacha.del deletes a given key. It always returns :ok.

Examples

iex> Cacha.del(:a_key)
:ok

iex> Cacha.del(:a_missing_key)
:ok

flush_all()

@spec flush_all() :: :ok | :error

Cacha.flush_all deletes all keys from the Cacha.

Examples

iex> Cacha.set(:a_key, 10)
:ok
iex> Cacha.flush_all()
:ok
iex> Cacha.get(:a_key)
nil

get(key)

@spec get(String.t()) :: any()

Cacha.get returns the value of a given key or nil if it does not exist.

Examples

iex> Cacha.get(:a_key)
10

iex> Cacha.get(:a_missing_key)
nil

incr(key)

@spec incr(String.t()) :: {:ok, integer()} | {:error, String.t()}

Cacha.incr increments the value of a given key. It expects the key to be of integer type. If successful returns {:ok, value} with the new value. Returns {:error, reason} otherwise.

If the key does not exist, it creates it and increments its value.

Examples

iex> Cacha.incr(:first_key)
{:ok, 1}

iex> Cacha.incr(:first_key)
{:ok, 2}

iex> Cacha.set(:second_key, "a")
iex> Cacha.incr(:second_key)
{:error, "value is not an integer"}

set(key, value, opts \\ [])

@spec set(String.t(), any(), Keyword.t()) :: :ok | :error

Cacha.set creates or updates the value for a given key.

Options

:get - returns the old value present before setting the new one.

    if the key was empty, `nil` is returned.

Examples

iex> Cacha.set(:a_key, 10)
:ok

iex> Cacha.set(:a_key, 15, get: true)
10

iex> Cacha.get(:a_key)
15

start_link(args)

See Cacha.Server.start_link/1.