php中使用redis执行lua脚本

<?php
$lua = <<<LUA
    local sequenceKey = KEYS[1]
    local time = KEYS[2]
    local sequenceNumber = redis.call("incr", sequenceKey)
    redis.call("expire", sequenceKey, time)
    return sequenceNumber
LUA;

$redis = new Redis();
$redis->connect('127.0.0.1');
$redis->select(0);
$sequence = $redis->eval($lua, ['lua1234', 120], 2);
$luaError = $redis->getLastError();
if (isset($luaError)) {
    print_r($luaError);
}
print_r($sequence);
//sequence = 1 2 3

 

上一篇:Lua中分割字符串


下一篇:lua 实现对象封装和继承