Mnesia の dirty_update_counter/3 を使う
voluntas
Posted on January 21, 2020
mnesia に カウンターを扱う関数があるので使って見たメモ。
- レコードが無ければ作ってくれる
- 加算後の値を返してくれる
$ erl
Erlang/OTP 17 [erts-6.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Eshell V6.1 (abort with ^G)
1> mnesia:start().
ok
2> rd(store, {key, value}).
store
3> mnesia:create_table(store, []).
{atomic,ok}
4> mnesia:dirty_update_counter(store, <<"spam">>, 1).
1
5> mnesia:dirty_update_counter(store, <<"spam">>, 1).
2
6> mnesia:dirty_update_counter(store, <<"spam">>, 1).
3
7> mnesia:dirty_update_counter(store, <<"spam">>, 1).
4
8> mnesia:dirty_read(store, <<"spam">>).
[#store{key = <<"spam">>,value = 4}]
9> mnesia:dirty_update_counter(store, <<"egg">>, 1).
1
10> mnesia:dirty_read(store, <<"egg">>).
[#store{key = <<"egg">>,value = 1}]
10> timer:tc(fun() -> mnesia:dirty_update_counter(store, <<"spam">>, 1) end).
{45,6}
シングルノードであれば 45 マイクロセカンドと高速です。ただ ets だと一桁台なので、カウンターの性能に引っ張られる可能性もありますね。
💖 💪 🙅 🚩
voluntas
Posted on January 21, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
undefined Unsafe Impedance: Safe Languages and Safe by Design Software, by Lee Barney and Adolfo Neto
November 2, 2024
rabbitmq RabbitMQ with Web MQTT Plugin vs. Node.js : Performance and Memory Usage Comparison
October 29, 2024
elixir New to dev.to and Excited to Share ProxyConf: My Elixir-Powered API Control Plane
October 25, 2024