报错,说是类型不匹配,该如何处理。。。
$redis = Redis::connection('seckill_cache');
$stockKey = "product_stock:{$data['productId']}";
$sortedSetKey = "grab_requests:{$data['productId']}";
$timestamp = microtime(true);
$userId = $data['userId'];
$productStock = $redis->get($stockKey);
if ($productStock === false || $productStock === null) {
$productStock = Db::table('wa_sec')->where('product_id', $data['productId'])->value('stock');
if ($productStock === null || $productStock <= 0) {
return 1;
}
$redis->set($stockKey, $productStock);
} else if ((int)$productStock === 0) {
return 1;
}
// 检查用户是否已经在抢购队列中
if ($redis->zScore($sortedSetKey, $userId) !== false) {
return 0; // 用户已存在,不允许再次抢购
}
// 使用Lua脚本来保证库存减少和用户加入队列的原子性操作
$luaScript = <<<LUA
local currentStock = tonumber(redis.call('GET', KEYS[1]))
if currentStock <= 0 then
return 0
end
redis.call('DECR', KEYS[1])
redis.call('ZADD', KEYS[2], ARGV[1], ARGV[2])
return 1
LUA;
// 调用 Lua 脚本并传递正确的参数
$result = $redis->eval($luaScript, [$stockKey, $sortedSetKey], 2, $timestamp, (int)$userId);
TypeError: Redis::eval(): Argument #3 ($num_keys) must be of type int, array given in D:\phps\webman\webman\vendor\illuminate\redis\Connections\Connection.php:116<br />Stack trace:<br />#0 D:\phps\webman\webman\vendor\illuminate\redis\Connections\Connection.php(116): Redis->eval(' local curre...', Array, Array)<br />#1 D:\phps\webman\webman\vendor\illuminate\redis\Connections\PhpRedisConnection.php(531): Illuminate\Redis\Connections\Connection->command('eval', Array)<br />#2 D:\phps\webman\webman\vendor\illuminate\redis\Connections\PhpRedisConnection.php(448): Illuminate\Redis\Connections\PhpRedisConnection->command('eval', Array)<br />#3 D:\phps\webman\webman\plugin\frontApi\app\model\Seckill.php(65): Illuminate\Redis\Connections\PhpRedisConnection->eval(' local curre...', Array, 2, 1738833568.0599, 1)<br />#4
解决办法 ->eval(脚本,keys的个数,然后是keys对应的,再试arg之后的参数,) 脚本对应不能乱! 没有数组
$result = $redis->eval($luaScript, count([$stockKey, $sortedSetKey]), stockKey,sortedSetKey $timestamp, (int)$userId);
参数2 应该写在第二个吧?
有人解决了么
$result = $redis->eval($luaScript, count([$stockKey, $sortedSetKey]), $stockKey, $sortedSetKey,$timestamp, (int)$userId);
后面的都是参数 ,要对应起来。 没有数组!!!!
提示都很明显了