On Thu, Jan 27, 2011 at 12:25 PM, Benoit Chesneau <[email protected]> wrote:
> here is a simpler patch, that just check if Counc is > 1:
If NewSize is equal to CurrentSize - N, you'll not free N - 1 cache
entries and not N (as it must be). So it must be "when Count > 0", not
1.
And again, I don't like repeating the code which does the removal in
another new function.
Alternative approach commited, revision 1064112
>
> ---
> src/couchdb/couch_auth_cache.erl | 22 ++++++++++++----------
> 1 files changed, 12 insertions(+), 10 deletions(-)
>
> diff --git a/src/couchdb/couch_auth_cache.erl
> b/src/couchdb/couch_auth_cache.erl
> index 033e160..f14b91e 100644
> --- a/src/couchdb/couch_auth_cache.erl
> +++ b/src/couchdb/couch_auth_cache.erl
> @@ -169,15 +169,7 @@ handle_call({new_max_cache_size, NewSize},
> {reply, ok, State#state{max_cache_size = NewSize}};
>
> handle_call({new_max_cache_size, NewSize}, _From, State) ->
> - lists:foreach(
> - fun(_) ->
> - LruTime = ets:last(?BY_ATIME),
> - [{LruTime, UserName}] = ets:lookup(?BY_ATIME, LruTime),
> - true = ets:delete(?BY_ATIME, LruTime),
> - true = ets:delete(?BY_USER, UserName)
> - end,
> - lists:seq(1, State#state.cache_size - NewSize)
> - ),
> + free_mru_cache_entries(State#state.cache_size - NewSize),
> {reply, ok, State#state{max_cache_size = NewSize, cache_size = NewSize}};
>
> handle_call({fetch, UserName}, _From, State) ->
> @@ -223,7 +215,7 @@ terminate(_Reason, #state{db_notifier = Notifier}) ->
>
> code_change(_OldVsn, State, _Extra) ->
> {ok, State}.
> -
> +
>
> clear_cache(State) ->
> exec_if_auth_db(fun(AuthDb) -> catch couch_db:close(AuthDb) end),
> @@ -244,6 +236,16 @@ add_cache_entry(UserName, Credentials, ATime, State) ->
> State#state{cache_size = couch_util:get_value(size, ets:info(?BY_USER))}.
>
>
> +free_mru_cache_entries(Count) when Count > 1 ->
> + LruTime = ets:last(?BY_ATIME),
> + [{LruTime, UserName}] = ets:lookup(?BY_ATIME, LruTime),
> + true = ets:delete(?BY_ATIME, LruTime),
> + true = ets:delete(?BY_USER, UserName),
> + free_mru_cache_entries(Count-1);
> +free_mru_cache_entries(_) ->
> + ok.
> +
> +
> free_mru_cache_entry() ->
> case ets:last(?BY_ATIME) of
> '$end_of_table' ->
> --
> 1.7.3.4
>
--
Filipe David Manana,
[email protected], [email protected]
"Reasonable men adapt themselves to the world.
Unreasonable men adapt the world to themselves.
That's why all progress depends on unreasonable men."