https://github.com/sean-lin/globalconst
GlobalConst converts large Key-Value entities to a module to make fast accessing by thousands of processes.
https://github.com/sean-lin/globalconst
elixir
Last synced: about 1 month ago
JSON representation
GlobalConst converts large Key-Value entities to a module to make fast accessing by thousands of processes.
- Host: GitHub
- URL: https://github.com/sean-lin/globalconst
- Owner: sean-lin
- License: mit
- Created: 2018-11-06T16:41:46.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-04-26T17:59:22.000Z (about 6 years ago)
- Last Synced: 2026-02-23T23:11:17.102Z (4 months ago)
- Topics: elixir
- Language: Elixir
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GlobalConst
[](https://hex.pm/packages/globalconst)
GlobalConst converts large Key-Value entities to a module to make fast accessing by thousands of processes.
## Thanks
This library inspired by [FastGlobal](https://github.com/discordapp/fastglobal) and [mochiglobal](https://github.com/mochi/mochiweb/blob/master/src/mochiglobal.erl).
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.
## Performance
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
```
## Installation
Add it to `mix.exs`
```elixir
def deps do
[
{:globalconst, "~> 0.3.0"}
]
end
```
Documentation can be found at [https://hexdocs.pm/globalconst/](https://hexdocs.pm/globalconst/).
## Usage
Create a new global const map and get the value
```elixir
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.
```elixir
defmodule GlobalMap do
use GlobalConst.DummyModule
end
GlobalConst.new(GlobalMap, %{a: 1, b: 2})
1 == GlobalMap.get(:a) # no compiler warning here.
```
## License
GlobalConst is released under [the MIT License](LICENSE).
Check [LICENSE](LICENSE) file for more information.