erlang gen_server实例

首先是gen_server回调模块实现:

 1 %% @author Administrator
 2 %% @doc @todo Add description to db_s.
 3 
 4 
 5 -module(db_s).
 6 %%-export([start/0,stop/0,write/2,delete/1,read/1,match/1,all/0]).
 7 %% ====================================================================
 8 %% API functions
 9 %% ====================================================================
10 -compile(export_all).
11 -behaviour(gen_server).
12 %% ====================================================================
13 %% External functions
14 %% ====================================================================
15 start() ->
16     start_link().
17 
18 stop() ->
19     gen_server:cast(?MODULE, stop).
20 
21 write(Key,Element) ->
22     gen_server:call(?MODULE, {write,Key,Element}).
23 
24 delete(Key) ->
25     gen_server:call(?MODULE, {delete,Key}).
26 
27 read(Key) ->
28     gen_server:call(?MODULE, {read,Key}).
29 
30 match(Element) ->
31     gen_server:call(?MODULE, {match,Element}).
32 
33 all() ->
34     gen_server:call(?MODULE, all).
35 
36 
37 %% ====================================================================
38 %% Server functions
39 %% ====================================================================
40 
41 
42 start_link() ->
43     gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
44 
45 init([]) ->
46     {ok,[]}.
47 
48 handle_cast(stop,LoopData) ->
49     {stop,normal,LoopData}.
50 
51 handle_call({write,Key,Element},_From,LoopData) ->
52     NewLoopData = db:write(Key, Element, LoopData),
53     Reply = ok,
54     {reply,Reply,NewLoopData};
55 
56 handle_call(all,_From,LoopData) ->
57     db:all(LoopData),
58     Reply = ok,
59     {reply,Reply,LoopData};
60 
61 handle_call({delete,Key},_From,LoopData) ->
62     NewLoopData = db:delete(Key,LoopData),
63     Reply = ok,
64     {reply,Reply,NewLoopData};
65 
66 handle_call({read,Key},_From,LoopData) ->
67     db:read(Key,LoopData),
68     Reply = ok,
69     {reply,Reply,LoopData};
70 
71 handle_call({match,Element},_From,LoopData) ->
72     db:match(Element, LoopData),
73     Reply = ok,
74     {reply,Reply,LoopData}.
75 
76 terminate(_Reason, _State) ->
77     ok.
 

其次是功能实现:

 1 %% @author Administrator
 2 %% @doc @todo Add description to db_4.
 3 
 4 
 5 -module(db).
 6 
 7 %% ====================================================================
 8 %% API functions
 9 %% ====================================================================
10 -compile(export_all).
11 %%-export([]).
12 
13 new() ->
14     [].
15 
16 write(Key,Element,Db) ->
17     io:format("~w~n",[[Key,Element]]),
18     [{Key,Element}|Db].
21 
22 read(Key,[{K,E}|D]) when K == Key ->
23     io:format("~w~n",[[ok,E]]),
24     read(Key,D);
25 read(Key,[L|D])  when D  /= []  ->
26     read(Key,D);
27 read(_,_) ->
28     finish.
29     
30 
31 match(Element,[{K,E}|D]) when Element == E ->
32     io:format("~w~n",[[ok,K]]),
33     match(Element,D);
34 match(Element,[L|D])  when D  /= []  ->
35     match(Element,D);
36 match(_,_) ->
37     finish.
38 
39 
40 delete(Key,Db) ->
41     lists:keydelete(Key,1,Db).
42     
43 
44 all(L) ->
45     io:format("~w~n",[{all,L}]).
46 
47 %% ====================================================================
48 %% Internal functions
49 %% ====================================================================

 

 

转载于:https://www.cnblogs.com/study-development/archive/2013/03/26/2982135.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值