Move expiration functions to rabbit_auth_cache

This commit is contained in:
Daniil Fedotov 2016-06-02 11:24:45 +01:00
parent dd9db448f9
commit be1bea1f99
4 changed files with 23 additions and 34 deletions

View File

@ -16,6 +16,8 @@
-module(rabbit_auth_cache).
-export([expiration/1, expired/1]).
-ifdef(use_specs).
-callback get(term()) -> term().
@ -35,3 +37,8 @@ behaviour_info(_Other) ->
-endif.
expiration(TTL) ->
time_compat:erlang_system_time(milli_seconds) + TTL.
expired(Exp) ->
time_compat:erlang_system_time(milli_seconds) > Exp.

View File

@ -33,7 +33,7 @@ start_link() -> gen_server2:start_link({local, ?MODULE}, ?MODULE, [], []).
get(Key) -> gen_server2:call(?MODULE, {get, Key}).
put(Key, Value, TTL) ->
Expiration = expiration(TTL),
Expiration = rabbit_auth_cache:expiration(TTL),
gen_server2:cast(?MODULE, {put, Key, Value, TTL, Expiration}).
delete(Key) -> gen_server2:call(?MODULE, {delete, Key}).
@ -43,7 +43,7 @@ init(_Args) ->
handle_call({get, Key}, _From, State = #state{cache = Table}) ->
Result = case ets:lookup(Table, Key) of
[{Key, {Exp, Val}}] -> case expired(Exp) of
[{Key, {Exp, Val}}] -> case rabbit_auth_cache:expired(Exp) of
true -> {error, not_found};
false -> {ok, Val}
end;
@ -78,9 +78,3 @@ do_delete(Key, Table, Timers) ->
true = ets:delete(Timers, Key);
[] -> ok
end.
expiration(TTL) ->
time_compat:erlang_system_time(milli_seconds) + TTL.
expired(Exp) ->
time_compat:erlang_system_time(milli_seconds) > Exp.

View File

@ -40,7 +40,7 @@ get(Key) ->
end.
put(Key, Value, TTL) ->
Expiration = expiration(TTL),
Expiration = rabbit_auth_cache:expiration(TTL),
Segment = gen_server2:call(?MODULE, {get_write_segment, Expiration}),
ets:insert(Segment, {Key, {Expiration, Value}}),
ok.
@ -57,7 +57,7 @@ gc() ->
init([SegmentSize]) ->
InitSegment = ets:new(segment, [set, public]),
InitBoundary = expiration(SegmentSize),
InitBoundary = rabbit_auth_cache:expiration(SegmentSize),
{ok, GCTimer} = timer:send_interval(SegmentSize * 2, gc),
{ok, #state{gc_timer = GCTimer, segment_size = SegmentSize,
segments = [{InitBoundary, InitSegment}]}}.
@ -76,9 +76,8 @@ handle_cast(_, State = #state{}) ->
{noreply, State}.
handle_info(gc, State = #state{ segments = Segments }) ->
Now = time_compat:erlang_system_time(milli_seconds),
{Expired, Valid} = lists:partition(
fun({Boundary, _}) -> Now > Boundary end,
fun({Boundary, _}) -> rabbit_auth_cache:expired(Boundary) end,
Segments),
[ets:delete(Table) || {_, Table} <- Expired],
{noreply, State#state{ segments = Valid }};
@ -104,12 +103,6 @@ maybe_add_segment(Boundary, OldSegments) ->
new_boundary(Expiration, SegmentSize) ->
((Expiration div SegmentSize) * SegmentSize) + SegmentSize.
expiration(TTL) ->
time_compat:erlang_system_time(milli_seconds) + TTL.
expired(Exp) ->
time_compat:erlang_system_time(milli_seconds) > Exp.
get_from_segments(Key) ->
Tables = gen_server2:call(?MODULE, get_segment_tables),
lists:flatmap(
@ -117,7 +110,7 @@ get_from_segments(Key) ->
(T) ->
case ets:lookup(T, Key) of
[{Key, {Exp, Val}}] ->
case expired(Exp) of
case rabbit_auth_cache:expired(Exp) of
true -> [];
false -> [Val]
end;

View File

@ -39,7 +39,7 @@ get(Key) ->
end.
put(Key, Value, TTL) ->
Expiration = expiration(TTL),
Expiration = rabbit_auth_cache:expiration(TTL),
[{_, SegmentSize}] = ets:lookup(?SEGMENT_TABLE, segment_size),
Segment = segment(Expiration, SegmentSize),
Table = case ets:lookup(?SEGMENT_TABLE, Segment) of
@ -63,7 +63,7 @@ init([SegmentSize]) ->
ets:new(?SEGMENT_TABLE, [ordered_set, named_table, public]),
ets:insert(?SEGMENT_TABLE, {segment_size, SegmentSize}),
InitSegment = segment(expiration(SegmentSize), SegmentSize),
InitSegment = segment(rabbit_auth_cache:expiration(SegmentSize), SegmentSize),
do_add_segment(InitSegment),
{ok, GCTimer} = timer:send_interval(SegmentSize * 2, gc),
@ -93,9 +93,6 @@ terminate(_Reason, State = #state{gc_timer = Timer}) ->
timer:cancel(Timer),
State.
expiration(TTL) ->
time_compat:erlang_system_time(milli_seconds) + TTL.
segment(Expiration, SegmentSize) ->
Begin = ((Expiration div SegmentSize) * SegmentSize),
End = Begin + SegmentSize,
@ -112,25 +109,23 @@ do_add_segment(Segment) ->
Table
end.
expired(Exp) ->
time_compat:erlang_system_time(milli_seconds) > Exp.
get_segment_tables(_Now) ->
get_segment_tables() ->
get_all_segment_tables().
% get_matching_segment_tables(Now).
% Now = time_compat:erlang_system_time(milli_seconds),
% MatchSpec = [{{'$1', '$2'}, [{'>', '$1', {const, Now}}], ['$_']}],
% [V || {K, V} <- ets:select(?SEGMENT_TABLE, MatchSpec), K =/= segment_size].
get_all_segment_tables() ->
[V || {K, V} <- ets:tab2list(?SEGMENT_TABLE), K =/= segment_size].
get_from_segments(Key) ->
Now = time_compat:erlang_system_time(milli_seconds),
Tables = get_segment_tables(Now),
Tables = get_segment_tables(),
lists:flatmap(
fun(undefined) -> [];
(T) ->
case ets:lookup(T, Key) of
[{Key, {Exp, Val}}] ->
case expired(Exp) of
case rabbit_auth_cache:expired(Exp) of
true -> [];
false -> [Val]
end;