summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/timed_cache_manager.erl76
-rw-r--r--src/timed_caches_manager.erl115
2 files changed, 115 insertions, 76 deletions
diff --git a/src/timed_cache_manager.erl b/src/timed_cache_manager.erl
deleted file mode 100644
index 45b013c..0000000
--- a/src/timed_cache_manager.erl
+++ /dev/null
@@ -1,76 +0,0 @@
--module(timed_cache_manager).
--export(
- [
- new/2,
- start/1,
- fetch/2
- ]
-).
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%% LOCAL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
-%%%% Manager %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-manager_core_loop (DB) ->
- receive
- terminate -> ets:delete(DB)
- end.
-
-new_database (DB) ->
- ets:new(
- DB,
- [
- set,
- public,
- {keypos, 1},
- {read_concurrency, true}
- ]
- ).
-
-%%%% Timer %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-timer_cleanup (DB, ObjectID) ->
- ets:delete(DB, ObjectID).
-
-timer_core_loop (DB, ObjectID, Timeout) ->
- receive
- ok -> timer_core_loop(DB, ObjectID, Timeout);
- terminate -> ok
- after Timeout ->
- timer_cleanup(DB, ObjectID)
- end.
-
-add_timer (DB, ObjectID, Timeout) ->
- spawn(timed_cache_manager, timer_core_loop, [DB, ObjectID, Timeout]).
-
-add_to_cache (DB, ObjectID) ->
- TimerPID = add_timer(DB, ObjectID, 60000),
- Data = nothing, %% Do the actual NoSQL Fetch here.
- ets:insert(DB, {ObjectID, TimerPID, Data}),
- Data.
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%% EXPORTED %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-new (Manager, DB) ->
- register(
- Manager,
- spawn(
- timed_cache_manager,
- start,
- [DB]
- )
- ).
-
-start (DB) ->
- new_database(DB),
- manager_core_loop(DB).
-
-fetch (DB, ObjectID) ->
- case ets:lookup(DB, ObjectID) of
- [] ->
- add_to_cache(DB, ObjectID);
-
- [{_, TimerPID, Data}] ->
- TimerPID ! ok,
- Data
- end.
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}.