30 lines
632 B
Erlang
30 lines
632 B
Erlang
-module(core_counters).
|
|
-export([inc/1, dec/1, get/1, read_and_reset/1]).
|
|
|
|
-define(TABLE, eventhub_counters).
|
|
|
|
inc(Key) ->
|
|
try ets:update_counter(?TABLE, Key, {2, 1}) % позиция 2 — значение
|
|
catch error:badarg ->
|
|
ets:insert(?TABLE, {Key, 1})
|
|
end.
|
|
|
|
dec(Key) ->
|
|
try ets:update_counter(?TABLE, Key, {2, -1})
|
|
catch error:badarg ->
|
|
ets:insert(?TABLE, {Key, 0})
|
|
end.
|
|
|
|
get(Key) ->
|
|
case ets:lookup(?TABLE, Key) of
|
|
[{Key, Val}] -> Val;
|
|
[] -> 0
|
|
end.
|
|
|
|
read_and_reset(Key) ->
|
|
case ets:lookup(?TABLE, Key) of
|
|
[{Key, Val}] ->
|
|
ets:insert(?TABLE, {Key, 0}),
|
|
Val;
|
|
[] -> 0
|
|
end. |