mirror of
https://github.com/valitydev/cache.git
synced 2024-11-06 01:45:19 +00:00
update stats on cache eviction
This commit is contained in:
parent
c05be3b34e
commit
bda14508ae
@ -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}}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user