Skip to content

Commit

Permalink
allowing use of CAS property in set
Browse files Browse the repository at this point in the history
For situations where you don't want to replace a value if the record has been modified since the last read a CAS value is passed from the last read.
  • Loading branch information
courtneycouch committed Apr 19, 2016
1 parent abfa4a6 commit 5815b58
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/cberl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
-export([start_link/2, start_link/3, start_link/5, start_link/6, start_link/7]).
-export([stop/1]).
%% store operations
-export([add/4, add/5, replace/4, replace/5, set/4, set/5, store/7]).
-export([add/4, add/5, replace/4, replace/5, set/4, set/5, set/6, store/7]).
%% update operations
-export([append/3, prepend/3, touch/3, mtouch/3]).
-export([incr/3, incr/4, incr/5, decr/3, decr/4, decr/5]).
Expand Down Expand Up @@ -101,6 +101,13 @@ set(PoolPid, Key, Exp, Value) ->
set(PoolPid, Key, Exp, Value, TranscoderOpts) ->
store(PoolPid, set, Key, Value, TranscoderOpts, Exp, 0).


%% @equiv store(PoolPid, set, "", Key, Value, Exp)
-spec set(pid(), key(), integer(), value(), atom(), integer()) -> ok | {error, _}.
set(PoolPid, Key, Exp, Value, TranscoderOpts, Cas) ->
store(PoolPid, set, Key, Value, TranscoderOpts, Exp, Cas).


%%%%%%%%%%%%%%%%%%%%%%%%%
%%% UPDATE OPERATIONS %%%
%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down
21 changes: 21 additions & 0 deletions test/cberl_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
cberl_test_() ->
[{foreach, fun setup/0, fun clean_up/1,
[fun test_set_and_get/1,
fun test_set_cas/1,
fun test_replace_add/1,
fun test_multi_get/1,
fun test_get_and_touch/1,
Expand Down Expand Up @@ -51,6 +52,26 @@ test_set_and_get(_) ->
?_assertMatch({Key, _, Value}, Get3)
].

test_set_cas(_) ->
Key = <<"testkey">>,
Value = "testval",
Value2 = "testval2",
Value3 = "testval3",
ok = cberl:set(?POOLNAME, Key, 0, Value),
{_, Cas1, _} = cberl:get(?POOLNAME, Key),
ok = cberl:set(?POOLNAME, Key, 0, Value2, standard, Cas1),
{_, Cas2, Value2} = cberl:get(?POOLNAME, Key),
SetFail = cberl:set(?POOLNAME, Key, 0, Value3, standard, Cas1),
{_, Cas2, Value3failed} = cberl:get(?POOLNAME, Key),
ok = cberl:set(?POOLNAME, Key, 0, Value3, standard, Cas2),
{_, _, Value3Success} = cberl:get(?POOLNAME, Key),
[?_assertEqual({error, key_eexists}, SetFail),
?_assertEqual(Value3failed, Value2),
?_assertEqual(Value3Success, Value3)
].



test_multi_get(_) ->
Value = "testval",
Keys = lists:map(fun(N) -> list_to_binary(integer_to_list(N)) end, lists:seq(1, 1000)),
Expand Down

0 comments on commit 5815b58

Please sign in to comment.