Krug.EtsUtil (Krug v0.4.11) View Source

Utilitary module to secure handle ETS in memory data.

Link to this section Summary

Functions

Deletes a existing ets table whit received session_key. Case the table don't exists, return true.

Creates a new ets table whit received session_key and visibility. Case the table already exists, do not create a new, only return the existing session_key.

Read a value relative to a key key stored in a ets table whit received session_key. Case the table don't exists, return nil.

Delete a key/value par in ets table identified by received session_key. Case the table don't exists, return true.

Store a key/value par in ets table identified by received session_key. Case the table don't exists, return false.

Link to this section Functions

Deletes a existing ets table whit received session_key. Case the table don't exists, return true.

The garbage collector do not destroy the ets tables. We need explicitly make it. Do not forget of call EtsUtil.delete(:my_key) before of terminate the proccess that has created it whit EtsUtil.new(...).

Doesn't acceppt string as key. EtsUtil.delete("key") will fail, as in :ets direct call.

Equivalent to :ets.delete(session_key).

Examples

iex > EtsUtil.delete("echo")
** (ArgumentError) argument error
iex > EtsUtil.delete(:echo)
true
iex > EtsUtil.delete(:keyThatNotExists)
true
Link to this function

new(session_key, visibility \\ "protected")

View Source

Creates a new ets table whit received session_key and visibility. Case the table already exists, do not create a new, only return the existing session_key.

Visibility can be one of ["public","protected","private"] as in ets documentation. By default the value is "protected", as defined in :ets machanism. If a not valid value for visibilty is received, the default value is assumed.

Use "private" for the values be acessible only in module where the ETS table is declared. Escope "protected" allows access to all modules in same supervisor tree (a same Thread processes analogy), and "public" allows access to any process of any supervisor tree (concurrent Threads access equivalency).

The garbage collector do not destroy the ets tables. We need explicitly make it. Do not forget of call EtsUtil.delete(:my_key) before of terminate the proccess that has created it whit EtsUtil.new(...).

Equivalent to :ets.new(session_key, [:set, :private, :named_table])

Doesn't acceppt string as key. EtsUtil.new("key") will fail, as in :ets direct call.

Examples

iex > EtsUtil.new("echo")
** (ArgumentError) argument error
iex > EtsUtil.new(:echo)
:echo
iex > EtsUtil.new(:echo,"protected")
:echo
iex > EtsUtil.new(:echo,"private")
:echo
iex > EtsUtil.new(:echo,"public")
:echo
Link to this function

read_from_cache(session_key, key)

View Source

Read a value relative to a key key stored in a ets table whit received session_key. Case the table don't exists, return nil.

If the key don't exists, return nil.

Equivalent to :ets.lookup(session_key,key) |> Tuple.to_list() |> Enum.at(1)

Examples

iex > EtsUtil.read_from_cache(:keyThatDontExists,"ping")
nil
iex > EtsUtil.new(:echo)
iex > EtsUtil.read_from_cache(:echo,"ping")
nil
iex > EtsUtil.new(:echo)
iex > EtsUtil.store_in_cache(:echo,"ping","pong")
iex > EtsUtil.read_from_cache(:echo,"ping")
"pong"
iex > EtsUtil.new(:echo)
iex > EtsUtil.store_in_cache(:echo,"ping","pong")
iex > EtsUtil.store_in_cache(:echo,"ping","foo")
iex > EtsUtil.read_from_cache(:echo,"ping")
"foo"
Link to this function

remove_from_cache(session_key, key)

View Source

Delete a key/value par in ets table identified by received session_key. Case the table don't exists, return true.

If the key don't exists in ets table or its value is nil, return true.

Equivalent to :ets.delete(session_key,key)

Examples

iex > EtsUtil.remove_from_cache(:keyThatDontExists,"ping")
true
iex > EtsUtil.new(:echo)
iex > EtsUtil.remove_from_cache(:echo,"ping")
true
iex > EtsUtil.new(:echo)
iex > EtsUtil.store_in_cache(:echo,"ping","pong")
iex > EtsUtil.remove_from_cache(:echo,"ping")
true
Link to this function

store_in_cache(session_key, key, value)

View Source

Store a key/value par in ets table identified by received session_key. Case the table don't exists, return false.

If a old value exists and couldn't be replaced, return false.

Equivalent to :ets.insert(session_key,{key,value}).

Examples

iex > EtsUtil.store_in_cache(:keyThatDontExists,"ping","pong")
false
iex > EtsUtil.new(:echo)
iex > EtsUtil.store_in_cache(:echo,"ping","pong")
true
iex > EtsUtil.new(:echo)
iex > EtsUtil.store_in_cache(:echo,"ping","pong")
iex > EtsUtil.store_in_cache(:echo,"ping","foo")
true