您可以使用gen_server:
-module(s3).
-behaviour(gen_server).
-export([start/1, stop/0, save/1]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-define(SERVER_NAME, ?MODULE).
-define(CALLBACK_MODULE, ?MODULE).
%%Interface functions:
start(BuffSize) ->
gen_server:start_link({local, ?SERVER_NAME}, ?CALLBACK_MODULE, [BuffSize], []).
stop() ->
gen_server:call(?CALLBACK_MODULE, stop).
save(Record) ->
gen_server:call(?CALLBACK_MODULE, {save, Record}).
persist(Records) ->
%%Code to save Records to db
io:format("BuffSize exceeded. Saving to database.~n").
%%Callback functions:
init([BuffSize]) ->
{ok, {BuffSize, 0, []} }.
handle_call({save, Record}, _From, {BuffSize, Count, Records}) ->
NewRecords = [Record|Records],
NewState =
case Count+1 > BuffSize of
true ->
persist(NewRecords),
{BuffSize, 0, []};
false ->
{BuffSize, Count+1, NewRecords}
end,
{reply, NewRecords, NewState};
handle_call(stop, _From, State) ->
{stop, normal, stopped, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
在外壳中:
1> c(s3).
s3.erl:48: Warning: variable 'Records' is unused
{ok,s3}
2> s3:start(3).
{ok,<0.64.0>}
3> s3:save(["John", 1]).
[["John",1]]
4> s3:save(["Sally", 2]).
[["Sally",2],["John",1]]
5> s3:save(["Joe", 3]).
[["Joe",3],["Sally",2],["John",1]]
6> s3:save(["Sue", 4]).
BuffSize exceeded. Saving to database.
[["Sue",4],["Joe",3],["Sally",2],["John",1]]
7> s3:save(["Jill", 5]).
[["Jill",5]]
8>