-
1)to_list的方法
to_list(Msg) when is_list(Ms) -> Msg; to_list(Msg) when is_atom(Ms) -> atom_to_list(Msg); to_list(Msg) when is_binary(Ms) -> binary_to_list(Msg); to_list(Msg) when is_integer(Ms) -> integer_to_list(Msg); to_list(Msg) when is_tuple(Ms) -> 2 tuple_to_list(Msg);
-
2)序列化、反序列化
列表变成字符串存储(序列 化、反序列化),db模块的format_sql会把<<"[{a},1]">>的外层去掉,取的时候也是得到<<"">>,如果是int拿到的也是int无需转换%%----序列化: [{a},1] => "[{a},1]" => <<"[{a},1]">> term_to_bitstring(Term) -> erlang:list_to_bitstring(io_lib:format("~w",[Term])). %%----反序列化: "<<[{a},1]>>" => "[{a},1]" => [{a},1] bitstring_to_term(BitString) -> string_to_term(binary_to_list(BitString)). string_to_term(String)-> case erl_scan:string(String ++ ".") of {ok, Tokens, _} -> case erl_parse:parse_term(Tokens) of {ok, Term} -> Term; _Err -> undefined end; _Error -> undefined end. ```
-
3).ets缓存
- 读数据:先读ets,没有的话读db
- 取数据:查询db,然后把db上要保存的数据提取出来,一起加上存到ets(ets:insert)和db(replace into)
Sql = <<"replace into `process_adventure_beasts` (`center_server_id, `open_state`, `group_list`, `adventure_game_list``) values (~p, ~p, ~s, ~s)">> db:insert(Sql, CenterServerId, OpenState, util:term_to_bitstring(GroupList), util:term_to_bitstring(AadventureGamelist1)). ```
- 普通的逻辑一般是要存之前,查询下db/ETS里面有没有
- 数据库list字段设置为text,防止数据过大
-
4)sql,增加字段
alter tablefashion
addframe_id
int not null default ‘0’ comment ‘xxx’ afterweapon_id
; -
5)进程是否存活
- erlang:is_process_alive(Pid) True/false
- spawn(fun()->end).
- 进程退出 Catch {‘EXIT’, Reason}
- 设置process_flag(trap_exit, true), 可以让进程不再服从外来的退出信号,而是将之捕捉,{‘EXIT’,Pid,Reason},丢入信箱,一版这类进程叫做系统进程,监督者 -
6)任一进程非正常退出,会给其link的进程集发出exit信号,exit信号将像多米诺骨牌一样传递出去(每张牌就是一个进程,倒掉代表进程被结束)。可以理解成:调用process_flag(trap_exit,true)后,进程将收到的其它进程exit信息转换成{‘EXIT’, Pid, Reason}消息,从而制止了多米诺骨牌的继续倒掉;一般的报错是{‘EXIT’, _R}
-
7)大神blog常用总结:http://blog.sina.com.cn/s/blog_a78beb1f0102uzno.html
-
8)缓存数据存储:先从缓存里面读,如果没有就读数据库,数据落地的时候保证缓存和数据库都同时更新最新的覆盖
-
9)erlang如何连接mysql https://blog.youkuaiyun.com/ifkirin/article/details/52067486
-
10)dao生成的数据有写好的fix record,自己写的set_data。get_data没有fix,所以改数据字段需要清理dets
-
11)堆栈:erlang:process_info(self(), [current_stacktrace]).
-
12)缓存策略,大神博客http://blog.chinaunix.net/uid-429659-id-5750769.html