Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ismaelga
Created May 19, 2015 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ismaelga/3fa7529376202a776a66 to your computer and use it in GitHub Desktop.
Save ismaelga/3fa7529376202a776a66 to your computer and use it in GitHub Desktop.
defmacro defredis_script(name, script) do
script_sha = :crypto.hash(:sha, script)
quote bind_quoted: [script_sha: script_sha, name: name, script: script] do
def unquote(name)(client, keys \\ [], argv \\ []) do
query_args = [length(keys)] ++ keys ++ argv
case Exredis.query client, ["EVALSHA", unquote(script_sha)] ++ query_args do
<<"NOSCRIPT", _ :: binary>> ->
Exredis.query client, ["EVAL", unquote(script)] ++ query_args
reply -> reply
end
end
end
end
end
defmacro defredis_script(name, script) do
quote do
@script_sha :crypto.hash(:sha, unquote(script))
def unquote(name)(client, keys \\ [], argv \\ []) do
query_args = [length(keys)] ++ keys ++ argv
case Exredis.query client, ["EVALSHA", @script_sha] ++ query_args do
<<"NOSCRIPT", _ :: binary>> ->
Exredis.query client, ["EVAL", unquote(script)] ++ query_args
reply -> reply
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment