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, nowarn_nomatch]).
-export([hello/2, keys/1, scan/2, scan_pattern/3, scan_with_type/3, scan_pattern_with_type/4, 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, publish/2, subscribe/1, subscribe_to_patterns/1, unsubscribe/1, unsubscribe_from_all/0, unsubscribe_from_patterns/1, unsubscribe_from_all_patterns/0]).
-export_type([hello_option/0, set_option/0]).
-type hello_option() :: {auth, binary()} |
{auth_with_username, binary(), binary()}.
-type set_option() :: nx |
xx |
get |
keepttl |
{ex, integer()} |
{px, integer()} |
{exat, integer()} |
{pxat, integer()}.
-spec hello(integer(), list(hello_option())) -> bitstring().
hello(Protocol, Options) ->
_pipe = [<<"HELLO"/utf8>>, gleam@int:to_string(Protocol)],
_pipe@1 = gleam@list:append(
_pipe,
gleam@list:flat_map(Options, fun(Item) -> case Item of
{auth, Password} ->
[<<"AUTH"/utf8>>, <<"default"/utf8>>, Password];
{auth_with_username, Username, Password@1} ->
[<<"AUTH"/utf8>>, Username, Password@1]
end end)
),
radish@utils:prepare(_pipe@1).
-spec keys(binary()) -> bitstring().
keys(Pattern) ->
_pipe = [<<"KEYS"/utf8>>, Pattern],
radish@utils:prepare(_pipe).
-spec scan(integer(), integer()) -> bitstring().
scan(Cursor, Count) ->
_pipe = [<<"SCAN"/utf8>>,
gleam@int:to_string(Cursor),
<<"COUNT"/utf8>>,
gleam@int:to_string(Count)],
radish@utils:prepare(_pipe).
-spec scan_pattern(integer(), binary(), integer()) -> bitstring().
scan_pattern(Cursor, Pattern, Count) ->
_pipe = [<<"SCAN"/utf8>>,
gleam@int:to_string(Cursor),
<<"MATCH"/utf8>>,
Pattern,
<<"COUNT"/utf8>>,
gleam@int:to_string(Count)],
radish@utils:prepare(_pipe).
-spec scan_with_type(integer(), binary(), integer()) -> bitstring().
scan_with_type(Cursor, Key_type, Count) ->
_pipe = [<<"SCAN"/utf8>>,
gleam@int:to_string(Cursor),
<<"COUNT"/utf8>>,
gleam@int:to_string(Count),
<<"TYPE"/utf8>>,
Key_type],
radish@utils:prepare(_pipe).
-spec scan_pattern_with_type(integer(), binary(), binary(), integer()) -> bitstring().
scan_pattern_with_type(Cursor, Key_type, Pattern, Count) ->
_pipe = [<<"SCAN"/utf8>>,
gleam@int:to_string(Cursor),
<<"MATCH"/utf8>>,
Pattern,
<<"COUNT"/utf8>>,
gleam@int:to_string(Count),
<<"TYPE"/utf8>>,
Key_type],
radish@utils:prepare(_pipe).
-spec exists(list(binary())) -> bitstring().
exists(Keys) ->
_pipe = [<<"EXISTS"/utf8>>],
_pipe@1 = gleam@list:append(_pipe, Keys),
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, Keys),
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
nx ->
gleam@list:append(Cmd, [<<"NX"/utf8>>]);
xx ->
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, Keys),
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(binary())) -> bitstring().
expire(Key, Ttl, Expire_if) ->
_pipe = case Expire_if of
none ->
[<<"EXPIRE"/utf8>>, Key, gleam@int:to_string(Ttl)];
{some, Condition} ->
[<<"EXPIRE"/utf8>>, Key, gleam@int:to_string(Ttl), Condition]
end,
radish@utils:prepare(_pipe).
-spec publish(binary(), binary()) -> bitstring().
publish(Channel, Message) ->
_pipe = [<<"PUBLISH"/utf8>>, Channel, Message],
radish@utils:prepare(_pipe).
-spec subscribe(list(binary())) -> bitstring().
subscribe(Channels) ->
_pipe = [<<"SUBSCRIBE"/utf8>>],
_pipe@1 = gleam@list:append(_pipe, Channels),
radish@utils:prepare(_pipe@1).
-spec subscribe_to_patterns(list(binary())) -> bitstring().
subscribe_to_patterns(Patterns) ->
_pipe = [<<"PSUBSCRIBE"/utf8>>],
_pipe@1 = gleam@list:append(_pipe, Patterns),
radish@utils:prepare(_pipe@1).
-spec unsubscribe(list(binary())) -> bitstring().
unsubscribe(Channels) ->
_pipe = [<<"UNSUBSCRIBE"/utf8>>],
_pipe@1 = gleam@list:append(_pipe, Channels),
radish@utils:prepare(_pipe@1).
-spec unsubscribe_from_all() -> bitstring().
unsubscribe_from_all() ->
_pipe = [<<"UNSUBSCRIBE"/utf8>>],
radish@utils:prepare(_pipe).
-spec unsubscribe_from_patterns(list(binary())) -> bitstring().
unsubscribe_from_patterns(Patterns) ->
_pipe = [<<"PUNSUBSCRIBE"/utf8>>],
_pipe@1 = gleam@list:append(_pipe, Patterns),
radish@utils:prepare(_pipe@1).
-spec unsubscribe_from_all_patterns() -> bitstring().
unsubscribe_from_all_patterns() ->
_pipe = [<<"PUNSUBSCRIBE"/utf8>>],
radish@utils:prepare(_pipe).