[wip] static file cache data structure works

This commit is contained in:
2025-10-22 13:17:39 -07:00
parent 80945de92d
commit 8b938fdd42
4 changed files with 174 additions and 55 deletions
+1 -53
View File
@@ -14,20 +14,10 @@
-include("$zx_include/zx_logger.hrl").
-record(e, {fs_path :: file:filename(),
last_modified :: file:date_time(),
mime_type :: string(),
contents :: binary()}).
-type entry() :: #e{}.
-record(s, {entries = #{ :: [entry()]}).
-record(s, {cache = fd_sfc_cache:new() :: fd_sfc_cache:cache()}).
-type state() :: #s{}.
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, none, []).
@@ -64,45 +54,3 @@ code_change(_, State, _) ->
terminate(_, _) ->
ok.
%%---------------------
%% doers
%%---------------------
-spec refresh_entry(Entry) -> Result
when Entry :: entry(),
Result :: {found, NewEntry :: entry()}
| not_found.
refresh_entry(Entry) ->
refresh_entry(Entry, false).
-spec refresh_entry(Entry, Force) -> Result
when Entry :: entry(),
Force :: boolean(),
Result :: {found, NewEntry :: entry()}
| not_found.
% @private
% Force = even if file has not been modified, still refresh contents
refresh_entry(E = #e{fs_path = Path}, Force) ->
case file:find_file(Path) of
{ok, _} -> re2(E, Force);
{error, not_found} -> not_found.
end.
re2(E = #e{fs_path = Path, last_modified = LastModified}, _Force = false) ->
case file:last_modified(Path) > LastModified of
false -> {found, E};
true -> re2(E, true)
end;
re2(E = #e{http_path = HttpPath}, _Force = true) ->
new_entry(HttpPath).
-spec new_entry(HttpPath) -> Result
when HttpPath :: binary(),
FilePath ::