Skip to content

Instantly share code, notes, and snippets.

@mirzap
Forked from bpo/eval-hello.sh
Created July 12, 2018 19:42
Show Gist options
  • Save mirzap/32bf582228c22851087df500ad7f742e to your computer and use it in GitHub Desktop.
Save mirzap/32bf582228c22851087df500ad7f742e to your computer and use it in GitHub Desktop.
Redis Lua examples
redis-cli EVAL "$(cat hello.lua)" 0
redis-cli EVAL "$(cat incr-and-stor.lua)" 2 links:counter links:urls http://malcolmgladwellbookgenerator.com/
These are examples for a blog post on www.redisgreen.net
local msg = "Hello, world!"
return msg
if redis.call("HEXISTS", KEYS[1], ARGV[1]) == 1 then
return redis.call("HINCR", KEYS[1], ARGV[1])
else
return nil
end
local link_id = redis.call("INCR", "links:counter")
redis.call("HSET", "links:urls", link_id, "http://malcolmgladwellbookgenerator.com")
return link_id
local link_id = redis.call("INCR", KEYS[1])
redis.call("HSET", KEYS[2], link_id, ARGV[1])
return link_id
if redis.call("EXISTS",KEYS[1]) == 1 then
return redis.call("INCR",KEYS[1])
else
return nil
end
redis-cli set apple '{ "color": "red", "type": "fruit" }'
=> OK
redis-cli eval "$(cat json-get.lua)" 1 apple type
=> "fruit"
if redis.call("EXISTS", KEYS[1]) == 1 then
local payload = redis.call("GET", KEYS[1])
return cjson.decode(payload)[ARGV[1]]
else
return nil
end
if redis.call("EXISTS", KEYS[1]) == 1 then
local payload = redis.call("GET", KEYS[1])
return cmsgpack.unpack(payload)[ARGV[1]]
else
return nil
end
local indiana_pi = 3.2
redis.call("SET", "pi", indiana_pi)
return redis.call("GET", "pi")
redis-cli SCRIPT LOAD "return 'hello world'"
=> "5332031c6b470dc5a0dd9b4bf2030dea6d65de91"
redis-cli EVALSHA 5332031c6b470dc5a0dd9b4bf2030dea6d65de91 0
=> "hello world"
local indiana_pi = 3.2
return indiana_pi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment