-module(go_over@cache). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([pull_if_not_cached/3]). -spec cache_name(binary()) -> binary(). cache_name(Path) -> filepath:join(Path, <<".go-over-cache"/utf8>>). -spec file_cached(binary(), integer()) -> {ok, boolean()} | {error, nil}. file_cached(Path, Max_age_seconds) -> _pipe = Path, _pipe@1 = cache_name(_pipe), _pipe@2 = simplifile:read(_pipe@1), _pipe@3 = gleam@result:map_error(_pipe@2, fun(_) -> nil end), _pipe@4 = gleam@result:'try'( _pipe@3, fun(_capture) -> gleam@int:base_parse(_capture, 10) end ), gleam@result:map( _pipe@4, fun(V) -> Cutoff = birl:from_unix(V + Max_age_seconds), case birl:compare(birl:utc_now(), Cutoff) of lt -> true; eq -> true; _ -> false end end ). -spec pull_if_not_cached(binary(), integer(), fun(() -> nil)) -> nil. pull_if_not_cached(Path, Max_age, Pullfn) -> case file_cached(Path, Max_age) of {ok, true} -> nil; _ -> Pullfn(), Now = begin _pipe = birl:utc_now(), _pipe@1 = birl:to_unix(_pipe), gleam@int:to_string(_pipe@1) end, _assert_subject = begin _pipe@2 = Path, _pipe@3 = cache_name(_pipe@2), simplifile:write(_pipe@3, Now) end, {ok, _} = case _assert_subject of {ok, _} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Assertion pattern match failed"/utf8>>, value => _assert_fail, module => <<"go_over/cache"/utf8>>, function => <<"pull_if_not_cached"/utf8>>, line => 35}) end, nil end.