redis翻译_redislua脚本(一)

2015-07-24 07:47:25 · 作者: · 浏览: 12
Introduction to eva l 介绍eva l
eva l and eva lSHA are used to eva luate scripts using the Lua interpreter built into Redis starting from version 2.6.0.


eva l和eva lSHA是从Redis2.6.0版本使用内置脚本解释器引入的。
The first argument of eva l is a Lua 5.1 script. The script does not need to define a Lua function (and should not). It is just a Lua program that will run in the context of the Redis server.
eva l的第一个参数是一个lua.5.1的脚本.这段脚本不需要定义lua方法函数(也不应该定义)。仅仅是一个运行在Redis 服务器的一段lua程序。
The second argument of eva l is the number of arguments that follows the script (starting from the third argument) that represent Redis key names. This arguments can be accessed by Lua using the KEYS global variable in the form of a one-based array (so KEYS[1], KEYS[2], ...).
eva l的第二个参数是一个数字,它表示紧跟着的脚本(第三个参数开始)中前多少个是Redis中的key的名称。这些Redis中的key的名称可以使用lua的数组 KEYS取出value(比如 KEYS[1],KEUS[2],...)。
All the additional arguments should not represent key names and can be accessed by Lua using the ARGV global variable, very similarly to what happens with keys (soARGV[1], ARGV[2], ...).
附加参数不代表key的名称并且和keys一样可以使用lua的ARGV全局变量数组访问(比如:ARGV[1],ARGV[2])。
The following example should clarify what stated above:
下面的例子应该可以说明上面的规定:


> eva l "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}" 2 key1 key2 first second
1) "key1"
2) "key2"
3) "first"
4) "second"
Note: as you can see Lua arrays are returned as Redis multi bulk replies, that is a Redis return type that your client library will likely convert into an Array type in your programming language.


注意:正如你所见,lua数组的返回是Redis的多重应答,这是redis的一个返回类型,你的客户端可能会转换成所用程序语言的数组。
It is possible to call Redis commands from a Lua script using two different Lua functions:
使用两个不同的lua函数去调用Redis命令:
redis.call()
redis.pcall() redis.call() is similar to redis.pcall(), the only difference is that if a Redis command call will result in an error, redis.call() will raise a Lua error that in turn will force eva l to return an error to the command caller, while redis.pcall will trap the error and return a Lua table representing the error.
redis.call()和redis.pcall()类似,唯一的不同是,如果命令返回一个错误结束时,redis.call()会将这个错误提升到lua的error,让eva l返回一个lua错误给调用者,redis.pcall()将捕获这个错误并且返回一个代表错误的 Lua table. The arguments of the redis.call() and redis.pcall() functions are all the arguments of a well formed Redis command:
redis.call()和redis.pcall()的参数可以是Redis所有命令的参数:
> eva l "return redis.call('set','foo','bar')" 0
OK
All Redis commands must be analyzed before execution to determine which keys the command will operate on. In order for this to be true for eva l, keys must be passed explicitly. This is useful in many ways, but especially to make sure Redis Cluster can forward your request to the appropriate cluster node (Redis Cluster is a work in progress, but the scripting feature was designed in order to play well with it).
所有的命令在执行之前都是先被解析的,以便确定操作在那些keys上。正确使用eva l,key必须被明确定传递。在很多时候,像上面的写法是没有错的,但是特别注意,在redis集群中明确传递的key可以确保找到正确节点,上面的写法就不行(redis集群正在开发中,但是使得现在的脚本设计去支持集群才是好的脚本). Note this rule is not enforced in order to provide the user with opportunities to abuse the Redis single instance configuration, at the cost of writing scripts not compatible with Redis Cluster.
注意上面这条不强制规则,并不是使用户造成滥用redis实例,编写脚本不支持redis集群的代价的因素。


Lua scripts can return a value that is converted from the Lua type to the Redis protocol using a set of conversion rules.
lua脚本的返回一个lua类型,根据redis协议转换规则转换的redis类型的值。

?

Conversion between Lua and Redis data types redis类型和lua类型