Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make cxy_cache_sup use transient workers; delete cache cxy_cache_fsm init/term #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]} }.
2 changes: 1 addition & 1 deletion src/epocxy.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{application, epocxy,
[
{id, "epocxy"},
{vsn, "0.9.8h"},
{vsn, "0.9.8i"},
{description, "Erlang Patterns of Concurrency"},
{modules, [
ets_buffer,
Expand Down