nrk / redis-lua

A Lua client library for the redis key value storage system.
MIT License
731 stars 239 forks source link

How to get return values via EVAL? #38

Closed apnix-uk closed 9 years ago

apnix-uk commented 9 years ago

local REDIS = require 'redis' local redis = REDIS.connect('127.0.0.1', 6379) local v = redis:eval("local v = redis.call('get',KEYS[1]) return v",1,"v") print(v)

v is 'nil' even though the command appears to be successful (no error thrown).

The same EVAL in redis-cli works ok....

127.0.0.1:6379[1]> set v value OK 127.0.0.1:6379[1]> eval "local v = redis.call('get',KEYS[1]) return v" 1 "v" "value" 127.0.0.1:6379[1]>

Am I misunderstanding the implementation of EVAL in your library? Is it even possible to get return values out of a script?

Best regards and thanks for a great library ... I'd just love to improve my work by using scripts as I'm reaching the limits of what I can do with transactions.

ignacio commented 9 years ago

Yes, you can get return values out of a script. Tried it and it works. Make sure the key 'v' is actually there.

local REDIS = require 'redis'
local redis = REDIS.connect('192.168.4.11', 6379)
redis:set("v", "hello there")
local v = redis:eval("local v = redis.call('get',KEYS[1]) return v",1,"v")
print(v)

It returns hello there

If not, please report which versions of Redis and redis-lua you are using.

apnix-uk commented 9 years ago

Thanks for confirming "works for me". It also now works for me since I realised when I ran your code and it worked that I created v in database 1 and because of where I was testing eval in my code I was in database 0. Sorry to have bothered you and thanks for your response! Sometimes you just need to know that it works that way :-)

ignacio commented 9 years ago

He! No problem :smiley: