static file caching/serving seems to work
This commit is contained in:
+42
-2
@@ -5,6 +5,8 @@
|
||||
|
||||
-export([
|
||||
%% caller context
|
||||
base_path/0,
|
||||
renew/0, query/1,
|
||||
start_link/0,
|
||||
|
||||
%% process context
|
||||
@@ -15,8 +17,24 @@
|
||||
-include("$zx_include/zx_logger.hrl").
|
||||
|
||||
|
||||
-record(s, {cache = fd_sfc_cache:new() :: fd_sfc_cache:cache()}).
|
||||
-type state() :: #s{}.
|
||||
-record(s, {base_path = base_path() :: file:filename(),
|
||||
cache = fd_sfc_cache:new(base_path()) :: fd_sfc_cache:cache(),
|
||||
auto_renew = 5_000 :: pos_integer()}).
|
||||
%-type state() :: #s{}.
|
||||
|
||||
|
||||
%%-----------------------------------------------------------------------------
|
||||
%% caller context
|
||||
%%-----------------------------------------------------------------------------
|
||||
|
||||
base_path() ->
|
||||
filename:join([zx:get_home(), "priv", "static"]).
|
||||
|
||||
renew() ->
|
||||
gen_server:cast(?MODULE, renew).
|
||||
|
||||
query(Path) ->
|
||||
gen_server:call(?MODULE, {query, Path}).
|
||||
|
||||
start_link() ->
|
||||
gen_server:start_link({local, ?MODULE}, ?MODULE, none, []).
|
||||
@@ -31,19 +49,31 @@ start_link() ->
|
||||
init(none) ->
|
||||
log(info, "starting fd_cache"),
|
||||
InitState = #s{},
|
||||
erlang:send_after(3_000, self(), auto_renew),
|
||||
{ok, InitState}.
|
||||
|
||||
|
||||
handle_call({query, Path}, _, State = #s{cache = Cache}) ->
|
||||
Reply = fd_sfc_cache:query(Path, Cache),
|
||||
{reply, Reply, State};
|
||||
handle_call(Unexpected, From, State) ->
|
||||
tell("~tp: unexpected call from ~tp: ~tp", [?MODULE, Unexpected, From]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
handle_cast(renew, State) ->
|
||||
NewState = i_renew(State),
|
||||
{noreply, NewState};
|
||||
handle_cast(Unexpected, State) ->
|
||||
tell("~tp: unexpected cast: ~tp", [?MODULE, Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
handle_info(auto_renew, State = #s{auto_renew = MS}) ->
|
||||
log(info, "~tp: auto_renew", [?MODULE]),
|
||||
erlang:send_after(MS, self(), auto_renew),
|
||||
NewState = i_renew(State),
|
||||
{noreply, NewState};
|
||||
handle_info(Unexpected, State) ->
|
||||
tell("~tp: unexpected info: ~tp", [?MODULE, Unexpected]),
|
||||
{noreply, State}.
|
||||
@@ -54,3 +84,13 @@ code_change(_, State, _) ->
|
||||
|
||||
terminate(_, _) ->
|
||||
ok.
|
||||
|
||||
|
||||
%%-----------------------------------------------------------------------------
|
||||
%% internals
|
||||
%%-----------------------------------------------------------------------------
|
||||
|
||||
i_renew(State = #s{base_path = BasePath}) ->
|
||||
NewCache = fd_sfc_cache:new(BasePath),
|
||||
NewState = State#s{cache = NewCache},
|
||||
NewState.
|
||||
|
||||
Reference in New Issue
Block a user