Skip to content

Commit 1c96d2e

Browse files
committed
Enable custom ETS table options
1 parent 2451a26 commit 1c96d2e

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

src/simple_cache_server.erl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
protected
1010
]).
1111

12-
-record(state, {table}).
12+
-record(state, {table, options}).
1313

1414
%%=============================================================================
1515
%% API Function Exports
1616
%%=============================================================================
1717
-export([start_link/0,
18+
start_link/1,
1819
ops_info/0,
1920
ops_list/0,
2021
sync_set/2,
@@ -43,6 +44,11 @@
4344
start_link() ->
4445
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
4546

47+
-spec start_link(list({atom(), any()}))
48+
-> 'ignore' | {'error', term()} | {'ok', pid()}.
49+
start_link(CustomOptions) ->
50+
gen_server:start_link({local, ?SERVER}, ?MODULE, [CustomOptions], []).
51+
4652
-spec ops_info() -> list().
4753
ops_info() ->
4854
gen_server:call(?MODULE, ops_info).
@@ -107,7 +113,12 @@ sync_flush() ->
107113
%% gen_server Function Definitions
108114
%%=============================================================================
109115
init([]) ->
110-
{ok, #state{table = ets:new(?SERVER, ?ETS_OPTIONS)}}.
116+
{ok, #state{table = ets:new(?SERVER, ?ETS_OPTIONS),
117+
options = ?ETS_OPTIONS}};
118+
init(CustomOptions) ->
119+
Options = merge_options(?ETS_OPTIONS, CustomOptions),
120+
{ok, #state{table = ets:new(?SERVER, Options),
121+
options = Options}}.
111122

112123
handle_call(ops_info, _From, #state{table = Table} = State) ->
113124
{reply, ets:info(Table), State};
@@ -172,3 +183,8 @@ get_by_key(Table, Key) ->
172183
[] ->
173184
{error, not_found}
174185
end.
186+
187+
merge_options(ExistingOptions, NewOptions) ->
188+
orddict:merge(fun (_, X, Y) -> X + Y end,
189+
orddict:from_list(ExistingOptions),
190+
orddict:from_list(NewOptions)).

0 commit comments

Comments
 (0)