summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/timed_caches_manager.erl')
-rw-r--r--src/timed_caches_manager.erl115
1 files changed, 115 insertions, 0 deletions
diff --git a/src/timed_caches_manager.erl b/src/timed_caches_manager.erl
new file mode 100644
index 0000000..a2c95f3
--- /dev/null
+++ b/src/timed_caches_manager.erl
@@ -0,0 +1,115 @@
+-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
+ ]
+)
+.
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% LOCAL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%%% Manager %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+delete_cache (DB) ->
+ ets:delete(DB).
+
+add_cache (DB, Heir) ->
+ ets:new(
+ DB,
+ [
+ set,
+ public,
+ {keypos, 1},
+ {read_concurrency, true},
+ {heir, Heir, DB}
+ ]
+ ).
+
+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 ->
+ ets:setopts(DB, {heir, Heir, DB}),
+ CacheList;
+
+ false ->
+ add_cache(DB, Heir),
+ [DB|CacheList]
+ end.
+
+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) ->
+ {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}.