Skip to content

Instantly share code, notes, and snippets.

@evangwt
Forked from bpo/eval-hello.sh
Created March 31, 2019 14:07
Show Gist options
  • Save evangwt/1a4c6200cd5b2cb5dad7755f0c3527ad to your computer and use it in GitHub Desktop.
Save evangwt/1a4c6200cd5b2cb5dad7755f0c3527ad 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