| summaryrefslogtreecommitdiff | 
diff options
| author | nsensfel <SpamShield0@noot-noot.org> | 2017-11-28 17:14:02 +0100 | 
|---|---|---|
| committer | nsensfel <SpamShield0@noot-noot.org> | 2017-11-28 17:14:02 +0100 | 
| commit | f539b7072c357339328d9bfd54f1f1ed51828586 (patch) | |
| tree | b6205dd79c78090831e812aceac177d2a9f35d28 /src/io/timed_caches_manager.erl | |
| parent | 80358376b9300a0d73cb8b62dfa9fdd65240ca66 (diff) | |
Trying to tidy up this mess.
Diffstat (limited to 'src/io/timed_caches_manager.erl')
| -rw-r--r-- | src/io/timed_caches_manager.erl | 141 | 
1 files changed, 141 insertions, 0 deletions
| diff --git a/src/io/timed_caches_manager.erl b/src/io/timed_caches_manager.erl new file mode 100644 index 0000000..ad66fbb --- /dev/null +++ b/src/io/timed_caches_manager.erl @@ -0,0 +1,141 @@ +-module(timed_caches_manager). +-behavior(gen_server). + +%%%% gen_server exports +-export( +   [ +      init/1, +      handle_cast/2, +      handle_call/3, +      terminate/2, +      code_change/3, +      format_status/2, +      handle_info/2 +   ] +). + +%%%% actual interface +-export( +   [ +      add_cache/3, +      inherit_cache/3, +      delete_cache/2, +      get_timeout/1 +   ] +) +. +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% LOCAL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%% Manager %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +delete_cache (DB) -> +   ets:delete(DB). + +add_cache (DB, none) -> +   io:format("~nTimed Caches Manager added a new cache. ~n"), +   ets:new( +      DB, +      [ +         set, +         public, +         named_table, +         {keypos, 2}, +         {read_concurrency, true}, +         {heir, none} +      ] +   ); +add_cache (DB, Heir) -> +   io:format("~nTimed Caches Manager added a new cache. ~n"), +   ets:new( +      DB, +      [ +         set, +         public, +         named_table, +         {keypos, 2}, +         {read_concurrency, true}, +         {heir, Heir, DB} +      ] +   ). + +inherit_cache (CacheList, DB, Heir) -> +   case lists:member(DB, CacheList) of +      true -> +         ets:setopts(DB, {heir, Heir, DB}), +         CacheList; + +      false -> +         [DB|CacheList] +   end. + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%% gen_server +init (CacheList) -> +   io:format("~nStarting Timed Caches Manager..."), +   {ok, CacheList}. + +handle_call ({delete, CacheName}, _Caller, State) -> +   {noreply, delete_cache(State, CacheName)}; +handle_call ({add, CacheName, Heir}, _Caller, State)-> +   {noreply, add_cache(State, CacheName, Heir)}; +handle_call ({inherit, CacheName, Heir}, _Caller, State)-> +   {noreply, inherit_cache(State, CacheName, Heir)}; +handle_call (terminate, _, State) -> +   {stop, normal, State}. + +handle_cast ({delete, CacheName}, State) -> +   {noreply, delete_cache(State, CacheName)}; +handle_cast ({add, CacheName, Heir}, State)-> +   {noreply, add_cache(State, CacheName, Heir)}; +handle_cast ({inherit, CacheName, Heir}, State)-> +   {noreply, inherit_cache(State, CacheName, Heir)}; +handle_cast (terminate, State) -> +   {stop, normal, State}. + +terminate (_Reason, []) -> +   ok; +terminate (Reason, [CacheName|OtherCaches]) -> +   delete_cache(CacheName), +   terminate(Reason, OtherCaches). + +code_change (_, State, _) -> +   {ok, State}. + +format_status (_, [_, State]) -> +   [{data, [{"State", State}]}]. + +handle_info(_, State) -> +   {noreply, State}. + +%%%% actual interface +delete_cache (CacheList, DB) -> +   case lists:member(DB, CacheList) of +      true -> +         delete_cache(DB), +         lists:delete(DB, CacheList); +      false -> +         CacheList +   end. + +add_cache (CacheList, DB, Heir) -> +   case lists:member(DB, CacheList) of +      true when (Heir =:= none) -> +         CacheList; + +      true -> +         ets:setopts(DB, {heir, Heir, DB}), +         CacheList; + +      false -> +         add_cache(DB, Heir), +         [DB|CacheList] +   end. + +get_timeout(battlemap_db) -> +   60000; +get_timeout(_) -> +   60000. | 


