nitrogen使用中session问题
nitrogen中的session在集群情况下会找不到,经常会出现重新登录情况,试图修改成为mnesia管理session,在节点超过一定数量时mnesia同步机制会出现同步慢的问题,为此决定再次修改为redis来管理session。
修改为redis管理session
1 在nitrogen中调用wf:session/2设置session时调用了session_handler:set_value/2,回调behaviour (session_handler),wf_handler:call/3,这里实现了一个简单的session_handler,只要实现自己的session_handler行为,就可以了,在wf:context中会配置具体调用哪些handler,只要修改make_handler(session_handler, simple_session_handler), %simple_session_handler), 为自己的实现即可。
2 我实现的redis_session_handler
- % Copyright (c) 2008-2010 Rusty Klophaus
- % See MIT-LICENSE for licensing information.
- % This is a "simple as possible" session handler. Unfortunately,
- % due to time constraints, had to leave out some great code
- % contributed by Dave Peticolas that fit Nitrogen sessions
- % into a gen_server. My code below is far inferior.
- % Someone please make it better! - Rusty
- -module (redis_session_handler).
- -include_lib ("wf.hrl").
- -behaviour (session_handler).
- -export ([
- init/2,
- finish/2,
- get_value/4,
- set_value/4,
- clear_all/2
- ]).
- -record (state, {unique}).
- init(_Config, _State) ->
- Cookie = wf:cookie(get_cookie_name()),
- State = case wf:depickle(Cookie) of
- undefined -> new_state();
- Other -> Other
- end,
- {ok, State}.
- finish(_Config, State) ->
- % Drop the session cookie...
- Timeout = wf:config_default(session_timeout, 20),
- ok = wf:cookie(get_cookie_name(), wf:pickle(State), "/", Timeout),
- {ok, []}.
- get_value(Key, DefaultValue, _Config, State) ->
- %Host = wf:config_default(erldis_host, "127.0.0.1"),
- %Port = wf:config_default(erldis_port, 6379),
- %{ok, Client} = erldis:connect(Host, Port),
- %io:format("~p",[?LINE]),
- %io:format("get_value, Key = ~p~n",[Key]),
- Client = erldis_sup:client(),
- Value = case erldis:hget(Client, State#state.unique, Key) of
- nil ->
- DefaultValue;
- Any ->
- binary_to_term(Any)
- end,
- %io:format("get_value, Key = ~p, Value = ~p~n",[Key, Value]),
- Timeout = wf:config_default(session_timeout, 20),
- erldis:expire(Client, State#state.unique, Timeout*60),
- {ok, Value, State}.
- set_value(Key, Value, _Config, State) ->
- %io:format("set_value, Key = ~p, Value = ~p~n",[Key, Value]),
- Client = erldis_sup:client(),
- OldValue = case erldis:hget(Client, State#state.unique, Key) of
- nil ->
- undefined;
- Any ->
- binary_to_term(Any)
- end,
- %io:format("~p",[?LINE]),
- %io:format("OldValue = ~n~p~n",[OldValue]),
- erldis:hset(Client, State#state.unique, Key, term_to_binary(Value)),
- Timeout = wf:config_default(session_timeout, 20),
- erldis:expire(Client, State#state.unique, Timeout*60),
- {ok, OldValue, State}.
- clear_all(_Config, State) ->
- Client = erldis_sup:client(),
- case erldis:del(Client, State#state.unique) of
- nil ->
- undefined;
- Any ->
- %io:format("~p",[?LINE]),
- %io:format("~n~p~n",[Any]),
- Any
- end,
- {ok, State}.
- %%% PRIVATE FUNCTIONS
- get_cookie_name() ->
- wf:config_default(cookie_name, "lxbssn").
- new_state() ->
- Unique = erlang:md5(term_to_binary({now(), erlang:make_ref()})),
- #state { unique=Unique }.
3 在上面代码中我使用了erldis作为redis的erlang客户端,经测试未发现问题,很稳定。