Skip to content

Commit

Permalink
Make cxy_cache_sup use transient workers; delete cache cxy_cache_fsm …
Browse files Browse the repository at this point in the history
…init/term
  • Loading branch information
jaynel committed Jan 25, 2016
1 parent 3a7eff7 commit e3313a2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/cxy_cache_fsm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@
%% itself is initialized inside the init function so that the new
%% FSM instance is the owner of the internal generation ets tables.

%% Supervisor restarts of cxy_cache_fsm need to ensure the old
%% cache meta data is not still around, so any lingering old
%% caches have to be deleted before instantiating them again.

%% No generation creation, so no poll time.
start_link(Cache_Name, Cache_Mod)
when is_atom(Cache_Name), is_atom(Cache_Mod) ->
_ = cxy_cache:delete(Cache_Name),
Cache_Name = cxy_cache:reserve(Cache_Name, Cache_Mod),
gen_fsm:start_link(?MODULE, {Cache_Name}, []).

Expand All @@ -75,30 +80,35 @@ start_link(Cache_Name, Cache_Mod, Gen_Fun)
start_link(Cache_Name, Cache_Mod, Gen_Fun, Poll_Time)
when is_atom(Cache_Name), is_atom(Cache_Mod), is_function(Gen_Fun, 3),
is_integer(Poll_Time), Poll_Time >= 10 ->
_ = cxy_cache:delete(Cache_Name),
Cache_Name = cxy_cache:reserve(Cache_Name, Cache_Mod, Gen_Fun),
gen_fsm:start_link(?MODULE, {Cache_Name, Poll_Time}, []);
%% Use strictly time-based microsecond generational change
%% (but millisecond granularity on FSM polling)...
start_link(Cache_Name, Cache_Mod, time, Gen_Frequency)
when is_atom(Cache_Name), is_atom(Cache_Mod),
is_integer(Gen_Frequency), Gen_Frequency >= 10000 ->
_ = cxy_cache:delete(Cache_Name),
Cache_Name = cxy_cache:reserve(Cache_Name, Cache_Mod, time, Gen_Frequency),
Poll_Time = round(Gen_Frequency / 1000) + 1,
gen_fsm:start_link(?MODULE, {Cache_Name, Poll_Time}, []);
%% Generational change occurs based on access frequency (using default polling time to check).
start_link(Cache_Name, Cache_Mod, count, Threshold)
when is_atom(Cache_Name), is_atom(Cache_Mod), is_integer(Threshold), Threshold > 0 ->
_ = cxy_cache:delete(Cache_Name),
Cache_Name = cxy_cache:reserve(Cache_Name, Cache_Mod, count, Threshold),
gen_fsm:start_link(?MODULE, {Cache_Name}, []);
%% Override default polling with a non-generational cache.
start_link(Cache_Name, Cache_Mod, none, Poll_Time)
when is_atom(Cache_Name), is_atom(Cache_Mod), is_integer(Poll_Time), Poll_Time >= 10 ->
_ = cxy_cache:delete(Cache_Name),
Cache_Name = cxy_cache:reserve(Cache_Name, Cache_Mod, none),
gen_fsm:start_link(?MODULE, {Cache_Name, Poll_Time}, []).

%% Generational change occurs based on access frequency (using override polling time to check).
start_link(Cache_Name, Cache_Mod, count, Threshold, Poll_Time)
when is_atom(Cache_Name), is_atom(Cache_Mod), is_integer(Threshold), Threshold > 0 ->
_ = cxy_cache:delete(Cache_Name),
Cache_Name = cxy_cache:reserve(Cache_Name, Cache_Mod, count, Threshold),
gen_fsm:start_link({local, ?SERVER}, ?MODULE, {Cache_Name, Poll_Time}, []).

Expand Down Expand Up @@ -133,8 +143,10 @@ init_finish(#ecf_state{poll_frequency=Poll_Millis} = Init_State) ->
erlang:send_after(Poll_Millis, self(), timeout),
{ok, 'POLL', Init_State}.

terminate (_Reason, _State_Name, #ecf_state{}) -> ok.
code_change (_OldVsn, State_Name, #ecf_state{} = State, _Extra) -> {ok, State_Name, State}.
terminate (_Reason, _State_Name, #ecf_state{cache_name=Cache_Name}) ->
_ = cxy_cache:delete(Cache_Name),
ok.

%% The FSM has only the 'POLL' state.
-spec 'POLL'(stop, #ecf_state{}) -> {stop, normal}.
Expand Down
5 changes: 2 additions & 3 deletions src/cxy_cache_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@

start_link() -> supervisor:start_link({local, ?SERVER}, ?MODULE, {}).


%% Start a non-generational cache (all data fits in memory).
start_cache(Cache_Name, Cache_Mod)
when is_atom(Cache_Name), is_atom(Cache_Mod) ->
Expand Down Expand Up @@ -84,8 +83,8 @@ start_cache(Cache_Name, Cache_Mod, Type, Threshold)

-spec init({}) -> sup_init_return().

-define(CHILD(__Mod, __Args), {__Mod, {__Mod, start_link, __Args}, temporary, 2000, worker, [__Mod]}).
-define(CHILD(__Mod, __Args), {__Mod, {__Mod, start_link, __Args}, transient, 2000, worker, [__Mod]}).

init({}) ->
Cache_Fsm = ?CHILD(cxy_cache_fsm, []),
{ok, { {simple_one_for_one, 5, 60}, [Cache_Fsm]} }.
{ok, { {simple_one_for_one, 5, 10}, [Cache_Fsm]} }.

0 comments on commit e3313a2

Please sign in to comment.