25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-28 16:34:13 +01:00

Use new cache API in mod_mam

This commit is contained in:
Evgeniy Khramtsov 2017-04-20 18:52:16 +03:00
parent a26f90a346
commit 264a40f217

View File

@ -64,6 +64,9 @@
-callback select(binary(), jid(), jid(), mam_query:result(), -callback select(binary(), jid(), jid(), mam_query:result(),
#rsm_set{} | undefined, chat | groupchat) -> #rsm_set{} | undefined, chat | groupchat) ->
{[{binary(), non_neg_integer(), xmlel()}], boolean(), non_neg_integer()}. {[{binary(), non_neg_integer(), xmlel()}], boolean(), non_neg_integer()}.
-callback use_cache(binary(), gen_mod:opts()) -> boolean().
-optional_callbacks([use_cache/2]).
%%%=================================================================== %%%===================================================================
%%% API %%% API
@ -73,7 +76,7 @@ start(Host, Opts) ->
one_queue), one_queue),
Mod = gen_mod:db_mod(Host, Opts, ?MODULE), Mod = gen_mod:db_mod(Host, Opts, ?MODULE),
Mod:init(Host, Opts), Mod:init(Host, Opts),
init_cache(Opts), init_cache(Host, Opts),
register_iq_handlers(Host, IQDisc), register_iq_handlers(Host, IQDisc),
ejabberd_hooks:add(user_receive_packet, Host, ?MODULE, ejabberd_hooks:add(user_receive_packet, Host, ?MODULE,
user_receive_packet, 88), user_receive_packet, 88),
@ -108,15 +111,38 @@ start(Host, Opts) ->
ejabberd_commands:register_commands(get_commands_spec()), ejabberd_commands:register_commands(get_commands_spec()),
ok. ok.
init_cache(Opts) -> use_cache(Host, Opts) ->
Mod = gen_mod:db_mod(Host, Opts, ?MODULE),
case erlang:function_exported(Mod, use_cache, 2) of
true -> Mod:use_cache(Host, Opts);
false ->
gen_mod:get_opt(use_cache, Opts,
mod_opt_type(use_cache),
ejabberd_config:use_cache(Host))
end.
init_cache(Host, Opts) ->
case use_cache(Host, Opts) of
true ->
ets_cache:new(archive_prefs_cache, cache_opts(Host, Opts));
false ->
ok
end.
cache_opts(Host, Opts) ->
MaxSize = gen_mod:get_opt(cache_size, Opts, MaxSize = gen_mod:get_opt(cache_size, Opts,
fun(I) when is_integer(I), I>0 -> I end, mod_opt_type(cache_size),
1000), ejabberd_config:cache_size(Host)),
LifeTime = gen_mod:get_opt(cache_life_time, Opts, CacheMissed = gen_mod:get_opt(cache_missed, Opts,
fun(I) when is_integer(I), I>0 -> I end, mod_opt_type(cache_missed),
timer:hours(1) div 1000), ejabberd_config:cache_missed(Host)),
cache_tab:new(archive_prefs, [{max_size, MaxSize}, LifeTime = case gen_mod:get_opt(cache_life_time, Opts,
{life_time, LifeTime}]). mod_opt_type(cache_life_time),
ejabberd_config:cache_life_time(Host)) of
infinity -> infinity;
I -> timer:seconds(I)
end,
[{max_size, MaxSize}, {life_time, LifeTime}, {cache_missed, CacheMissed}].
stop(Host) -> stop(Host) ->
unregister_iq_handlers(Host), unregister_iq_handlers(Host),
@ -161,22 +187,7 @@ reload(Host, NewOpts, OldOpts) ->
true -> true ->
ok ok
end, end,
case gen_mod:is_equal_opt(cache_size, NewOpts, OldOpts, ets_cache:setopts(archive_prefs_cache, NewOpts),
fun(I) when is_integer(I), I>0 -> I end,
1000) of
{false, MaxSize, _} ->
cache_tab:setopts(archive_prefs, [{max_size, MaxSize}]);
true ->
ok
end,
case gen_mod:is_equal_opt(cache_life_time, NewOpts, OldOpts,
fun(I) when is_integer(I), I>0 -> I end,
timer:hours(1) div 1000) of
{false, LifeTime, _} ->
cache_tab:setopts(archive_prefs, [{life_time, LifeTime}]);
true ->
ok
end,
case gen_mod:is_equal_opt(iqdisc, NewOpts, OldOpts, case gen_mod:is_equal_opt(iqdisc, NewOpts, OldOpts,
fun gen_iq_handler:check_type/1, fun gen_iq_handler:check_type/1,
one_queue) of one_queue) of
@ -230,8 +241,8 @@ remove_user(User, Server) ->
LServer = jid:nameprep(Server), LServer = jid:nameprep(Server),
Mod = gen_mod:db_mod(LServer, ?MODULE), Mod = gen_mod:db_mod(LServer, ?MODULE),
Mod:remove_user(LUser, LServer), Mod:remove_user(LUser, LServer),
cache_tab:dirty_delete(archive_prefs, {LUser, LServer}, fun() -> ok end), ets_cache:delete(archive_prefs_cache, {LUser, LServer},
ok. ejabberd_cluster:get_nodes()).
-spec remove_room(binary(), binary(), binary()) -> ok. -spec remove_room(binary(), binary(), binary()) -> ok.
remove_room(LServer, Name, Host) -> remove_room(LServer, Name, Host) ->
@ -760,13 +771,17 @@ write_prefs(LUser, LServer, Host, Default, Always, Never) ->
always = Always, always = Always,
never = Never}, never = Never},
Mod = gen_mod:db_mod(Host, ?MODULE), Mod = gen_mod:db_mod(Host, ?MODULE),
cache_tab:dirty_insert( case Mod:write_prefs(LUser, LServer, Prefs, Host) of
archive_prefs, {LUser, LServer}, Prefs, ok ->
fun() -> Mod:write_prefs(LUser, LServer, Prefs, Host) end). ets_cache:delete(archive_prefs_cache, {LUser, LServer},
ejabberd_cluster:get_nodes());
_Err ->
{error, db_failure}
end.
get_prefs(LUser, LServer) -> get_prefs(LUser, LServer) ->
Mod = gen_mod:db_mod(LServer, ?MODULE), Mod = gen_mod:db_mod(LServer, ?MODULE),
Res = cache_tab:lookup(archive_prefs, {LUser, LServer}, Res = ets_cache:lookup(archive_prefs_cache, {LUser, LServer},
fun() -> Mod:get_prefs(LUser, LServer) end), fun() -> Mod:get_prefs(LUser, LServer) end),
case Res of case Res of
{ok, Prefs} -> {ok, Prefs} ->
@ -803,7 +818,7 @@ maybe_activate_mam(LUser, LServer) ->
case ActivateOpt of case ActivateOpt of
true -> true ->
Mod = gen_mod:db_mod(LServer, ?MODULE), Mod = gen_mod:db_mod(LServer, ?MODULE),
Res = cache_tab:lookup(archive_prefs, {LUser, LServer}, Res = ets_cache:lookup(archive_prefs_cache, {LUser, LServer},
fun() -> fun() ->
Mod:get_prefs(LUser, LServer) Mod:get_prefs(LUser, LServer)
end), end),