Commit 1375d220 authored by benoît chesneau's avatar benoît chesneau

improvements: allows a function to return `{ok, val}`

parent 59314291
Pipeline #4573905 passed with stage
in 52 seconds
erlang-lru version 2.2.0, released on 2016/10/12
================================================
- improvements: allows a function to return `{ok, Val}`.
erlang-lru version 2.1.0, released on 2016/10/12
================================================
......
@copyright 2016 Benoît Chesneau.
@version 2.1.0.
@version 2.2.0.
@title: Erlang LRU: a fixed size LRU cache.
@doc
......
{application, 'lru',
[{description, "implements a fixed-size LRU cache"},
{vsn, "2.1.0"},
{vsn, "2.2.0"},
{registered, []},
{applications,
[kernel,
......
......@@ -389,16 +389,19 @@ do_contains_or_add(Key, Value, From, Cache) ->
value({M, F, A}) ->
case catch apply(M, F, A) of
{'EXIT', Error} -> {error, Error};
{ok, Val} -> {ok, Val};
Val -> {ok, Val}
end;
value({F, A}) ->
case catch apply(F, A) of
{'EXIT', Error} -> {error, Error};
{ok, Val} -> {ok, Val};
Val -> {ok, Val}
end;
value(Fun) when is_function(Fun) ->
case catch Fun() of
{'EXIT', Error} -> {error, Error};
{ok, Val} -> {ok, Val};
Val -> {ok, Val}
end;
value(Val) ->
......@@ -542,9 +545,11 @@ lru_add_test() ->
ok.
lru_add_fun_test() ->
{ok, Cache} = lru:start_link([{max_objs, 1}]),
{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.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment