redis翻译_redislua脚本(二)

2015-07-24 07:47:25 · 作者: · 浏览: 19
的转换 Redis return values are converted into Lua data types when Lua calls a Redis command using call() or pcall(). Similarly Lua data types are converted into the Redis protocol when a Lua script returns a value, so that scripts can control what eva l will return to the client.
当lua调用call()或者pcall()函数时,redis的返回类型被转换成lua的数据类型。同样的当lua脚本返回值时,lua数据类型被转换成redis数据类型,因此脚本可以控制eva l的返回。 This conversion between data types is designed in a way that if a Redis type is converted into a Lua type, and then the result is converted back into a Redis type, the result is the same as the initial value.
两种数据类型的转换,被设计成,如果从Redis类型转换成Lua类型,然后再从lua类型转换成redis类型,最后得到的结果和最初的值时一样的。 In other words there is a one-to-one conversion between Lua and Redis types. The following table shows you all the conversions rules:
换句话说,Lua类型和Redis类型中,这是一个一一对应的转换。下面这个表就是转换规则: Redis to Lua conversion table.Redis 类型转换成Lua类型表:
Redis integer reply -> Lua number
Redis bulk reply -> Lua string
Redis multi bulk reply -> Lua table (may have other Redis data types nested)
Redis status reply -> Lua table with a single ok field containing the status
Redis error reply -> Lua table with a single err field containing the error
Redis Nil bulk reply and Nil multi bulk reply -> Lua false boolean type Lua to Redis conversion table.lua类型转换成Redis类型表:
Lua number -> Redis integer reply (the number is converted into an integer)
Lua string -> Redis bulk reply
Lua table (array) -> Redis multi bulk reply (truncated to the first nil inside the Lua array if any)
Lua table with a single ok field -> Redis status reply
Lua table with a single err field -> Redis error reply
Lua boolean false -> Redis Nil bulk reply. There is an additional Lua-to-Redis conversion rule that has no corresponding Redis to Lua conversion rule:
还有一个Lua转换成Redis的附加规则,这条规则没有对应的Redis转换成Lua:
Lua boolean true -> Redis integer reply with value of 1. Also there are two important rules to note:并且这里有两个重要的规则需要注意:
Lua has a single numerical type, Lua numbers. There is no distinction between integers and floats. So we always convert Lua numbers into integer replies, removing the decimal part of the number if any. If you want to return a float from Lua you should return it as a string, exactly like Redis itself does (see for instance the ZSCORE command). Lua有一个数值类型,number。它没有整型和浮点型的区别。因此我们总是将Lua的numbers类型转换成integer replies,如果有小数就删除小数部分。如果你想返回一个float,那么你应该把它作为一个string类型返回,就像Redis自己的做法(参照ZSCORE命令用法的例子)
There is no simple way to have nils inside Lua arrays, this is a result of Lua table semantics, so when Redis converts a Lua array into Redis protocol the conversion is stopped if a nil is encountered. 在Lua中数组中没有nil值得表示方式,因为Lua中数组是一个table的含义,因此当Redis转换成Lua 数组遇到nil值就停止了(后面的也不转换了)。 Here are a few conversion examples: 看几个例子
> eva l "return 10" 0
(integer) 10

> eva l "return {1,2,{3,'Hello World!'}}" 0
1) (integer) 1
2) (integer) 2
3) 1) (integer) 3
   2) "Hello World!"

> eva l "return redis.call('get','foo')" 0
"bar"
The last example shows how it is possible to receive the exact return value ofredis.call() or redis.pcall() from Lua that would be returned if the command was called directly.
最后一个例子是展示使用redis.call()或者redis.pcall()函数直接调命令,结果被直接从lua类型强制转换成Redis类型。 In the following example we can see how floats and arrays with nils are handled:
下面例子说float和数组有nil值得处理:
> eva l "return {1,2,3.3333,'foo',nil,'bar'}" 0
1) (integer) 1
2) (integer) 2
3) (integer) 3
4) "foo"
As you can see 3.333 is converted into 3, and the bar string is neve