Skip to content

GlobalConst converts large Key-Value entities to a module to make fast accessing by thousands of processes.

License

Notifications You must be signed in to change notification settings

sean-lin/globalconst

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GlobalConst

Hex.pm Version

GlobalConst converts large Key-Value entities to a module to make fast accessing by thousands of processes.

Thanks

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.

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

def deps do
  [
    {:globalconst, "~> 0.3.0"}
  ]
end

Documentation can be found at https://hexdocs.pm/globalconst/.

Usage

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.

License

GlobalConst is released under the MIT License. Check LICENSE file for more information.

About

GlobalConst converts large Key-Value entities to a module to make fast accessing by thousands of processes.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages