doc: 阅读redis lua文档

This commit is contained in:
asahi
2025-09-25 14:09:29 +08:00
parent 3baa0caff2
commit a38527cc17

View File

@@ -174,6 +174,9 @@
- [Script Replication](#script-replication) - [Script Replication](#script-replication)
- [Replicating commands instead of Scripts](#replicating-commands-instead-of-scripts) - [Replicating commands instead of Scripts](#replicating-commands-instead-of-scripts)
- [Scripts with deterministic writes](#scripts-with-deterministic-writes) - [Scripts with deterministic writes](#scripts-with-deterministic-writes)
- [Debugging Eval scripts](#debugging-eval-scripts)
- [Execution under low memory conditions](#execution-under-low-memory-conditions)
- [Eval flags](#eval-flags)
# redis # redis
@@ -2932,4 +2935,28 @@ puts r.eval(RandomPushScript,1,:mylist,10,rand(2**32))
该seed是由client生成的作为参数被传递给脚本并且会作为参数被传播给replicas和AOF。这样能够确保同步给AOF和replicas的changes相同。 该seed是由client生成的作为参数被传递给脚本并且会作为参数被传播给replicas和AOF。这样能够确保同步给AOF和replicas的changes相同。
#### Debugging Eval scripts
从Redis 3.2开始Redis支持natvie Lua debuggingredis lua debugger是远程的由server和client组成。
#### Execution under low memory conditions
当redis的内存使用超过最大限制后`the first write command encountered in the script that uses additional memory will cause the script to abort`。
但是当script中的第一条write command没有使用额外内存时存在例外例如`DEL, LREM`命令。在该场景下redis会允许该脚本中所有的命令运行从而保证lua脚本执行的原子性。`如果lua脚本中接下来的命令耗费了额外的内存那么redis内存使用可以超过最大值限制。`
#### Eval flags
通常当运行Eval script时server并不知道该脚本如何访问database。默认情况下redis假设所有脚本都会对数据进行`读取和写入`。
但是从Redis 7.0开始,支持在创建脚本时声明`flags`用于告知redis将如何访问数据。
在如下示例中将在脚本的第一行声明flags:
```lua
#!lua flags=no-writes,allow-stale
local x = redis.call('get','x')
return x
```
当redis看到`#!`的注释时,其将会将脚本看作`声明了flags`即使没有flags被实际定义其相比于没有`#!`的脚本仍然存在一系列不同的默认值。
另一个不同的区别是,`scripts without #!`可以运行命令来访问`keys belonging to different cluster hash slots`,但是拥有`#!`的将继承默认的flags故而其不能对`keys belonging to different cluster hash slots`进行访问。