iot/apps/iot/src/redis/redis_handler.erl
2023-07-24 11:26:46 +08:00

198 lines
6.6 KiB
Erlang

%%%-------------------------------------------------------------------
%%% @author licheng5
%%% @copyright (C) 2021, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 21. 1月 2021 上午11:23
%%%-------------------------------------------------------------------
-module(redis_handler).
-author("licheng5").
%% API
-export([handle/1]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Key管理
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
handle([<<"DEL">>, Key]) when is_binary(Key) ->
N = mnesia_kv:del(Key),
{reply, N};
handle([<<"EXISTS">>, Key]) when is_binary(Key) ->
N = mnesia_kv:exists(Key),
{reply, N};
handle([<<"EXPIRE">>, Key, Second0]) when is_binary(Key), is_binary(Second0) ->
Second = binary_to_integer(Second0),
N = mnesia_kv:expire(Key, Second),
{reply, N};
handle([<<"KEYS">>, Pattern]) when is_binary(Pattern) andalso Pattern =/= <<>> ->
Keys = mnesia_kv:keys(Pattern),
{reply, Keys};
handle([<<"PERSIST">>, Key]) when is_binary(Key) ->
N = mnesia_kv:persist(Key),
{reply, N};
handle([<<"TTL">>, Key]) when is_binary(Key) ->
TTL = mnesia_kv:ttl(Key),
{reply, TTL};
handle([<<"TYPE">>, Key]) when is_binary(Key) ->
Type = mnesia_kv:type(Key),
{reply, atom_to_binary(Type)};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 字符串处理
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
handle([<<"GET">>, Key]) when is_binary(Key) ->
{reply, mnesia_kv:get(Key)};
handle([<<"SET">>, Key, Val]) when is_binary(Key), is_binary(Val) ->
case mnesia_kv:set(Key, Val) of
true ->
{reply, <<"OK">>};
false ->
{reply, <<"FAILED">>}
end;
handle([<<"SETNX">>, Key, Val]) when is_binary(Key), is_binary(Val) ->
{reply, mnesia_kv:setnx(Key, Val)};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% HashTable处理
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
handle([<<"HSET">>, Key, Field, Val]) when is_binary(Key), is_binary(Field), is_binary(Val) ->
{reply, mnesia_kv:hset(Key, Field, Val)};
handle([<<"HMSET">>, Key | KvPairs]) when is_binary(Key), length(KvPairs) rem 2 =:= 0 ->
{reply, mnesia_kv:hmset(Key, lists_to_map(KvPairs))};
handle([<<"HGET">>, Key, Field]) when is_binary(Key), is_binary(Field) ->
{reply, mnesia_kv:hget(Key, Field)};
handle([<<"HMGET">>, Key | Fields]) when is_binary(Key), is_list(Fields) ->
{reply, mnesia_kv:hmget(Key, Fields)};
handle([<<"HDEL">>, Key | Fields]) when is_binary(Key), is_list(Fields) ->
{reply, mnesia_kv:hdel(Key, Fields)};
handle([<<"HEXISTS">>, Key, Field]) when is_binary(Key), is_binary(Field) ->
{reply, mnesia_kv:hexists(Key, Field)};
handle([<<"HGETALL">>, Key]) when is_binary(Key) ->
{reply, mnesia_kv:hgetall(Key)};
handle([<<"HKEYS">>, Key]) when is_binary(Key) ->
{reply, mnesia_kv:hkeys(Key)};
handle([<<"HLEN">>, Key]) when is_binary(Key) ->
{reply, mnesia_kv:hlen(Key)};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% set处理
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
handle([<<"SADD">>, Key | Members]) when is_binary(Key), is_list(Members) ->
{reply, mnesia_kv:sadd(Key, Members)};
handle([<<"SCARD">>, Key]) when is_binary(Key) ->
{reply, mnesia_kv:scard(Key)};
handle([<<"SISMEMBER">>, Key, Member]) when is_binary(Key), is_binary(Member) ->
{reply, mnesia_kv:sismember(Key, Member)};
handle([<<"SDIFF">>, Key1, Key2]) when is_binary(Key1), is_binary(Key2) ->
{reply, mnesia_kv:sdiff(Key1, Key2)};
handle([<<"SINTER">>, Key1, Key2]) when is_binary(Key1), is_binary(Key2) ->
{reply, mnesia_kv:sinter(Key1, Key2)};
handle([<<"SUNION">>, Key1, Key2]) when is_binary(Key1), is_binary(Key2) ->
{reply, mnesia_kv:sunion(Key1, Key2)};
handle([<<"SMEMBERS">>, Key]) when is_binary(Key) ->
{reply, mnesia_kv:smembers(Key)};
handle([<<"SPOP">>, Key]) when is_binary(Key) ->
{reply, mnesia_kv:spop(Key)};
handle([<<"SRANDMEMBER">>, Key, Count0]) when is_binary(Key), is_binary(Count0) ->
Count = binary_to_integer(Count0),
{reply, mnesia_kv:srandmember(Key, Count)};
handle([<<"SREM">>, Key | Members]) when is_binary(Key), is_list(Members) ->
{reply, mnesia_kv:srem(Key, Members)};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% List 处理
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
handle([<<"LINDEX">>, Key, Idx0]) when is_binary(Key), is_binary(Idx0) ->
Idx = binary_to_integer(Idx0),
{reply, mnesia_kv:lindex(Key, Idx + 1)};
handle([<<"LLEN">>, Key]) when is_binary(Key) ->
{reply, mnesia_kv:llen(Key)};
handle([<<"LPOP">>, Key]) when is_binary(Key) ->
{reply, mnesia_kv:lpop(Key)};
handle([<<"RPOP">>, Key]) when is_binary(Key) ->
{reply, mnesia_kv:rpop(Key)};
handle([<<"LPUSH">>, Key | Members]) when is_binary(Key) ->
{reply, mnesia_kv:lpush(Key, Members)};
handle([<<"LPUSHX">>, Key | Members]) when is_binary(Key) ->
{reply, mnesia_kv:lpushx(Key, Members)};
handle([<<"RPUSH">>, Key | Members]) when is_binary(Key) ->
{reply, mnesia_kv:rpush(Key, Members)};
handle([<<"RPUSHX">>, Key | Members]) when is_binary(Key) ->
{reply, mnesia_kv:rpushx(Key, Members)};
handle([<<"LRANGE">>, Key, Start0, End0]) when is_binary(Key), is_binary(Start0), is_binary(End0) ->
Start = binary_to_integer(Start0),
End = binary_to_integer(End0),
{reply, mnesia_kv:lrange(Key, Start + 1, End + 1)};
handle([<<"LREM">>, Key, Count0, Val]) when is_binary(Key), is_binary(Count0), is_binary(Val) ->
Count = binary_to_integer(Count0),
{reply, mnesia_kv:lrem(Key, Count, Val)};
handle([<<"LSET">>, Key, Idx0, Val]) when is_binary(Key), is_binary(Idx0), is_binary(Val) ->
Idx = binary_to_integer(Idx0),
{reply, mnesia_kv:lset(Key, Idx + 1, Val)};
handle([<<"LTRIM">>, Key, Start0, End0]) when is_binary(Key), is_binary(Start0), is_binary(End0) ->
Start = binary_to_integer(Start0),
End = binary_to_integer(End0),
{reply, mnesia_kv:ltrim(Key, Start + 1, End + 1)};
handle([<<"LINSERT">>, Key, Position, Pivot, Val]) when is_binary(Key), Position =:= <<"BEFORE">>; Position =:= <<"AFTER">> ->
{reply, mnesia_kv:linsert(Key, Position, Pivot, Val)};
handle(_) ->
{reply, {error, <<"Unsuported Command">>}}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% helper methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 将数组转换成map
lists_to_map(L) when is_list(L) ->
lists_to_map(L, #{}).
lists_to_map([], Map) ->
Map;
lists_to_map([K, V | Tail], Map) ->
lists_to_map(Tail, Map#{K => V}).