我正在将数据移入和移出 dets,并且我有一个选择:我可以:
1)在访问它之前立即打开dets并在之后立即关闭它,或者
%% Based on Armstrong, Programming Erlang, page 279
open() ->
File = ?NAMEDB,
case dets:open_file(?MODULE, [{file, File}, {keypos,2}]) of
{ok, ?MODULE} ->
io:format("dets opened:~p~n", [File]);
{error,_Reason} ->
io:format("cannot open dets table~n"),
exit(eDetsOpen)
end.
%% Open db, get record, close db, return name
name(Record) ->
open(),
#name{honorific=Honorific, fname=FName, mname=MName, lname=LName, suffix=Suffix} = Record,
close(),
format_name([Honorific, FName, MName, LName, Suffix]).
2) 将 dets 链接到在发生崩溃时重新打开它的主管;例如,通过带有主管的 gen-server 访问 dets,例如:
start_link() ->
supervisor:start_link({local, ?SERVER}, ?MODULE, []).
start_child(Value, LeaseTime) ->
supervisor:start_child(?SERVER, [Value, LeaseTime]).
init([]) ->
Names = {lit_names, {lit_names, start_link, []},
temporary, brutal_kill, worker, [zpt_gridz_scratchpad]},
Children = [Names],
RestartStrategy = {simple_one_for_one, 0, 1},
{ok, {RestartStrategy, Children}}.
哪个最好?或者还有更好的选择吗?
非常感谢,
LRP