-module(tallgrass@client@cache). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([shutdown/1, contains/2, lookup/2, insert/3, new/2]). -export_type([cache/0, cache_expiry/0, cache_flusher_message/0, cache_flusher_state/0, error/0]). -opaque cache() :: {cache, carpenter@table:set(binary(), binary()), fun(() -> nil)}. -type cache_expiry() :: {seconds, integer()} | {minutes, integer()} | {hours, integer()}. -type cache_flusher_message() :: {start_cache_flusher, gleam@erlang@process:subject(cache_flusher_message())} | {run_cache_flusher, gleam@erlang@process:subject(cache_flusher_message())} | stop_cache_flusher. -type cache_flusher_state() :: {cache_flusher_state, carpenter@table:set(binary(), binary()), integer()}. -type error() :: new_cache_error | not_found. -file("/Users/stevetoro/Code/tallgrass/src/tallgrass/client/cache.gleam", 58). -spec shutdown(cache()) -> nil. shutdown(Cache) -> {cache, _, Stop_cache_flusher} = Cache, Stop_cache_flusher(). -file("/Users/stevetoro/Code/tallgrass/src/tallgrass/client/cache.gleam", 64). -spec contains(cache(), binary()) -> boolean(). contains(Cache, Req) -> {cache, Table, _} = Cache, _pipe = Table, carpenter@table:contains(_pipe, Req). -file("/Users/stevetoro/Code/tallgrass/src/tallgrass/client/cache.gleam", 70). -spec lookup(cache(), binary()) -> {ok, binary()} | {error, error()}. lookup(Cache, Req) -> case begin _pipe = Cache, contains(_pipe, Req) end of false -> {error, not_found}; true -> {cache, Table, _} = Cache, _assert_subject = begin _pipe@1 = Table, carpenter@table:lookup(_pipe@1, Req) end, [Tup | _] = case _assert_subject of [_ | _] -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail, module => <<"tallgrass/client/cache"/utf8>>, function => <<"lookup"/utf8>>, line => 75}) end, {ok, erlang:element(2, Tup)} end. -file("/Users/stevetoro/Code/tallgrass/src/tallgrass/client/cache.gleam", 82). -spec insert(cache(), binary(), binary()) -> nil. insert(Cache, Req, Res) -> {cache, Table, _} = Cache, _pipe = Table, carpenter@table:insert(_pipe, [{Req, Res}]). -file("/Users/stevetoro/Code/tallgrass/src/tallgrass/client/cache.gleam", 87). -spec table(binary()) -> {ok, carpenter@table:set(any(), any())} | {error, nil}. table(Name) -> _pipe = carpenter@table:build(Name), _pipe@1 = carpenter@table:privacy(_pipe, public), _pipe@2 = carpenter@table:write_concurrency(_pipe@1, auto_write_concurrency), _pipe@3 = carpenter@table:read_concurrency(_pipe@2, true), _pipe@4 = carpenter@table:decentralized_counters(_pipe@3, true), _pipe@5 = carpenter@table:compression(_pipe@4, false), carpenter@table:set(_pipe@5). -file("/Users/stevetoro/Code/tallgrass/src/tallgrass/client/cache.gleam", 109). -spec handle_flusher_message(cache_flusher_message(), cache_flusher_state()) -> gleam@otp@actor:next(any(), cache_flusher_state()). handle_flusher_message(Message, State) -> case Message of {start_cache_flusher, Subject} -> gleam@erlang@process:send_after( Subject, erlang:element(3, State), {run_cache_flusher, Subject} ), gleam@otp@actor:continue(State); {run_cache_flusher, Subject@1} -> _pipe = erlang:element(2, State), carpenter@table:delete_all(_pipe), gleam@erlang@process:send_after( Subject@1, erlang:element(3, State), {run_cache_flusher, Subject@1} ), gleam@otp@actor:continue(State); stop_cache_flusher -> _pipe@1 = erlang:element(2, State), carpenter@table:drop(_pipe@1), {stop, normal} end. -file("/Users/stevetoro/Code/tallgrass/src/tallgrass/client/cache.gleam", 97). -spec start_flusher(carpenter@table:set(binary(), binary()), cache_expiry()) -> gleam@erlang@process:subject(cache_flusher_message()). start_flusher(Table, Expiry) -> Expire = case Expiry of {hours, Hours} -> ((1000 * 60) * 60) * Hours; {minutes, Minutes} -> (1000 * 60) * Minutes; {seconds, Seconds} -> 1000 * Seconds end, _assert_subject = gleam@otp@actor:start( {cache_flusher_state, Table, Expire}, fun handle_flusher_message/2 ), {ok, Flusher} = case _assert_subject of {ok, _} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail, module => <<"tallgrass/client/cache"/utf8>>, function => <<"start_flusher"/utf8>>, line => 103}) end, gleam@otp@actor:send(Flusher, {start_cache_flusher, Flusher}), Flusher. -file("/Users/stevetoro/Code/tallgrass/src/tallgrass/client/cache.gleam", 45). -spec new(binary(), cache_expiry()) -> {ok, cache()} | {error, error()}. new(Name, Expiry) -> case table(Name) of {ok, Table} -> Flusher = begin _pipe = Table, start_flusher(_pipe, Expiry) end, Stop = fun() -> gleam@otp@actor:send(Flusher, stop_cache_flusher) end, {ok, {cache, Table, Stop}}; _ -> {error, new_cache_error} end.