GlobalConst converts large Key-Value entities to a module to make fast accessing by thousands of processes.
This library inspired by FastGlobal and mochiglobal. In our case, we has thousands of entities to save, and don't want to make so many modules. So converting entities to a named module is a good approach.
Run mix test
## GlobalConstBench
benchmark name iterations average time
globalconst get string 100000000 0.04 µs/op
globalconst get atom 100000000 0.04 µs/op
globalconst get(10000keys) atom 100000000 0.06 µs/op
globalconst get(10000keys) string 100000000 0.07 µs/op
ets get atom 10000000 0.24 µs/op
ets get string 10000000 0.28 µs/op
fastglobal get 10000000 0.36 µs/op
agent get 100000 19.62 µs/op
Add it to mix.exs
def deps do
[
{:globalconst, "~> 0.3.0"}
]
end
Documentation can be found at https://hexdocs.pm/globalconst/.
Create a new global const map and get the value
GlobalConst.new(GlobalMap, %{a: 1, b: 2}) # GlobalConst.new(GlobalMap, %{a: 1, b: 2}, [key_type: :atom])
1 == GlobalMap.get(:a)
2 == GlobalMap.get(:b)
[:a, :b] == GlobalMap.keys()
{:error, :global_const_not_found} = GlobalMap.get(:c)
:default_value = GlobalMap.get(:c, :default_value)
GlobalConst.new(GlobalMapAny, %{:a => 1, "b" => 2, 3 => 3, [:c] => 4}, [key_type: :any])
1 == GlobalMapAny.get(:a)
2 == GlobalMapAny.get("b")
3 == GlobalMapAny.get(3)
4 == GlobalMapAny.get([:c])
Define a DummyModule to stop compiler warning.
defmodule GlobalMap do
use GlobalConst.DummyModule
end
GlobalConst.new(GlobalMap, %{a: 1, b: 2})
1 == GlobalMap.get(:a) # no compiler warning here.
GlobalConst is released under the MIT License. Check LICENSE file for more information.