mirror of
https://github.com/valitydev/cache.git
synced 2024-11-06 01:45:19 +00:00
Add OTP behavior
This commit is contained in:
parent
20544f1bd1
commit
81ce0ebdf1
@ -1,7 +1,7 @@
|
||||
{application, cache,
|
||||
[
|
||||
{description, "in-memory cache"},
|
||||
{vsn, "0.1.0"},
|
||||
{vsn, "git"},
|
||||
{modules, [
|
||||
cache,
|
||||
cache_sup,
|
||||
|
@ -1,49 +1,59 @@
|
||||
-module(cache).
|
||||
-export([start/0]).
|
||||
-export([start_link/2, i/1, put/3, get/2, evict/1, stop/1]).
|
||||
|
||||
-export([
|
||||
start_link/2,
|
||||
i/0,
|
||||
i/1,
|
||||
put/2,
|
||||
put/3,
|
||||
get/1,
|
||||
get/2,
|
||||
evict/0,
|
||||
evict/1,
|
||||
stop/0,
|
||||
stop/1
|
||||
]).
|
||||
|
||||
start() ->
|
||||
AppFile = code:where_is_file(atom_to_list(?MODULE) ++ ".app"),
|
||||
{ok, [{application, _, List}]} = file:consult(AppFile),
|
||||
Apps = proplists:get_value(applications, List, []),
|
||||
lists:foreach(
|
||||
fun(X) ->
|
||||
ok = case application:start(X) of
|
||||
{error, {already_started, X}} -> ok;
|
||||
Ret -> Ret
|
||||
end
|
||||
end,
|
||||
Apps
|
||||
),
|
||||
application:start(?MODULE).
|
||||
|
||||
-define(NAME, cache).
|
||||
|
||||
%%
|
||||
%%
|
||||
start_link(Opts) ->
|
||||
start_link(?NAME, Opts).
|
||||
start_link(Name, Opts) ->
|
||||
cache_bucket:start_link(Name, Opts).
|
||||
|
||||
i() ->
|
||||
i(?NAME).
|
||||
|
||||
i(Cache) ->
|
||||
cache_bucket:i(Cache).
|
||||
|
||||
%%
|
||||
%%
|
||||
put(Key, Val) ->
|
||||
put(?NAME, Key, Val).
|
||||
put(Cache, Key, Val) ->
|
||||
cache_bucket:put(Cache, Key, Val).
|
||||
|
||||
%%
|
||||
%%
|
||||
get(Key) ->
|
||||
get(?NAME, Key).
|
||||
get(Cache, Key) ->
|
||||
cache_bucket:get(Cache, Key).
|
||||
|
||||
%%
|
||||
%%
|
||||
evict() ->
|
||||
evict(?NAME).
|
||||
evict(Cache) ->
|
||||
cache_bucket:evict(Cache).
|
||||
|
||||
%%
|
||||
%%
|
||||
stop() ->
|
||||
stop(?NAME).
|
||||
stop(Cache) ->
|
||||
cache_bucket:stop(Cache).
|
||||
|
||||
|
@ -2,18 +2,33 @@
|
||||
-module(cache_sup).
|
||||
-behaviour(supervisor).
|
||||
-author('Dmitry Kolesnikov <dmkolesnikov@gmail.com>').
|
||||
-author('Jose Luis Navarro <jlnavarro@gmail.com>').
|
||||
|
||||
-export([start_link/0, init/1]).
|
||||
%% API
|
||||
-export([start_link/0]).
|
||||
|
||||
%% Supervisor callbacks
|
||||
-export([init/1]).
|
||||
|
||||
%% Helper macro for declaring children of supervisor
|
||||
-define(CHILD(I, Type, Params), {I, {I, start_link, Params}, permanent, 5000, Type, [I]}).
|
||||
|
||||
%% ===================================================================
|
||||
%% API functions
|
||||
%% ===================================================================
|
||||
|
||||
%%
|
||||
%%
|
||||
start_link() ->
|
||||
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
||||
|
||||
init([]) ->
|
||||
{ok,
|
||||
%% ===================================================================
|
||||
%% Supervisor callbacks
|
||||
%% ===================================================================
|
||||
|
||||
init([]) ->
|
||||
Params = [],
|
||||
{ok,
|
||||
{
|
||||
{one_for_one, 4, 1800},
|
||||
[]
|
||||
[?CHILD(cache, worker, [Params])]
|
||||
}
|
||||
}.
|
||||
}.
|
Loading…
Reference in New Issue
Block a user