Krug.EtsUtil (Krug v2.0.27) View Source
Utilitary module to secure handle ETS in memory data.
Link to this section Summary
Functions
Deletes a existing ets table whit received ets_key
.
Case the table don't exists, return true.
Creates a new ets table whit received ets_key
and
visibility. Case the table already exists, do not create a new,
only return the existing ets_key.
Read a value relative to a key key
stored in a ets table whit received ets_key
.
Case the table don't exists, return nil.
Delete a key/value par in ets table identified by received ets_key
.
Case the table don't exists, return true.
Store a key/value par in ets table identified by received ets_key
.
Case the table don't exists, return false.
Link to this section Functions
Deletes a existing ets table whit received ets_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(ets_key)
.
Examples
iex > EtsUtil.delete("echo")
** (ArgumentError) argument error
iex > EtsUtil.delete(:echo)
true
iex > EtsUtil.delete(:keyThatNotExists)
true
Creates a new ets table whit received ets_key
and
visibility. Case the table already exists, do not create a new,
only return the existing ets_key.
SHOULD BE created in a process that act a task, living across the runtime of application. One good example is a Ecto repository.
defmodule MyApp.App.Repo do
use Ecto.Repo, otp_app: :my_app, adapter: Ecto.Adapters.MyXQL
...
alias Krug.EtsUtil
def init(_type, config) do
...
EtsUtil.new(:my_ets_key_atom_identifier)
{:ok, config}
end
end
``
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(ets_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
Read a value relative to a key key
stored in a ets table whit received ets_key
.
Case the table don't exists, return nil.
If the key don't exists, return nil.
Equivalent to :ets.lookup(ets_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"
Delete a key/value par in ets table identified by received ets_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(ets_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
Store a key/value par in ets table identified by received ets_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(ets_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