Current section

Files

Jump to
go_over src go_over@cache.erl
Raw

src/go_over@cache.erl

-module(go_over@cache).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([pull_if_not_cached/4]).
-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:replace_error(_pipe@2, nil),
_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), binary()) -> nil.
pull_if_not_cached(Path, Max_age, Pullfn, Cache_message) ->
case file_cached(Path, Max_age) of
{ok, true} ->
go_over@print:progress(<<"Cached: "/utf8, Cache_message/binary>>),
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 => 49})
end,
nil
end.