|
@ -73,7 +73,35 @@ set(Key, Value) -> |
|
|
%% persistent term was added in OTP 21.2 but we can't |
|
|
%% persistent term was added in OTP 21.2 but we can't |
|
|
%% check minor versions with macros so we're stuck waiting |
|
|
%% check minor versions with macros so we're stuck waiting |
|
|
%% for OTP 22 |
|
|
%% for OTP 22 |
|
|
-if(?OTP_RELEASE < 22). |
|
|
|
|
|
|
|
|
-ifdef(HAVE_PERSISTENT_TERM). |
|
|
|
|
|
init() -> |
|
|
|
|
|
ok. |
|
|
|
|
|
|
|
|
|
|
|
insert(Key, Value) -> |
|
|
|
|
|
persistent_term:put({?TBL, Key}, Value). |
|
|
|
|
|
|
|
|
|
|
|
insert_new(Key, Value) -> |
|
|
|
|
|
try persistent_term:get({?TBL, Key}) of |
|
|
|
|
|
_Value -> |
|
|
|
|
|
false |
|
|
|
|
|
catch error:badarg -> |
|
|
|
|
|
insert(Key, Value), |
|
|
|
|
|
true |
|
|
|
|
|
end. |
|
|
|
|
|
|
|
|
|
|
|
lookup(Key, Default) -> |
|
|
|
|
|
try persistent_term:get({?TBL, Key}) of |
|
|
|
|
|
Value -> Value |
|
|
|
|
|
catch |
|
|
|
|
|
error:badarg -> |
|
|
|
|
|
Default |
|
|
|
|
|
end. |
|
|
|
|
|
|
|
|
|
|
|
cleanup() -> |
|
|
|
|
|
[ persistent_term:erase(K) || {{?TBL, _} = K, _} <- persistent_term:get() ]. |
|
|
|
|
|
|
|
|
|
|
|
-else. |
|
|
|
|
|
|
|
|
init() -> |
|
|
init() -> |
|
|
%% set up the ETS configuration table |
|
|
%% set up the ETS configuration table |
|
|
_ = try ets:new(?TBL, [named_table, public, set, {keypos, 1}, {read_concurrency, true}]) of |
|
|
_ = try ets:new(?TBL, [named_table, public, set, {keypos, 1}, {read_concurrency, true}]) of |
|
@ -85,10 +113,10 @@ init() -> |
|
|
end. |
|
|
end. |
|
|
|
|
|
|
|
|
insert(Key, Value) -> |
|
|
insert(Key, Value) -> |
|
|
ets:insert(?TBL, {{Sink, Key}, Value}). |
|
|
|
|
|
|
|
|
ets:insert(?TBL, {Key, Value}). |
|
|
|
|
|
|
|
|
insert_new(Key, Value) -> |
|
|
insert_new(Key, Value) -> |
|
|
ets:insert_new(?TBL, Key, Value). |
|
|
|
|
|
|
|
|
ets:insert_new(?TBL, {Key, Value}). |
|
|
|
|
|
|
|
|
lookup(Key, Default) -> |
|
|
lookup(Key, Default) -> |
|
|
try |
|
|
try |
|
@ -104,32 +132,5 @@ lookup(Key, Default) -> |
|
|
end. |
|
|
end. |
|
|
|
|
|
|
|
|
cleanup() -> ok. |
|
|
cleanup() -> ok. |
|
|
|
|
|
|
|
|
-else. |
|
|
|
|
|
init() -> ok. |
|
|
|
|
|
|
|
|
|
|
|
insert(Key, Value) -> |
|
|
|
|
|
persistent_term:put({?TBL, Key}, Value). |
|
|
|
|
|
|
|
|
|
|
|
insert_new(Key, Value) -> |
|
|
|
|
|
try persistent_term:get({?TBL, Key}) of |
|
|
|
|
|
_Value -> |
|
|
|
|
|
false |
|
|
|
|
|
catch error:badarg -> |
|
|
|
|
|
insert(Key, Value), |
|
|
|
|
|
true |
|
|
|
|
|
end. |
|
|
|
|
|
|
|
|
|
|
|
lookup(Key, Default) -> |
|
|
|
|
|
try persistent_term:get({?TBL, Key}) of |
|
|
|
|
|
Value -> Value |
|
|
|
|
|
catch |
|
|
|
|
|
error:badarg -> |
|
|
|
|
|
Default |
|
|
|
|
|
end. |
|
|
|
|
|
|
|
|
|
|
|
cleanup() -> |
|
|
|
|
|
[ persistent_term:erase(K) || {{?TBL, _} = K, _} <- persistent_term:get() ]. |
|
|
|
|
|
|
|
|
|
|
|
-endif. |
|
|
-endif. |
|
|
|
|
|
|