可以看见3.333转换成3,因为nil的原因string类型的bar没返回。
?
Helper functions to return Redis types 返回redis类型的帮助函数
There are two helper functions to return Redis types from Lua.有两个从lua脚本返回Redis类型的帮助函数.
redis.error_reply(error_string) returns an error reply. This function simply returns the single field table with the err field set to the specified string for you. redis.error_reply(error_string) 返回一个错误应答.这个函数可以简单地返回你设置提示的 err字段表。
redis.status_reply(status_string) returns a status reply. This function simply returns the single field table with the ok field set to the specified string for you. redis.status_reply(status_string) 返回一个状态应答。这个函数可以简单地返回你设置提示的ok字段表。 (下面是译者执行的例子)
?
Atomicity of scripts 脚本的原子性
Redis uses the same Lua interpreter to run all the commands. Also Redis guarantees that a script is executed in an atomic way: no other script or Redis command will be executed while a script is being executed. This semantic is similar to the one ofMULTI / EXEC. From the point of view of all the other clients the effects of a script are either still not visible or already completed.Redis使用相同的Lua解释器去运行所有的命令。并且保证脚本的实现是原子性的:当一个脚本正在执行的时候没有其他的脚本或者命令可以被执行。意思和命令 MULTI/EXEC 相似(MULTI/EXEX是Redis事务)。需要注意的是,从其他客户端的角度来看,脚本的影响是:其他客户端不能访问redis或者已经执行完毕(也就是其他客户端不能执行命令或者脚本)。 However this also means that executing slow scripts is not a good idea. It is not hard to create fast scripts, as the script overhead is very low, but if you are going to use slow scripts you should be aware that while the script is running no other client can execute commands.
然而这也是说,执行一个慢的脚本非常不好。不能快速创建脚本,因为它非常慢,如果你执行这个非常慢的脚本就该意识到,这个脚本在执行的时候其他客户端是不能执行命令的。
?
Error handling 错误处理
As already stated, calls to redis.call() resulting in a Redis command error will stop the execution of the script and return an error, in a way that makes it obvious that the error was generated by a script:
> del foo
(integer) 1
> lpush foo a
(integer) 1
> eva l "return redis.call('get','foo')" 0
(error) ERR Error running script (call to f_6b1bf486c81ceb7edf3c093f4c48582e3 Using redis.pcall() no error is raised, but an error object is returned in the format specified above (as a Lua table with an err field). The script can pass the exact error to the user by returning the error object returned by redis.pcall().
使用函数redis.pcall()没有脚本错误,它仅仅是被指定格式上的这个错误对象被返回(作为Lua 表的err字段)。通过调用redis.pcall()返回得到的错误对象可以被用户使用脚本精确定义. (例如) 127.0.0.1:6379> eva l "return redis.pcall('get','foo')" 0 (error) WRONGTYPE Operation against a key holding the wrong kind of value
?
Bandwidth and eva lSHA 带宽和eva lSHA
The eva l command forces you to send the script body again and again. Redis does not need to recompile the script every time as it uses an internal caching mechanism, however paying the cost of the additional bandwidth may not be optimal in many contexts.eva l命令可以让你重复发送相同脚本。redis内部的缓存机制不需要每次都去重新编译相同的脚本,但是很多时候脚本附带的带宽开销可能不是最好的。 On the other hand, defining commands using a special command or via redis.confwould be a problem for a few reasons:
另一方面,用特殊命令或者通过redis.conf方式定义eva l脚本命令,因为下面几个原因会有问题:
Different instances may have different implementations of a command.
一个命令在不同实例可能有不同的实现Deployment is hard if we have to make sure all instances contain a given command, especially in a distributed environment.
使得所有的实例都包含给出的命令部署是很困难的,特别是在分布式系统中。Reading application code, the complete semant
