redis翻译_redislua脚本(九)

2015-07-24 07:47:25 · 作者: · 浏览: 9
tections in order to circumvent globals protection is not hard. However it is difficult to do it accidentally. If the user messes with the Lua global state, the consistency of AOF and replication is not guaranteed: don't do it.
使用Lua的调试功能或其他方法,如改变使用的元表以规避全局保护并不难。但是很难做到不小心。如果用户与Lua全局状态混乱, AOF和复制的一致性是没有保证的:不要这样做。 Note for Lua newbies: in order to avoid using global variables in your scripts simply declare every variable you are going to use using the local keyword.
lua新手要注意:为了避免使用全局变量,声明变量时应该明确使用local关键字。

?

Using SELECT inside scripts在脚本中使用SELECT

It is possible to call SELECT inside Lua scripts like with normal clients, However one subtle aspect of the behavior changes between Redis 2.8.11 and Redis 2.8.12. Before the 2.8.12 release the database selected by the Lua script was transferred to the calling script as current database. Starting from Redis 2.8.12 the database selected by the Lua script only affects the execution of the script itself, but does not modify the database selected by the client calling the script.
在客户端的脚本中使用 SELECT就像一般的客户端一样,只有在 2.8.11和2.8.12有一点变化。在2.8.12之前选择的 数据库是通过Lua脚本传送到Lua脚本当前的数据库。2.8.12之后,通过Lua选择的数据库仅仅影响脚本本身的执行,但是通过客户端调用脚本不修改数据库的选择。 The semantic change between patch level releases was needed since the old behavior was inherently incompatible with the Redis replication layer and was the cause of bugs.
需要修补程序级别版本之间的语义变化,因为旧的行为不符合Redis的复制层的本质,这个是bug的原因。

?

Available libraries 有效的类库

The Redis Lua interpreter loads the following Lua libraries:

Redis的解释器加载了以下类库:

base lib.
table lib.
string lib.
math lib.
debug lib.
struct lib.
cjson lib.
cmsgpack lib.
bitop lib
redis.sha1hex function. Every Redis instance is guaranteed to have all the above libraries so you can be sure that the environment for your Redis scripts is always the same.
每一个Redis实例都确保加载了上面所有的类库,因此你可以确保你的脚本运行环境总是一样的。 struct, CJSON and cmsgpack are external libraries, all the other libraries are standard Lua libraries.
除了struct,CJSON和cmsgpack 是外部类库,其他所有都是Lua的标准类库。

?

struct

struct is a library for packing/unpacking structures within Lua.
struct 是 压缩/解压缩 结构库。
Valid formats:
> - big endian
< - little endian
![num] - alignment
x - pading
b/B - signed/unsigned byte
h/H - signed/unsigned short
l/L - signed/unsigned long
T   - size_t
i/In - signed/unsigned integer with size `n' (default is size of int)
cn - sequence of `n' chars (from/to a string); when packing, n==0 means
     the whole string; when unpacking, n==0 means use the previous
     read number as the string length
s - zero-terminated string
f - float
d - double
' ' - ignored
Example:
127.0.0.1:6379> eva l 'return struct.pack("HH", 1, 2)' 0
"\x01\x00\x02\x00"
127.0.0.1:6379> eva l 'return {struct.unpack("HH", ARGV[1])}' 0 "\x01\x00\x02\x00"
1) (integer) 1
2) (integer) 2
3) (integer) 5
127.0.0.1:6379> eva l 'return struct.size("HH")' 0
(integer) 4

CJSON

The CJSON library provides extremely fast JSON manipulation within Lua.

CJSON提供了快速操作json的类库。

Example:

redis 127.0.0.1:6379> eva l 'return cjson.encode({["foo"]= "bar"})' 0
"{\"foo\":\"bar\"}"
redis 127.0.0.1:6379> eva l 'return cjson.decode(ARGV[1])["foo"]' 0 "{\"foo\":\"bar\"}"
"bar"

cmsgpack

The cmsgpack library provides simple and fast MessagePack manipulation within Lua.

cmsgpack 提供了简单并且快速的MessagePack操作类库。

Example:

127.0.0.1:6379> eva l 'return cmsgpack.pack({"foo", "bar", "baz"})' 0
"\x93\xa3foo\xa3bar\xa3baz"
127.0.0.1:6379> eva l 'return cmsgpack.unpack(ARGV[1])' 0 "\x93\xa3foo\xa3bar\xa3baz"
1) "foo"
2) "bar"
3) "baz

bitop

The Lua Bit Operations Module adds bitwise operations on numbers. It is available for scripting in Redis since version 2.8.18.

?

Lua位运算模块增加了对数字的位运算。从2.8.18版本开始可以使用。

?

Example:

127.0.0.1:6379> eva l 'return bit.tobit(1)' 0
(integer) 1
127.0.0.1:6379> eva l 'return bit.bor(1,2,4,8,16,32,64,128)' 0
(integer) 25