Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Trigger garbage collection after ?GC_TIMEOUT milliseconds of inactivity

  • Loading branch information...
commit 43e5adf7d14acd3396546183996c3599f4426a04 1 parent 436ae44
@fdmanana authored
View
21 src/term_cache_dict.erl
@@ -35,6 +35,7 @@
-define(DEFAULT_POLICY, lru).
-define(DEFAULT_SIZE, "128Kb"). % bytes
-define(DEFAULT_TTL, 0). % 0 means no TTL
+-define(GC_TIMEOUT, 5000).
-record(state, {
cache_size,
@@ -155,7 +156,7 @@ handle_cast({put, Key, Item, ItemSize}, State) ->
atimes = ATimes2,
free = Free - ItemSize
},
- {noreply, NewState};
+ {noreply, NewState, ?GC_TIMEOUT};
handle_cast({update, _Key, _NewItem, NewItemSize},
@@ -168,12 +169,12 @@ handle_cast({update, Key, NewItem, NewItemSize}, State) ->
{ok, {_OldItem, OldItemSize, ATime, Timer}} ->
case NewItemSize > (Free + OldItemSize) of
true ->
- {noreply, State};
+ {noreply, State, ?GC_TIMEOUT};
false ->
Items2 = dict:store(
Key, {NewItem, NewItemSize, ATime, Timer}, Items),
Free2 = Free - OldItemSize + NewItemSize,
- {noreply, State#state{items = Items2, free = Free2}}
+ {noreply, State#state{items = Items2, free = Free2}, ?GC_TIMEOUT}
end;
error ->
{noreply, State}
@@ -191,7 +192,7 @@ handle_cast(flush, #state{items = Items, cache_size = Size} = State) ->
atimes = gb_trees:empty(),
free = Size
},
- {noreply, NewState}.
+ {noreply, NewState, ?GC_TIMEOUT}.
handle_call({get, Key}, _From, State) ->
@@ -211,9 +212,9 @@ handle_call({get, Key}, _From, State) ->
atimes = ATimes2,
hits = Hits + 1
},
- {reply, {ok, Item}, NewState};
+ {reply, {ok, Item}, NewState, ?GC_TIMEOUT};
error ->
- {reply, not_found, State#state{misses = Misses + 1}}
+ {reply, not_found, State#state{misses = Misses + 1}, ?GC_TIMEOUT}
end;
@@ -229,7 +230,7 @@ handle_call(get_info, _From, State) ->
{size, CacheSize}, {free, Free}, {items, gb_trees:size(ATimes)},
{hits, Hits}, {misses, Misses}
],
- {reply, {ok, Info}, State};
+ {reply, {ok, Info}, State, ?GC_TIMEOUT};
handle_call(stop, _From, State) ->
@@ -244,7 +245,11 @@ handle_info({expired, Key}, State) ->
atimes = gb_trees:delete(ATime, ATimes),
free = Free + ItemSize
},
- {noreply, NewState}.
+ {noreply, NewState, ?GC_TIMEOUT};
+
+handle_info(timeout, State) ->
+ true = erlang:garbage_collect(),
+ {noreply, State}.
terminate(_Reason, _State) ->
View
20 src/term_cache_ets.erl
@@ -34,6 +34,7 @@
-define(DEFAULT_POLICY, lru).
-define(DEFAULT_SIZE, "128Kb"). % bytes
-define(DEFAULT_TTL, 0). % 0 means no TTL
+-define(GC_TIMEOUT, 5000).
-record(state, {
cache_size,
@@ -149,7 +150,7 @@ handle_cast({put, Key, Item, ItemSize}, State) ->
Timer = set_timer(Key, Ttl),
true = ets:insert(ATimes, {Now, Key}),
true = ets:insert(Items, {Key, {Item, ItemSize, Now, Timer}}),
- {noreply, State#state{free = Free - ItemSize}};
+ {noreply, State#state{free = Free - ItemSize}, ?GC_TIMEOUT};
handle_cast({update, _Key, _NewItem, NewItemSize},
@@ -166,7 +167,8 @@ handle_cast({update, Key, NewItem, NewItemSize}, State) ->
false ->
true = ets:insert(
Items, {Key, {NewItem, NewItemSize, ATime, Timer}}),
- {noreply, State#state{free = Free - OldItemSize + NewItemSize}}
+ Free2 = Free - OldItemSize + NewItemSize,
+ {noreply, State#state{free = Free2}, ?GC_TIMEOUT}
end;
[] ->
{noreply, State}
@@ -182,7 +184,7 @@ handle_cast(flush,
),
true = ets:delete_all_objects(Items),
true = ets:delete_all_objects(ATimes),
- {noreply, State#state{free = Size}}.
+ {noreply, State#state{free = Size}, ?GC_TIMEOUT}.
handle_call({get, Key}, _From, State) ->
@@ -197,9 +199,9 @@ handle_call({get, Key}, _From, State) ->
true = ets:insert(ATimes, {NewATime, Key}),
NewTimer = set_timer(Key, Ttl),
true = ets:insert(Items, {Key, {Item, ItemSize, NewATime, NewTimer}}),
- {reply, {ok, Item}, State#state{hits = Hits + 1}};
+ {reply, {ok, Item}, State#state{hits = Hits + 1}, ?GC_TIMEOUT};
[] ->
- {reply, not_found, State#state{misses = Misses + 1}}
+ {reply, not_found, State#state{misses = Misses + 1}, ?GC_TIMEOUT}
end;
@@ -215,7 +217,7 @@ handle_call(get_info, _From, State) ->
{size, CacheSize}, {free, Free}, {items, ets:info(ATimes, size)},
{hits, Hits}, {misses, Misses}
],
- {reply, {ok, Info}, State};
+ {reply, {ok, Info}, State, ?GC_TIMEOUT};
handle_call(stop, _From, State) ->
@@ -227,7 +229,11 @@ handle_info({expired, Key}, State) ->
[{Key, {_Item, ItemSize, ATime, _Timer}}] = ets:lookup(Items, Key),
true = ets:delete(Items, Key),
true = ets:delete(ATimes, ATime),
- {noreply, State#state{free = Free + ItemSize}}.
+ {noreply, State#state{free = Free + ItemSize}, ?GC_TIMEOUT};
+
+handle_info(timeout, State) ->
+ true = erlang:garbage_collect(),
+ {noreply, State}.
terminate(_Reason, #state{items = Items, atimes = ATimes}) ->
View
25 src/term_cache_pdict.erl
@@ -34,6 +34,7 @@
-define(DEFAULT_POLICY, lru).
-define(DEFAULT_SIZE, "128Kb"). % bytes
-define(DEFAULT_TTL, 0). % 0 means no TTL
+-define(GC_TIMEOUT, 5000).
-record(state, {
cache_size,
@@ -150,7 +151,7 @@ handle_cast({put, Key, Item, ItemSize}, State) ->
atimes = ATimes2,
free = Free - ItemSize
},
- {noreply, NewState};
+ {noreply, NewState, ?GC_TIMEOUT};
handle_cast({update, _Key, _NewItem, NewItemSize},
@@ -165,7 +166,8 @@ handle_cast({update, Key, NewItem, NewItemSize}, #state{free = Free} = State) ->
{noreply, State};
false ->
erlang:put({key, Key}, {item, NewItem, NewItemSize, ATime, Timer}),
- {noreply, State#state{free = Free - OldItemSize + NewItemSize}}
+ Free2 = Free - OldItemSize + NewItemSize,
+ {noreply, State#state{free = Free2}, ?GC_TIMEOUT}
end;
undefined ->
{noreply, State}
@@ -185,13 +187,14 @@ handle_cast(flush, #state{cache_size = Size} = State) ->
atimes = gb_trees:empty(),
free = Size
},
- {noreply, NewState}.
+ {noreply, NewState, ?GC_TIMEOUT}.
+
handle_call({get, Key}, _From,
#state{atimes = ATimes, ttl = T, hits = Hits, misses = Misses} = State) ->
case erlang:get({key, Key}) of
undefined ->
- {reply, not_found, State#state{misses = Misses + 1}};
+ {reply, not_found, State#state{misses = Misses + 1}, ?GC_TIMEOUT};
{item, Item, ItemSize, ATime, Timer} ->
cancel_timer(Key, Timer),
NewATime = erlang:now(),
@@ -199,7 +202,11 @@ handle_call({get, Key}, _From,
NewATime, Key, gb_trees:delete(ATime, ATimes)),
erlang:put(
{key, Key}, {item, Item, ItemSize, NewATime, set_timer(Key, T)}),
- {reply, {ok, Item}, State#state{atimes = ATimes2, hits = Hits + 1}}
+ NewState = State#state{
+ atimes = ATimes2,
+ hits = Hits + 1
+ },
+ {reply, {ok, Item}, NewState, ?GC_TIMEOUT}
end;
@@ -215,7 +222,7 @@ handle_call(get_info, _From, State) ->
{size, CacheSize}, {free, Free}, {items, gb_trees:size(ATimes)},
{hits, Hits}, {misses, Misses}
],
- {reply, {ok, Info}, State};
+ {reply, {ok, Info}, State, ?GC_TIMEOUT};
handle_call(stop, _From, State) ->
@@ -228,7 +235,11 @@ handle_info({expired, Key}, #state{atimes = ATimes, free = Free} = State) ->
atimes = gb_trees:delete(ATime, ATimes),
free = Free + ItemSize
},
- {noreply, NewState}.
+ {noreply, NewState, ?GC_TIMEOUT};
+
+handle_info(timeout, State) ->
+ true = erlang:garbage_collect(),
+ {noreply, State}.
terminate(_Reason, _State) ->
View
21 src/term_cache_trees.erl
@@ -34,6 +34,7 @@
-define(DEFAULT_POLICY, lru).
-define(DEFAULT_SIZE, "128Kb"). % bytes
-define(DEFAULT_TTL, 0). % 0 means no TTL
+-define(GC_TIMEOUT, 5000).
-record(state, {
cache_size,
@@ -154,7 +155,7 @@ handle_cast({put, Key, Item, ItemSize}, State) ->
atimes = ATimes2,
free = Free - ItemSize
},
- {noreply, NewState};
+ {noreply, NewState, ?GC_TIMEOUT};
handle_cast({update, _Key, _NewItem, NewItemSize},
@@ -167,12 +168,12 @@ handle_cast({update, Key, NewItem, NewItemSize}, State) ->
{value, {_OldItem, OldItemSize, ATime, Timer}} ->
case NewItemSize > (Free + OldItemSize) of
true ->
- {noreply, State};
+ {noreply, State, ?GC_TIMEOUT};
false ->
Items2 = gb_trees:update(
Key, {NewItem, NewItemSize, ATime, Timer}, Items),
Free2 = Free - OldItemSize + NewItemSize,
- {noreply, State#state{items = Items2, free = Free2}}
+ {noreply, State#state{items = Items2, free = Free2}, ?GC_TIMEOUT}
end;
none ->
{noreply, State}
@@ -189,7 +190,7 @@ handle_cast(flush, #state{items = Items, cache_size = Size} = State) ->
atimes = gb_trees:empty(),
free = Size
},
- {noreply, NewState}.
+ {noreply, NewState, ?GC_TIMEOUT}.
handle_call({get, Key}, _From, State) ->
@@ -209,9 +210,9 @@ handle_call({get, Key}, _From, State) ->
atimes = ATimes2,
hits = Hits + 1
},
- {reply, {ok, Item}, NewState};
+ {reply, {ok, Item}, NewState, ?GC_TIMEOUT};
none ->
- {reply, not_found, State#state{misses = Misses + 1}}
+ {reply, not_found, State#state{misses = Misses + 1}, ?GC_TIMEOUT}
end;
@@ -227,7 +228,7 @@ handle_call(get_info, _From, State) ->
{size, CacheSize}, {free, Free}, {items, gb_trees:size(ATimes)},
{hits, Hits}, {misses, Misses}
],
- {reply, {ok, Info}, State};
+ {reply, {ok, Info}, State, ?GC_TIMEOUT};
handle_call(stop, _From, State) ->
@@ -242,7 +243,11 @@ handle_info({expired, Key}, State) ->
atimes = gb_trees:delete(ATime, ATimes),
free = Free + ItemSize
},
- {noreply, NewState}.
+ {noreply, NewState, ?GC_TIMEOUT};
+
+handle_info(timeout, State) ->
+ true = erlang:garbage_collect(),
+ {noreply, State}.
terminate(_Reason, _State) ->
Please sign in to comment.
Something went wrong with that request. Please try again.