Stash (Stash v2.0.0)
View SourceThis 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 from all namespaces.
Removes a value from the namespace.
Removes all items from a namespace.
Checks whether the namespace is empty.
Determines whether a given key exists inside the namespace.
Retrieves a value from the namespace.
Increments a key directly in the namespace 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 namespace, and returns them as an (unordered) list.
Loads a namespace into memory from DTS storage.
Persists a namespace onto disk to allow reload after the process dies.
Places a value in the namespace against a given key.
Removes a key from the namespace, whilst also returning the last known value.
Determines the size of the namespace.
Functions
@spec clear() :: true
Removes all items from all namespaces.
Examples
iex> Stash.put(:namespace1, "key1", "value1")
iex> Stash.put(:namespace2, "key2", "value2")
iex> Stash.put(:namespace3, "key3", "value3")
iex> Stash.clear()
true
iex> Stash.size(:namespace1)
0
iex> Stash.size(:namespace2)
0
iex> Stash.size(:namespace3)
0
Removes a value from the namespace.
Examples
iex> Stash.put(:my_namespace, "key", "value")
iex> Stash.get(:my_namespace, "key")
"value"
iex> Stash.delete(:my_namespace, "key")
true
iex> Stash.get(:my_namespace, "key")
nil
@spec drop(atom()) :: true
Removes all items from a namespace.
Examples
iex> Stash.put(:namespace, "key1", "value1")
iex> Stash.put(:namespace, "key2", "value2")
iex> Stash.put(:namespace, "key3", "value3")
iex> Stash.drop(:namespace)
true
iex> Stash.size(:namespace)
0
Checks whether the namespace is empty.
Examples
iex> Stash.put(:my_namespace, "key1", "value1")
iex> Stash.put(:my_namespace, "key2", "value2")
iex> Stash.put(:my_namespace, "key3", "value3")
iex> Stash.empty?(:my_namespace)
false
iex> Stash.clear(:my_namespace)
true
iex> Stash.empty?(:my_namespace)
true
Determines whether a given key exists inside the namespace.
Examples
iex> Stash.put(:my_namespace, "key", "value")
iex> Stash.exists?(:my_namespace, "key")
true
iex> Stash.exists?(:my_namespace, "missing_key")
false
Retrieves a value from the namespace.
Examples
iex> Stash.put(:my_namespace, "key", "value")
iex> Stash.get(:my_namespace, "key")
"value"
iex> Stash.get(:my_namespace, "missing_key")
nil
iex> Stash.get(:my_namespace, "missing_key, "default")
"default
Increments a key directly in the namespace by count. If the key does not exist
it is set to initial before then being incremented.
Examples
iex> Stash.put(:my_namespace, "key", 1)
iex> Stash.increment(:my_namespace, "key")
2
iex> Stash.increment(:my_namespace, "key", 2)
4
iex> Stash.increment(:my_namespace, "missing_key", 1)
1
iex> Stash.increment(:my_namespace, "a_missing_key", 1, 5)
6
Returns information about the backing ETS table.
Examples
iex> Stash.info()
[read_concurrency: true, write_concurrency: true, compressed: false,
memory: 1361, owner: #PID<0.126.0>, heir: :none, name: :my_namespace, size: 2,
node: :nonode@nohost, named_table: true, type: :set, keypos: 1,
protection: :public]
Retrieves all keys from the namespace, and returns them as an (unordered) list.
Examples
iex> Stash.put(:my_namespace, "key1", "value1")
iex> Stash.put(:my_namespace, "key2", "value2")
iex> Stash.put(:my_namespace, "key3", "value3")
iex> Stash.keys(:my_namespace)
[ "key2", "key1", "key3" ]
iex> Stash.keys(:empty_namespace)
[]
Loads a namespace into memory from DTS storage.
Examples
iex> Stash.load("/tmp/temporary.dat")
:ok
Persists a namespace onto disk to allow reload after the process dies.
Examples
iex> Stash.persist(:my_namespace, "/tmp/temporary.dat")
:ok
Places a value in the namespace against a given key.
Examples
iex> Stash.put(:my_namespace, "key", "value")
true
Removes a key from the namespace, whilst also returning the last known value.
Examples
iex> Stash.put(:my_namespace, "key", "value")
iex> Stash.remove(:my_namespace, "key")
"value"
iex> Stash.get(:my_namespace, "key")
nil
iex> Stash.remove(:my_namespace, "missing_key")
nil
iex> Stash.remove(:my_namespace, "missing_key, "default")
"default"
Determines the size of the namespace.
Examples
iex> Stash.put(:my_namespace, "key1", "value1")
iex> Stash.put(:my_namespace, "key2", "value2")
iex> Stash.put(:my_namespace, "key3", "value3")
iex> Stash.size(:my_namespace)
3