Erlang gen_server和ets简单应用

时间:2022-06-01 12:59:57
%%Author:Lee
%%Desc:学生管理的小例子
%%gen_server

-module(stuManager).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

%% External exports
-export([start/0,add_User/1,remove_User/1,find_User/1,show/0]).

%%学生数据信息
-record(userInfo,{id=1,name,sex,age}).

%%接口函数
%%添加学生 add_User
%%移除学生 remove_User
%%查询学生 find_User
%%显示所有学生 show

%%=======================================================

start()->
gen_server:start_link({local,?MODULE},?MODULE,[],[]).

%% --------------------------------------------------------------------
%% Function: init/1
%% Description: Initiates the server
%% Returns: {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% --------------------------------------------------------------------

init([])->
{ok,ets:new(?MODULE,[set,named_table,{keypos,3}])}.

%% --------------------------------------------------------------------
%% Function: handle_call/3
%% Description: Handling call messages
%% Returns: {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} | (terminate/2 is called)
%% {stop, Reason, State} (terminate/2 is called)
%% --------------------------------------------------------------------

handle_call({add,User},_From,_State)->
{A,B,C} = User,
New_Record=#userInfo{name=A,sex=B,age=C},
Reply = ets:insert(?MODULE,New_Record),
{reply,Reply,_State};
handle_call({remove,User},_From,_State)->
Reply = ets:delete(?MODULE,User),
{reply,Reply,_State};
handle_call({find,User},_From,_State)->
Reply = ets:lookup(?MODULE,User),
{reply,Reply,_State}.

%% --------------------------------------------------------------------
%% Function: handle_cast/2
%% Description: Handling cast messages
%% Returns: {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State} (terminate/2 is called)
%% --------------------------------------------------------------------

handle_cast(_Msg, State) ->
{noreply, State}.

%% --------------------------------------------------------------------
%% Function: handle_info/2
%% Description: Handling all non call/cast messages
%% Returns: {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State} (terminate/2 is called)
%% --------------------------------------------------------------------

handle_info(_Info,State)->
{noreply,State}.

%% --------------------------------------------------------------------
%% Function: terminate/2
%% Description: Shutdown the server
%% Returns: any (ignored by gen_server)
%% --------------------------------------------------------------------
terminate(_Reason, _State) ->
ok.

%% --------------------------------------------------------------------
%% Func: code_change/3
%% Purpose: Convert process state when code is changed
%% Returns: {ok, NewState}
%% --------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.

%% --------------------------------------------------------------------
%%% Internal functions
%% --------------------------------------------------------------------

add_User(User)->
gen_server:call(?MODULE,{add,User}).


remove_User(User)->
gen_server:call(?MODULE,{remove,User}).


find_User(User)->
gen_server:call(?MODULE,{find,User}).

show()->
List = ets:tab2list(?MODULE),
io:format("~13w => ~p~n",[set,List]).