update stats on cache eviction

This commit is contained in:
Dmitry Kolesnikov 2016-03-23 22:49:08 +02:00
parent c05be3b34e
commit bda14508ae
2 changed files with 18 additions and 9 deletions

View File

@ -201,9 +201,10 @@ handle_cast(_, State) ->
%%
handle_info(check_heap, #cache{n=N, check=Check}=State) ->
erlang:send_after(Check, self(), check_heap),
Heap = cache_heap:slip(State#cache.heap),
{Reason, Heap} = cache_heap:slip(State#cache.heap),
case cache_heap:size(Heap) of
X when X > N ->
cache_util:stats(State#cache.stats, {cache, State#cache.name, Reason}),
{noreply, State#cache{heap=cache_heap:drop(Heap, State#cache.heir)}};
_ ->
{noreply, State#cache{heap=Heap}}
@ -211,9 +212,10 @@ handle_info(check_heap, #cache{n=N, check=Check}=State) ->
handle_info(evict_heap, #cache{n=N, evict=Evict}=State) ->
erlang:send_after(Evict, self(), evict_heap),
Heap = cache_heap:slip(State#cache.heap),
{Reason, Heap} = cache_heap:slip(State#cache.heap),
case cache_heap:size(Heap) of
X when X > N ->
cache_util:stats(State#cache.stats, {cache, State#cache.name, Reason}),
{noreply, State#cache{heap=cache_heap:drop(Heap, State#cache.heir)}};
_ ->
{noreply, State#cache{heap=Heap}}

View File

@ -79,19 +79,26 @@ refs(#heap{segments=Refs}) ->
Refs.
%%
%% slip heap segments
-spec(slip/1 :: (#heap{}) -> #heap{}).
%% slip heap segments and report reason
-spec(slip/1 :: (#heap{}) -> {ok | ttl | oom | ooc , #heap{}}).
slip(#heap{}=Heap) ->
case is_expired(cache_util:now(), Heap) of
true ->
init(Heap);
false ->
Heap
false ->
{ok, Heap};
Reason ->
{Reason, init(Heap)}
end.
is_expired(Time, #heap{cardinality=C, memory=M, segments=[{Expire, Ref}|_]}) ->
Time >= Expire orelse ets:info(Ref, size) >= C orelse ets:info(Ref, memory) >= M.
case
{Time >= Expire, ets:info(Ref, size) >= C, ets:info(Ref, memory) >= M}
of
{true, _, _} -> ttl;
{_, true, _} -> ooc;
{_, _, true} -> oom;
_ -> false
end.
%%
%% drop last segment