redis翻译_redislua脚本(七)

2015-07-24 07:47:25 · 作者: · 浏览: 16
nly File. The reason is that sending a script to another Redis instance is much faster than sending the multiple commands the script generates, so if the client is sending many scripts to the master, converting the scripts into individual commands for the slave / AOF would result in too much bandwidth for the replication link or the Append Only File (and also too much CPU since dispatching a command received via network is a lot more work for Redis compared to dispatching a command invoked by Lua scripts).
脚本一个非常重要的部分是作为一个纯粹的方法写脚本。脚本运行在Redis实例上可以通过发送该脚本使得该脚本可以 被 slaves 复制--作为没有返回的命令.和添加只读文件是一样的。原因是发送一个脚本到其他Redis实例比发送几个命令使之产生脚本快,所以如果客户端发送一些脚本到master上,使脚本为 slave/SOF 转换成单个的命令的结果是为应答或者增加文件花费更多的带宽(而且Redis一个命令通过网络接收调用与使用脚本比较要消耗更多的CPU)。

?

The only drawback with this approach is that scripts are required to have the following property:

唯一的缺点是,使用的脚本必须有下面的特点:

The script always eva luates the same Redis write commands with the same arguments given the same input data set. Operations performed by the script cannot depend on any hidden (non-explicit) information or state that may change as script execution proceeds or between different executions of the script, nor can it depend on any external input from I/O devices. 脚本的执行结果总是与给出相同参数的命令的执行结果相同。脚本操作不能依赖隐藏的信息或者可变的状态作为脚本执行的结果或者不同的执行过程,也不能依赖与使用I/O设备的输入信息。 Things like using the system time, calling Redis random commands like RANDOMKEY, or using Lua random number generator, could result into scripts that will not always eva luate in the same way.
就像使用系统时间,使用Redis 随机命令 如RANDOMKEY,或者使用Lua随机数触发器,都使得同样的脚本有不同的结果。 In order to enforce this behavior in scripts Redis does the following:
为了强制这个做法,脚本应该这样做:

Lua does not export commands to access the system time or other external state.

没有导出命令来访问系统时间或其他外部状态。

Redis will block the script with an error if a script calls a Redis command able to alter the data set after a Redis random command like RANDOMKEY,SRANDMEMBER, TIME. This means that if a script is read-only and does not modify the data set it is free to call those commands. Note that a random command does not necessarily mean a command that uses random numbers: any non-deterministic command is considered a random command (the best example in this regard is the TIME command).

修改数据的脚本应该避免调用随机设置数据的命令,比如RANDOMKEY,SRANDMEMBER,TIME。反过来也就是说,如果一个脚本是只读的,不修改数据设置的,它就可以自由地调用这些命令。注意这里说的随机命令并不只是只那些随机数字的命令:任何非确定性的命令都被认为是一个随机命令(最好的例子就是TIME命令)。

Redis commands that may return elements in random order, like SMEMBERS(because Redis Sets are unordered) have a different behavior when called from Lua, and undergo a silent lexicographical sorting filter before returning data to Lua scripts. So redis.call("smembers",KEYS[1]) will always return the Set elements in the same order, while the same command invoked from normal clients may return different results even if the key contains exactly the same elements.

随机返回元素的命令,像SMEMBERS(因为Set是无序的)被Lua脚本调用时会有不同的结果,并且返回要经过lua过滤排序。因此 redis.call("smembers",KEY[1]) 总是以相同的顺序返回元素,当同一个命令客户端调用时将返回不同的结果,尽管key包含完全相同的元素。

Lua pseudo random number generation functions math.random andmath.randomseed are modified in order to always have the same seed every time a new script is executed. This means that calling math.random will always generate the same sequence of numbers every time a script is executed if math.randomseedis not used.

为了每次调用lua的math.random和math.randomseed方法都是一个相同的随机种子,Redis对math.random和mathrandomseed被进行了修正。意思是如果math.randomsee