Stash v1.0.0 Stash

This module provides a convenient interface around ETS/DTS without taking large performance hits. Designed for being thrown into a project for basic memory-based storage, perhaps with some form of persistence required.

Summary

Functions

Removes all items in the cache

Removes a value from the cache

Checks whether the cache is empty

Determines whether a given key exists inside the cache

Retrieves a value from the cache

Increments a key directly in the cache by count. If the key does not exist it is set to initial before then being incremented

Returns information about the backing ETS table

Retrieves all keys from the cache, and returns them as an (unordered) list

Loads a cache into memory from DTS storage

Persists a cache onto disk to allow reload after the process dies

Removes a key from the cache, whilst also returning the last known value

Sets a value in the cache against a given key

Determines the size of the cache

Functions

clear(cache)

Specs

clear(atom) :: true

Removes all items in the cache.

Examples

iex> Stash.set(:my_cache, "key1", "value1")
iex> Stash.set(:my_cache, "key2", "value2")
iex> Stash.set(:my_cache, "key3", "value3")
iex> Stash.size(:my_cache)
3

iex> Stash.clear(:my_cache)
true

iex> Stash.size(:my_cache)
0
delete(cache, key)

Specs

delete(atom, any) :: true

Removes a value from the cache.

Examples

iex> Stash.set(:my_cache, "key", "value")
iex> Stash.get(:my_cache, "key")
"value"

iex> Stash.delete(:my_cache, "key")
true

iex> Stash.get(:my_cache, "key")
nil
empty?(cache)

Specs

empty?(atom) :: true | false

Checks whether the cache is empty.

Examples

iex> Stash.set(:my_cache, "key1", "value1")
iex> Stash.set(:my_cache, "key2", "value2")
iex> Stash.set(:my_cache, "key3", "value3")
iex> Stash.empty?(:my_cache)
false

iex> Stash.clear(:my_cache)
true

iex> Stash.empty?(:my_cache)
true
exists?(cache, key)

Specs

exists?(atom, any) :: true | false

Determines whether a given key exists inside the cache.

Examples

iex> Stash.set(:my_cache, "key", "value")
iex> Stash.exists?(:my_cache, "key")
true

iex> Stash.exists?(:my_cache, "missing_key")
false
get(cache, key)

Specs

get(atom, any) :: any

Retrieves a value from the cache.

Examples

iex> Stash.set(:my_cache, "key", "value")
iex> Stash.get(:my_cache, "key")
"value"

iex> Stash.get(:my_cache, "missing_key")
nil
inc(cache, key, count \\ 1, initial \\ 0)

Specs

inc(atom, any, number, number) :: number

Increments a key directly in the cache by count. If the key does not exist it is set to initial before then being incremented.

Examples

iex> Stash.set(:my_cache, "key", 1)
iex> Stash.inc(:my_cache, "key")
2

iex> Stash.inc(:my_cache, "key", 2)
4

iex> Stash.inc(:my_cache, "missing_key", 1)
1

iex> Stash.inc(:my_cache, "a_missing_key", 1, 5)
6
info(cache)

Specs

info(atom) :: [{atom, any}]

Returns information about the backing ETS table.

Examples

iex> Stash.info(:my_cache)
[read_concurrency: true, write_concurrency: true, compressed: false,
 memory: 1361, owner: #PID<0.126.0>, heir: :none, name: :my_cache, size: 2,
 node: :nonode@nohost, named_table: true, type: :set, keypos: 1,
 protection: :public]
keys(cache)

Specs

keys(atom) :: [any]

Retrieves all keys from the cache, and returns them as an (unordered) list.

Examples

iex> Stash.set(:my_cache, "key1", "value1")
iex> Stash.set(:my_cache, "key2", "value2")
iex> Stash.set(:my_cache, "key3", "value3")
iex> Stash.keys(:my_cache)
[ "key2", "key1", "key3" ]

iex> Stash.keys(:empty_cache)
[]
load(cache, path)

Specs

load(atom, binary) :: atom

Loads a cache into memory from DTS storage.

Examples

iex> Stash.load(:my_cache, "/tmp/temporary.dat")
:ok
persist(cache, path)

Specs

persist(atom, binary) :: atom

Persists a cache onto disk to allow reload after the process dies.

Examples

iex> Stash.persist(:my_cache, "/tmp/temporary.dat")
:ok
remove(cache, key)

Specs

remove(atom, any) :: any

Removes a key from the cache, whilst also returning the last known value.

Examples

iex> Stash.set(:my_cache, "key", "value")
iex> Stash.remove(:my_cache, "key")
"value"

iex> Stash.get(:my_cache, "key")
nil

iex> Stash.remove(:my_cache, "missing_key")
nil
set(cache, key, value)

Specs

set(atom, any, any) :: true

Sets a value in the cache against a given key.

Examples

iex> Stash.set(:my_cache, "key", "value")
true
size(cache)

Specs

size(atom) :: number

Determines the size of the cache.

Examples

iex> Stash.set(:my_cache, "key1", "value1")
iex> Stash.set(:my_cache, "key2", "value2")
iex> Stash.set(:my_cache, "key3", "value3")
iex> Stash.size(:my_cache)
3