<?php
if (!function_exists('handle_order_lock')) {
function handle_order_lock($orderId = 0, $expire = 5)
{
$key = 'multi_handle_order_lock_' . $orderId;
$value = uniqid();
$script = <<<SCRIPT
return redis.call('SET', KEYS[1], ARGV[1], 'NX', 'EX', ARGV[2])
SCRIPT;
$bool = \Illuminate\Support\Facades\Redis::eval($script, 1, $key, $value, $expire);
return $bool ? $value : false;
}
}
if (!function_exists('handle_order_unlock')) {
function handle_order_unlock($orderId = 0, $lockStr = '')
{
$key = 'multi_handle_order_lock_' . $orderId;
$has = \Illuminate\Support\Facades\Redis::exists($key);
if ($has) {
$value = \Illuminate\Support\Facades\Redis::get($key);
if ($value == $lockStr) {
$del = \Illuminate\Support\Facades\Redis::del($key);
return $del ? true : false;
}
return false;
}
return true;
}
}