Current section

Files

Jump to
go_over src go_over@util@cache.erl
Raw

src/go_over@util@cache.erl

-module(go_over@util@cache).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([pull_if_not_cached/5]).
-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:nil_error(_pipe@2),
_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(),
boolean(),
fun(() -> nil),
binary()
) -> nil.
pull_if_not_cached(Path, Max_age, Force_pull, Pullfn, Cache_message) ->
case {Force_pull, file_cached(Path, Max_age)} of
{false, {ok, true}} ->
go_over@util@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,
_pipe@2 = simplifile:create_directory_all(Path),
go_over@util@util:hard_fail(
_pipe@2,
<<"could not write cache file for "/utf8, Path/binary>>
),
_pipe@3 = Path,
_pipe@4 = cache_name(_pipe@3),
_pipe@5 = simplifile:write(_pipe@4, Now),
go_over@util@util:hard_fail(
_pipe@5,
<<"could not write cache file for "/utf8, Path/binary>>
),
nil
end.