Current section

Files

Jump to
radish src radish@command.erl
Raw

src/radish@command.erl

-module(radish@command).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function]).
-export([hello/1, keys/1, exists/1, get/1, mget/1, append/2, set/3, mset/1, del/1, incr/1, incr_by/2, incr_by_float/2, decr/1, decr_by/2, random_key/0, key_type/1, rename/2, renamenx/2, persist/1, expire/3]).
-export_type([set_option/0, expire_condition/0]).
-type set_option() :: snx |
sxx |
get |
keepttl |
{ex, integer()} |
{px, integer()} |
{exat, integer()} |
{pxat, integer()}.
-type expire_condition() :: nx | xx | gt | lt.
-spec hello(integer()) -> bitstring().
hello(Protocol) ->
_pipe = [<<"HELLO"/utf8>>, gleam@int:to_string(Protocol)],
radish@utils:prepare(_pipe).
-spec keys(binary()) -> bitstring().
keys(Pattern) ->
_pipe = [<<"KEYS"/utf8>>, Pattern],
radish@utils:prepare(_pipe).
-spec exists(list(binary())) -> bitstring().
exists(Keys) ->
_pipe = [<<"EXISTS"/utf8>>],
_pipe@1 = gleam@list:append(
_pipe,
gleam@list:map(Keys, fun(Key) -> Key end)
),
radish@utils:prepare(_pipe@1).
-spec get(binary()) -> bitstring().
get(Key) ->
_pipe = [<<"GET"/utf8>>, Key],
radish@utils:prepare(_pipe).
-spec mget(list(binary())) -> bitstring().
mget(Keys) ->
_pipe = [<<"MGET"/utf8>>],
_pipe@1 = gleam@list:append(
_pipe,
gleam@list:map(Keys, fun(Key) -> Key end)
),
radish@utils:prepare(_pipe@1).
-spec append(binary(), binary()) -> bitstring().
append(Key, Value) ->
_pipe = [<<"APPEND"/utf8>>, Key, Value],
radish@utils:prepare(_pipe).
-spec set(binary(), binary(), list(set_option())) -> bitstring().
set(Key, Value, Options) ->
_pipe = gleam@list:fold(
Options,
[<<"SET"/utf8>>, Key, Value],
fun(Cmd, Option) -> case Option of
snx ->
gleam@list:append(Cmd, [<<"NX"/utf8>>]);
sxx ->
gleam@list:append(Cmd, [<<"XX"/utf8>>]);
get ->
gleam@list:append(Cmd, [<<"GET"/utf8>>]);
keepttl ->
gleam@list:append(Cmd, [<<"KEEPTTL"/utf8>>]);
{ex, Value@1} ->
gleam@list:append(
Cmd,
[<<"EX"/utf8>>, gleam@int:to_string(Value@1)]
);
{px, Value@2} ->
gleam@list:append(
Cmd,
[<<"PX"/utf8>>, gleam@int:to_string(Value@2)]
);
{exat, Value@3} ->
gleam@list:append(
Cmd,
[<<"EXAT"/utf8>>, gleam@int:to_string(Value@3)]
);
{pxat, Value@4} ->
gleam@list:append(
Cmd,
[<<"PXAT"/utf8>>, gleam@int:to_string(Value@4)]
)
end end
),
radish@utils:prepare(_pipe).
-spec mset(list({binary(), binary()})) -> bitstring().
mset(Kv_list) ->
_pipe = Kv_list,
_pipe@1 = gleam@list:map(
_pipe,
fun(Kv) -> [erlang:element(1, Kv), erlang:element(2, Kv)] end
),
_pipe@2 = gleam@list:flatten(_pipe@1),
_pipe@3 = gleam@list:append([<<"MSET"/utf8>>], _pipe@2),
radish@utils:prepare(_pipe@3).
-spec del(list(binary())) -> bitstring().
del(Keys) ->
_pipe = [<<"DEL"/utf8>>],
_pipe@1 = gleam@list:append(
_pipe,
gleam@list:map(Keys, fun(Key) -> Key end)
),
radish@utils:prepare(_pipe@1).
-spec incr(binary()) -> bitstring().
incr(Key) ->
_pipe = [<<"INCR"/utf8>>, Key],
radish@utils:prepare(_pipe).
-spec incr_by(binary(), integer()) -> bitstring().
incr_by(Key, Value) ->
_pipe = [<<"INCRBY"/utf8>>, Key, gleam@int:to_string(Value)],
radish@utils:prepare(_pipe).
-spec incr_by_float(binary(), float()) -> bitstring().
incr_by_float(Key, Value) ->
_pipe = [<<"INCRBYFLOAT"/utf8>>, Key, gleam@float:to_string(Value)],
radish@utils:prepare(_pipe).
-spec decr(binary()) -> bitstring().
decr(Key) ->
_pipe = [<<"DECR"/utf8>>, Key],
radish@utils:prepare(_pipe).
-spec decr_by(binary(), integer()) -> bitstring().
decr_by(Key, Value) ->
_pipe = [<<"DECRBY"/utf8>>, Key, gleam@int:to_string(Value)],
radish@utils:prepare(_pipe).
-spec random_key() -> bitstring().
random_key() ->
_pipe = [<<"RANDOMKEY"/utf8>>],
radish@utils:prepare(_pipe).
-spec key_type(binary()) -> bitstring().
key_type(Key) ->
_pipe = [<<"TYPE"/utf8>>, Key],
radish@utils:prepare(_pipe).
-spec rename(binary(), binary()) -> bitstring().
rename(Key, New_key) ->
_pipe = [<<"RENAME"/utf8>>, Key, New_key],
radish@utils:prepare(_pipe).
-spec renamenx(binary(), binary()) -> bitstring().
renamenx(Key, New_key) ->
_pipe = [<<"RENAMENX"/utf8>>, Key, New_key],
radish@utils:prepare(_pipe).
-spec persist(binary()) -> bitstring().
persist(Key) ->
_pipe = [<<"PERSIST"/utf8>>, Key],
radish@utils:prepare(_pipe).
-spec expire(binary(), integer(), gleam@option:option(expire_condition())) -> bitstring().
expire(Key, Ttl, Expire_if) ->
_pipe = case Expire_if of
none ->
[<<"EXPIRE"/utf8>>, Key, gleam@int:to_string(Ttl)];
{some, nx} ->
[<<"EXPIRE"/utf8>>, Key, gleam@int:to_string(Ttl), <<"NX"/utf8>>];
{some, xx} ->
[<<"EXPIRE"/utf8>>, Key, gleam@int:to_string(Ttl), <<"XX"/utf8>>];
{some, gt} ->
[<<"EXPIRE"/utf8>>, Key, gleam@int:to_string(Ttl), <<"GT"/utf8>>];
{some, lt} ->
[<<"EXPIRE"/utf8>>, Key, gleam@int:to_string(Ttl), <<"LT"/utf8>>]
end,
radish@utils:prepare(_pipe).