...
 
Commits (2)
......@@ -4,7 +4,7 @@
Copyright (c) 2016 Benoît Chesneau.
__Version:__ 2.3.0.
__Version:__ 2.3.1.
## Erlang LRU
......
......@@ -4,7 +4,7 @@
Copyright (c) 2016 Benoît Chesneau.
__Version:__ 2.3.0.
__Version:__ 2.3.1.
## Erlang LRU
......
......@@ -75,7 +75,7 @@ registered name.</td></tr><tr><td valign="top"><a href="#stop-1">stop/1</a></td>
### add/3 ###
<pre><code>
add(Cache::<a href="#type-cache">cache()</a>, Key::term(), Value::term()) -&gt; true | false
add(Cache::<a href="#type-cache">cache()</a>, Key::term(), Value::term() | function()) -&gt; true | false
</code></pre>
<br />
......@@ -97,7 +97,7 @@ like add but with a callback
### contains/2 ###
<pre><code>
contains(Cache::<a href="#type-cache">cache()</a>, Key::term()) -&gt; true | false
contains(Cache::<a href="#type-cache">cache()</a>, Key::term() | function()) -&gt; true | false
</code></pre>
<br />
......
@copyright 2016 Benoît Chesneau.
@version 2.3.0.
@version 2.3.1.
@title: Erlang LRU: a fixed size LRU cache.
@doc
......
{application, 'lru',
[{description, "implements a fixed-size LRU cache"},
{vsn, "2.3.0"},
{vsn, "2.3.1"},
{registered, []},
{applications,
[kernel,
......
......@@ -109,7 +109,7 @@ stop(Cache) ->
end.
%% @doc adds a value to the cache. Returns true if an eviction occured.
-spec add(cache(), term(), term()) -> true | false.
-spec add(cache(), term(), term() | fun()) -> true | false.
add(Cache, Key, Value) ->
Reply = call(Cache, {add, Key, Value}),
case Reply of
......@@ -152,7 +152,7 @@ peek(Cache, Key, Default) ->
call(Cache, {peek, Key, Default}).
%% @doc check if the key is in the cache
-spec contains(cache(), term()) -> true | false.
-spec contains(cache(), term() | fun()) -> true | false.
contains(Cache, Key) ->
call(Cache, {contains, Key}).
......@@ -255,7 +255,12 @@ init([Opts]) ->
%% @private
handle_call({add, Key, Value}, From, Cache) ->
do_add(Key, Value, From, Cache);
case value_1(Value) of
{ok, Value2} ->
do_add(Key, Value2, From, Cache);
Error ->
{reply, Error, Cache}
end;
handle_call({add_with, Key, Callback}, From, Cache) ->
case value(Callback) of
......@@ -281,7 +286,12 @@ handle_call({contains, Key}, _From, Cache) ->
{reply, maps:is_key(Key, Cache#cache.items), Cache};
handle_call({contains_or_add, Key, Value}, From, Cache) ->
do_contains_or_add(Key, Value, From, Cache);
case value_1(Value) of
{ok, Value2} ->
do_contains_or_add(Key, Value2, From, Cache);
Error ->
{reply, Error, Cache}
end;
handle_call({contains_or_add_with, Key, Callback}, From, Cache) ->
case value(Callback) of
......@@ -371,7 +381,6 @@ code_change(_OldVsn, Cache, _Extra) ->
terminate(_Reason, _Cache) ->
ok.
do_add(Key, Value, From, Cache) ->
#cache{items=Items,
evict_list=Evicted} = Cache,
......@@ -418,6 +427,15 @@ do_contains_or_add(Key, Value, From, Cache) ->
{reply, {true, false}, Cache}
end.
value_1(Fun) when is_function(Fun) ->
case catch Fun() of
{'EXIT', Error} -> {error, Error};
{ok, Val} -> {ok, Val};
Val -> {ok, Val}
end;
value_1(Val) ->
{ok, Val}.
value({M, F, A}) when is_function(F) ->
case catch apply(M, F, A) of
{'EXIT', Error} -> {error, Error};
......@@ -577,6 +595,15 @@ lru_add_test() ->
ok.
lru_add_fun_test() ->
{ok, Cache} = lru:start_link([{max_objs, 2}]),
lru:add(Cache, 1, fun() -> 1 end),
?assert(lru:get(Cache, 1) =:= 1),
lru:add(Cache, 2, fun() -> {ok, 2} end),
lru:add(Cache, 2, fun() -> 2 end),
lru:stop(Cache),
ok.
lru_add_with_test() ->
{ok, Cache} = lru:start_link([{max_objs, 2}]),
lru:add_with(Cache, 1, fun() -> 1 end),
?assert(lru:get(Cache, 1) =:= 1),
......@@ -606,7 +633,18 @@ lru_contains_or_add_test() ->
lru:stop(Cache),
ok.
lru_contains_or_add_with_fun_test() ->
lru_contains_or_add_fun_test() ->
{ok, Cache} = lru:start_link([{max_objs, 2}]),
lru:add(Cache, 1, 1),
lru:add(Cache, 2, 2),
?assertEqual(lru:contains_or_add(Cache, 1, fun() -> 1 end), {true, false}),
lru:add(Cache, 3, 3),
?assertEqual(lru:contains_or_add(Cache, 1, fun() -> 1 end), {false, true}),
?assert(lru:contains(Cache, 1)),
lru:stop(Cache),
ok.
lru_contains_or_add_with_test() ->
{ok, Cache} = lru:start_link([{max_objs, 2}]),
lru:add(Cache, 1, 1),
lru:add(Cache, 2, 2),
......