Skip to content

Instantly share code, notes, and snippets.

@SHyx0rmZ
Created April 22, 2015 21:26
Show Gist options
  • Select an option

  • Save SHyx0rmZ/c33b6b0233586b66979d to your computer and use it in GitHub Desktop.

Select an option

Save SHyx0rmZ/c33b6b0233586b66979d to your computer and use it in GitHub Desktop.
-module (redis_hash).
-export ([setall/3,getall/2]).
-include ("redis_types.hrl").
setall(Server, Name, Hash) when is_record(Server, redis_server), is_record(Hash, redis_hash) ->
ok = redis_connection:send(Server, "MULTI"),
"OK" = redis_parser:parse_response(redis_connection:recv(Server)),
setall(Server, Name, Hash#redis_hash.entries).
setall(Server, Name, []) ->
ok = redis_connection:send(Server, "EXEC"),
Response = redis_parser:parse_response(redis_connection:recv(Server)),
lists:any(fun(Success) -> Success =:= 0 end, Response) =:= false;
setall(Server, Name, [ { Key, Value } | Hash ]) ->
ok = redis_connection:send(Server, "HSET " ++ Name ++ " " ++ Key ++ " " ++ Value),
"QUEUED" = redis_parser:parse_response(redis_connection:recv(Server)),
setall(Server, Name, Hash).
getall(Server, Name) when is_record(Server, redis_server) ->
ok = redis_connection:send(Server, "HGETALL " ++ Name),
Response = redis_connection:recv(Server),
Array = redis_parser:parse_response(Response),
#redis_hash{ entries = hashify(Array) }.
hashify([]) ->
[];
hashify([ Key, Value | Rest ]) ->
[ { Key, Value } | hashify(Rest) ].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment