Last active
January 3, 2016 13:19
-
-
Save czarneckid/8468926 to your computer and use it in GitHub Desktop.
Redis, Lua and global functions Redis 2.8.3
redis-rb 3.0.6
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
require 'redis' | |
redis = Redis.new | |
redis.script(:flush) | |
redis.flushdb | |
hello_name_lua_script = <<-HELLO_NAME_LUA_SCRIPT | |
local function hello_name(name) | |
return("Hello " .. name) | |
end | |
rawset(_G, "hello_name", hello_name) | |
HELLO_NAME_LUA_SCRIPT | |
hello_name_sha = redis.eval(hello_name_lua_script) | |
use_hello_name_lua_script = <<-USE_HELLO_NAME_LUA_SCRIPT | |
return hello_name("David") | |
USE_HELLO_NAME_LUA_SCRIPT | |
use_hello_name_sha = redis.script(:load, use_hello_name_lua_script) | |
redis.evalsha(use_hello_name_sha) |
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
1.9.3p484 :001 > require 'redis' | |
=> true | |
1.9.3p484 :002 > | |
1.9.3p484 :003 > redis = Redis.new | |
=> #<Redis client v3.0.6 for redis://127.0.0.1:6379/0> | |
1.9.3p484 :004 > redis.script(:flush) | |
=> "OK" | |
1.9.3p484 :005 > redis.flushdb | |
=> "OK" | |
1.9.3p484 :006 > | |
1.9.3p484 :007 > hello_name_lua_script = <<-HELLO_NAME_LUA_SCRIPT | |
1.9.3p484 :008"> local function hello_name(name) | |
1.9.3p484 :009"> return("Hello " .. name) | |
1.9.3p484 :010"> end | |
1.9.3p484 :011"> rawset(_G, "hello_name", hello_name) | |
1.9.3p484 :012"> HELLO_NAME_LUA_SCRIPT | |
=> "local function hello_name(name)\n return(\"Hello \" .. name)\nend\nrawset(_G, \"hello_name\", hello_name)\n" | |
1.9.3p484 :013 > | |
1.9.3p484 :014 > hello_name_sha = redis.eval(hello_name_lua_script) | |
=> nil | |
1.9.3p484 :015 > | |
1.9.3p484 :016 > use_hello_name_lua_script = <<-USE_HELLO_NAME_LUA_SCRIPT | |
1.9.3p484 :017"> return hello_name("David") | |
1.9.3p484 :018"> USE_HELLO_NAME_LUA_SCRIPT | |
=> "return hello_name(\"David\")\n" | |
1.9.3p484 :019 > | |
1.9.3p484 :020 > use_hello_name_sha = redis.script(:load, use_hello_name_lua_script) | |
=> "4f7f594b261bc8cec50a6ef445ef43ca156f0bd2" | |
1.9.3p484 :021 > redis.evalsha(use_hello_name_sha) | |
=> "Hello David" | |
1.9.3p484 :022 > |
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
require 'redis' | |
redis = Redis.new | |
redis.script(:flush) | |
redis.flushdb | |
hello_name_lua_script = <<-HELLO_NAME_LUA_SCRIPT | |
local function hello_name(name) | |
return("Hello " .. name) | |
end | |
rawset(_G, "hello_name", hello_name) | |
HELLO_NAME_LUA_SCRIPT | |
# Using script load here instead of eval | |
hello_name_sha = redis.script(:load, hello_name_lua_script) | |
use_hello_name_lua_script = <<-USE_HELLO_NAME_LUA_SCRIPT | |
return hello_name("David") | |
USE_HELLO_NAME_LUA_SCRIPT | |
use_hello_name_sha = redis.script(:load, use_hello_name_lua_script) | |
redis.evalsha(use_hello_name_sha) |
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
1.9.3p484 :001 > require 'redis' | |
=> true | |
1.9.3p484 :002 > | |
1.9.3p484 :003 > redis = Redis.new | |
=> #<Redis client v3.0.6 for redis://127.0.0.1:6379/0> | |
1.9.3p484 :004 > redis.script(:flush) | |
=> "OK" | |
1.9.3p484 :005 > redis.flushdb | |
=> "OK" | |
1.9.3p484 :006 > | |
1.9.3p484 :007 > hello_name_lua_script = <<-HELLO_NAME_LUA_SCRIPT | |
1.9.3p484 :008"> local function hello_name(name) | |
1.9.3p484 :009"> return("Hello " .. name) | |
1.9.3p484 :010"> end | |
1.9.3p484 :011"> rawset(_G, "hello_name", hello_name) | |
1.9.3p484 :012"> HELLO_NAME_LUA_SCRIPT | |
=> "local function hello_name(name)\n return(\"Hello \" .. name)\nend\nrawset(_G, \"hello_name\", hello_name)\n" | |
1.9.3p484 :013 > | |
1.9.3p484 :014 > hello_name_sha = redis.script(:load, hello_name_lua_script) | |
=> "94babaed36833c7019d5fc0d9b8b2ecaf59a13e9" | |
1.9.3p484 :015 > | |
1.9.3p484 :016 > use_hello_name_lua_script = <<-USE_HELLO_NAME_LUA_SCRIPT | |
1.9.3p484 :017"> return hello_name("David") | |
1.9.3p484 :018"> USE_HELLO_NAME_LUA_SCRIPT | |
=> "return hello_name(\"David\")\n" | |
1.9.3p484 :019 > | |
1.9.3p484 :020 > use_hello_name_sha = redis.script(:load, use_hello_name_lua_script) | |
=> "4f7f594b261bc8cec50a6ef445ef43ca156f0bd2" | |
1.9.3p484 :021 > redis.evalsha(use_hello_name_sha) | |
Redis::CommandError: ERR Error running script (call to f_4f7f594b261bc8cec50a6ef445ef43ca156f0bd2): @enable_strict_lua:14: user_script:1: Script attempted to access unexisting global variable 'hello_name' | |
redis.evalsha(use_hello_name_sha) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment