💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
# lua脚本 ``` <pre class="calibre29">``` <span class="token4">eval</span><span class="token3">(</span>script <span class="token3">,</span>args_arr <span class="token3">,</span>num_keys<span class="token1">-</span>int<span class="token3">)</span> <span class="token1">-</span> Evaluate a LUA script serverside 通过内置的Lua解释器<span class="token3">,</span>对 Lua 脚本进行求值<span class="token1">--</span>评估LUA脚本服务器端 <span class="token4">evalSha</span><span class="token3">(</span>script_sha_str<span class="token3">,</span>args_arr<span class="token3">,</span>num_keys_int<span class="token3">)</span> <span class="token1">-</span> 根据给定的 sha1 校验码,对缓存在服务器中的脚本进行求值 <span class="token1">--</span> 从脚本的SHA1哈希值而不是脚本本身评估LUA脚本服务器端 <span class="token4">script</span><span class="token3">(</span>model<span class="token3">,</span>script1<span class="token3">,</span>script2<span class="token3">,</span>scriptn<span class="token3">)</span> <span class="token1">-</span> 执行Redis SCRIPT命令以对脚本子系统执行各种操作 model包含<span class="token3">:</span> load<span class="token3">:</span> 将脚本script1<span class="token1">~</span>n添加到脚本缓存中,但并不立即执行这个脚本<span class="token3">(</span>eval会立即执行<span class="token3">)</span> 将在成功时返回传递脚本的SHA1哈希值,并在失败时返回FALSE flush<span class="token3">:</span>清除所有 Lua 脚本缓存。 始终返回<span class="token6">true</span> kill<span class="token3">:</span>杀死当前正在运行的 Lua 脚本,当且仅当这个脚本没有执行过任何写操作时,这个命令才生效 如果脚本能够被杀死则返回<span class="token6">true</span>,否则返回<span class="token6">false</span> exists<span class="token3">:</span>给定一个或多个脚本的 SHA1 校验和,表示校验和所指定的脚本是否已经被保存在缓存当中 将为每个传递的脚本返回一个TRUE或FALSE的数组 $redis<span class="token1">-</span><span class="token1">></span><span class="token4">script</span><span class="token3">(</span><span class="token2">'load'</span><span class="token3">,</span> $script<span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">script</span><span class="token3">(</span><span class="token2">'flush'</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">script</span><span class="token3">(</span><span class="token2">'kill'</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">script</span><span class="token3">(</span><span class="token2">'exists'</span><span class="token3">,</span> $script1<span class="token3">,</span> <span class="token3">[</span>$script2<span class="token3">,</span> $script3<span class="token3">,</span> <span class="token3">.</span><span class="token3">.</span><span class="token3">.</span><span class="token3">]</span><span class="token3">)</span><span class="token3">;</span> client:使用各种参数发出CLIENT命令 Redis CLIENT命令可以以四种方式使用LIST、GETNAME、SETNAME <span class="token3">[</span>name<span class="token3">]</span>、KILL <span class="token3">[</span>ip<span class="token3">:</span>port<span class="token3">]</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">client</span><span class="token3">(</span><span class="token2">'list'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 获取客户端列表 返回包含客户端信息的数组数组</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">client</span><span class="token3">(</span><span class="token2">'getname'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 获取当前连接的名称 返回客户端名称,如果没有设置,则返回false</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">client</span><span class="token3">(</span><span class="token2">'setname'</span><span class="token3">,</span> <span class="token2">'somename'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 设置当前连接的名称 如果可以设置则为true,否则为false</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">client</span><span class="token3">(</span><span class="token2">'kill'</span><span class="token3">,</span> <span class="token1"><</span>ip<span class="token3">:</span>port<span class="token1">></span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 通过ip:prot 杀死进程 如果客户端可以被杀死则返回true,否则返回false</span> getLastError <span class="token1">-</span> 最后一条错误消息(如果有的话) 包含上次返回的基于脚本的错误消息的字符串,如果没有错误,则为NULL $redis<span class="token1">-</span><span class="token1">></span><span class="token4">eval</span><span class="token3">(</span><span class="token2">'this-is-not-lua'</span><span class="token3">)</span><span class="token3">;</span> $err <span class="token1">=</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">getLastError</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> clearLastError <span class="token1">-</span> 清除最后一条错误消息 返回<span class="token6">true</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">set</span><span class="token3">(</span><span class="token2">'x'</span><span class="token3">,</span> <span class="token2">'a'</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">incr</span><span class="token3">(</span><span class="token2">'x'</span><span class="token3">)</span><span class="token3">;</span> $err <span class="token1">=</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">getLastError</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// "ERR value is not an integer or out of range"</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">clearLastError</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $err <span class="token1">=</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">getLastError</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token4">_prefix</span><span class="token3">(</span>prefix<span class="token3">)</span> <span class="token1">-</span> 一种实用工具方法,用于为phpredis的前缀设置添加前缀 $redis<span class="token1">-</span><span class="token1">></span><span class="token4">setOption</span><span class="token3">(</span>Redis<span class="token3">:</span><span class="token3">:</span>OPT_PREFIX<span class="token3">,</span> <span class="token2">'my-prefix:'</span><span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">_prefix</span><span class="token3">(</span><span class="token2">'my-value'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// Will return 'my-prefix:my-value'</span> <span class="token4">_unserialize</span><span class="token3">(</span>serialized_value<span class="token3">)</span> <span class="token1">-</span> 设置了使用任何序列化程序对数据进行反序列化的实用程序方法 $redis<span class="token1">-</span><span class="token1">></span><span class="token4">setOption</span><span class="token3">(</span>Redis<span class="token3">:</span><span class="token3">:</span>OPT_SERIALIZER<span class="token3">,</span> Redis<span class="token3">:</span><span class="token3">:</span>SERIALIZER_NONE<span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">_serialize</span><span class="token3">(</span><span class="token2">"foo"</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// returns "foo"</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">_serialize</span><span class="token3">(</span><span class="token3">[</span><span class="token3">]</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// Returns "Array"</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">_serialize</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">stdClass</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// Returns "Object"</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">setOption</span><span class="token3">(</span>Redis<span class="token3">:</span><span class="token3">:</span>OPT_SERIALIZER<span class="token3">,</span> Redis<span class="token3">:</span><span class="token3">:</span>SERIALIZER_PHP<span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">_serialize</span><span class="token3">(</span><span class="token2">"foo"</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// Returns 's:3:"foo";'</span> <span class="token4">_serialize</span><span class="token3">(</span>unserialized_value<span class="token3">)</span> <span class="token1">-</span> 设置了使用任何序列化程序序列化数据的实用程序方法 $redis<span class="token1">-</span><span class="token1">></span><span class="token4">setOption</span><span class="token3">(</span>Redis<span class="token3">:</span><span class="token3">:</span>OPT_SERIALIZER<span class="token3">,</span> Redis<span class="token3">:</span><span class="token3">:</span>SERIALIZER_PHP<span class="token3">)</span><span class="token3">;</span> $redis<span class="token1">-</span><span class="token1">></span><span class="token4">_unserialize</span><span class="token3">(</span><span class="token2">'a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// Will return [1,2,3]</span> ``` ```