Add function 'get_counters/1'

This commit is contained in:
Feng Lee 2019-09-24 17:07:32 +08:00
parent 20ddd498fc
commit 609f442ea9
1 changed files with 11 additions and 5 deletions

View File

@ -19,27 +19,33 @@
-include("types.hrl"). -include("types.hrl").
-export([ update_counter/2 -export([ get_counters/1
, get_counter/1 , get_counter/1
, update_counter/2
, reset_counter/1 , reset_counter/1
]). ]).
-compile({inline, -compile({inline,
[ update_counter/2 [ get_counters/1
, get_counter/1 , get_counter/1
, update_counter/2
, reset_counter/1 , reset_counter/1
]}). ]}).
-type(key() :: term()). -type(key() :: term()).
-spec(update_counter(key(), number()) -> maybe(number())). -spec(get_counters(list(key())) -> list({key(), number()})).
update_counter(Key, Inc) -> get_counters(Keys) when is_list(Keys) ->
put(Key, get_counter(Key) + Inc). [{Key, emqx_pd:get_counter(Key)} || Key <- Keys].
-spec(get_counter(key()) -> number()). -spec(get_counter(key()) -> number()).
get_counter(Key) -> get_counter(Key) ->
case get(Key) of undefined -> 0; Cnt -> Cnt end. case get(Key) of undefined -> 0; Cnt -> Cnt end.
-spec(update_counter(key(), number()) -> maybe(number())).
update_counter(Key, Inc) ->
put(Key, get_counter(Key) + Inc).
-spec(reset_counter(key()) -> number()). -spec(reset_counter(key()) -> number()).
reset_counter(Key) -> reset_counter(Key) ->
case put(Key, 0) of undefined -> 0; Cnt -> Cnt end. case put(Key, 0) of undefined -> 0; Cnt -> Cnt end.