diff --git a/src/marina_cache.erl b/src/marina_cache.erl index 38db29d..48bb529 100644 --- a/src/marina_cache.erl +++ b/src/marina_cache.erl @@ -3,6 +3,7 @@ -export([ erase/2, + erase_server/1, get/2, init/0, put/3 @@ -20,6 +21,14 @@ erase(Pool, Key) -> {error, not_found} end. + +-spec erase_server(shackle:request_id()) -> ok. + +erase_server({ServerName, _}) -> + Pool = marina_utils:server_to_pool(ServerName), + ets:select_delete(?ETS_TABLE_CACHE, [{{{'$1', '$2'}, '_'}, [], [{'==', '$1', Pool}]}]), + ok. + -spec get(atom(), binary()) -> {ok, term()} | {error, not_found}. get(Pool, Key) -> diff --git a/src/marina_utils.erl b/src/marina_utils.erl index 8c9c0ac..2b91bc5 100644 --- a/src/marina_utils.erl +++ b/src/marina_utils.erl @@ -8,8 +8,9 @@ pack/1, query/2, query_opts/2, - sync_msg/2, + server_to_pool/1, startup/1, + sync_msg/2, timeout/2, unpack/1, use_keyspace/1 @@ -88,6 +89,14 @@ query_opts(timeout, QueryOpts) -> query_opts(values, QueryOpts) -> maps:get(values, QueryOpts, undefined). +-spec server_to_pool(atom()) -> atom(). + +server_to_pool(Node) -> + NodeSplit = binary:split(erlang:atom_to_binary(Node), <<"_">>, [global]), + PoolSplit = lists:sublist(NodeSplit, length(NodeSplit) - 1), + PoolBin = erlang:iolist_to_binary(lists:join(<<"_">>, PoolSplit)), + erlang:binary_to_atom(PoolBin). + -spec sync_msg(inet:socket(), iodata()) -> {ok, term()} | {error, term()}.