You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@couchdb.apache.org by Benoit Chesneau <bc...@gmail.com> on 2011/01/27 14:07:50 UTC

Re: svn commit: r1064112 - /couchdb/trunk/src/couchdb/couch_auth_cache.erl

On Thu, Jan 27, 2011 at 1:49 PM,  <fd...@apache.org> wrote:
> Author: fdmanana
> Date: Thu Jan 27 12:49:34 2011
> New Revision: 1064112
>
> URL: http://svn.apache.org/viewvc?rev=1064112&view=rev
> Log:
> Avoid duplicated code and unnecessary generation of a list
>
> Modified:
>    couchdb/trunk/src/couchdb/couch_auth_cache.erl
>
> Modified: couchdb/trunk/src/couchdb/couch_auth_cache.erl
> URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_auth_cache.erl?rev=1064112&r1=1064111&r2=1064112&view=diff
> ==============================================================================
> --- couchdb/trunk/src/couchdb/couch_auth_cache.erl (original)
> +++ couchdb/trunk/src/couchdb/couch_auth_cache.erl Thu Jan 27 12:49:34 2011
> @@ -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(_) ->
> -            MruTime = ets:last(?BY_ATIME),
> -            [{MruTime, UserName}] = ets:lookup(?BY_ATIME, MruTime),
> -            true = ets:delete(?BY_ATIME, MruTime),
> -            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) ->
> @@ -232,6 +224,8 @@ clear_cache(State) ->
>     State#state{cache_size = 0}.
>
>
> +add_cache_entry(_, _, _, #state{max_cache_size = 0} = State) ->
> +    State;
>  add_cache_entry(UserName, Credentials, ATime, State) ->
>     case State#state.cache_size >= State#state.max_cache_size of
>     true ->
> @@ -243,16 +237,17 @@ add_cache_entry(UserName, Credentials, A
>     true = ets:insert(?BY_USER, {UserName, {Credentials, ATime}}),
>     State#state{cache_size = couch_util:get_value(size, ets:info(?BY_USER))}.
>
> +free_mru_cache_entries(0) ->
> +    ok;
> +free_mru_cache_entries(N) when N > 0 ->
> +    free_mru_cache_entry(),
> +    free_mru_cache_entries(N - 1).
>
>  free_mru_cache_entry() ->
> -    case ets:last(?BY_ATIME) of
> -    '$end_of_table' ->
> -        ok;  % empty cache
> -    MruTime ->
> -        [{MruTime, UserName}] = ets:lookup(?BY_ATIME, MruTime),
> -        true = ets:delete(?BY_ATIME, MruTime),
> -        true = ets:delete(?BY_USER, UserName)
> -    end.
> +    MruTime = ets:last(?BY_ATIME),
> +    [{MruTime, UserName}] = ets:lookup(?BY_ATIME, MruTime),
> +    true = ets:delete(?BY_ATIME, MruTime),
> +    true = ets:delete(?BY_USER, UserName).
>
>
>  cache_hit(UserName, Credentials, ATime) ->
>
>
>

Patch looks OK.

However I'm not sure that if someone set a new size > cache_size then
Count is < 0 and will create an error. I will put the guard on top and
match (_) for that.

-benoît

Re: svn commit: r1064112 - /couchdb/trunk/src/couchdb/couch_auth_cache.erl

Posted by Benoit Chesneau <bc...@gmail.com>.
On Thu, Jan 27, 2011 at 2:11 PM, Filipe David Manana
<fd...@apache.org> wrote:
> On Thu, Jan 27, 2011 at 1:07 PM, Benoit Chesneau <bc...@gmail.com> wrote:
>>
>> However I'm not sure that if someone set a new size > cache_size then
>> Count is < 0 and will create an error. I will put the guard on top and
>> match (_) for that.
>
> handle_call({new_max_cache_size, NewSize},
>        _From, #state{cache_size = Size} = State) when NewSize >= Size ->
>    {reply, ok, State#state{max_cache_size = NewSize}};
>
> handle_call({new_max_cache_size, NewSize}, _From, State) ->
>    free_mru_cache_entries(State#state.cache_size - NewSize),
>    {reply, ok, State#state{max_cache_size = NewSize, cache_size = NewSize}};
>
> Please read the surrounding code :)
>

I missed that, my bad :) Sorry for that.

- benoît

Re: svn commit: r1064112 - /couchdb/trunk/src/couchdb/couch_auth_cache.erl

Posted by Filipe David Manana <fd...@apache.org>.
On Thu, Jan 27, 2011 at 1:07 PM, Benoit Chesneau <bc...@gmail.com> wrote:
>
> However I'm not sure that if someone set a new size > cache_size then
> Count is < 0 and will create an error. I will put the guard on top and
> match (_) for that.

handle_call({new_max_cache_size, NewSize},
        _From, #state{cache_size = Size} = State) when NewSize >= Size ->
    {reply, ok, State#state{max_cache_size = NewSize}};

handle_call({new_max_cache_size, NewSize}, _From, State) ->
    free_mru_cache_entries(State#state.cache_size - NewSize),
    {reply, ok, State#state{max_cache_size = NewSize, cache_size = NewSize}};

Please read the surrounding code :)

>
> -benoît
>



-- 
Filipe David Manana,
fdmanana@gmail.com, fdmanana@apache.org

"Reasonable men adapt themselves to the world.
 Unreasonable men adapt the world to themselves.
 That's why all progress depends on unreasonable men."