Last active
November 2, 2022 19:26
-
-
Save bpo/5157860 to your computer and use it in GitHub Desktop.
Redis Lua examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
redis-cli EVAL "$(cat hello.lua)" 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
redis-cli EVAL "$(cat incr-and-stor.lua)" 2 links:counter links:urls http://malcolmgladwellbookgenerator.com/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
These are examples for a blog post on www.redisgreen.net |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local msg = "Hello, world!" | |
return msg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if redis.call("HEXISTS", KEYS[1], ARGV[1]) == 1 then | |
return redis.call("HINCR", KEYS[1], ARGV[1]) | |
else | |
return nil | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local link_id = redis.call("INCR", "links:counter") | |
redis.call("HSET", "links:urls", link_id, "http://malcolmgladwellbookgenerator.com") | |
return link_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local link_id = redis.call("INCR", KEYS[1]) | |
redis.call("HSET", KEYS[2], link_id, ARGV[1]) | |
return link_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if redis.call("EXISTS",KEYS[1]) == 1 then | |
return redis.call("INCR",KEYS[1]) | |
else | |
return nil | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
redis-cli set apple '{ "color": "red", "type": "fruit" }' | |
=> OK | |
redis-cli eval "$(cat json-get.lua)" 1 apple type | |
=> "fruit" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local indiana_pi = 3.2 | |
redis.call("SET", "pi", indiana_pi) | |
return redis.call("GET", "pi") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
redis-cli SCRIPT LOAD "return 'hello world'" | |
=> "5332031c6b470dc5a0dd9b4bf2030dea6d65de91" | |
redis-cli EVALSHA 5332031c6b470dc5a0dd9b4bf2030dea6d65de91 0 | |
=> "hello world" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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