<?php

namespace App\Utility;

use Illuminate\Support\Facades\Redis;

class SetNXLock
{
    /**
     * 获取排他锁
     * @param $key
     * @param int $time
     * @return bool
     */
    public static function getExclusiveLock($key, $time = 5)
    {
        $res = Redis::setnx($key, 1);
        if ($res) {
            Redis::expire($key, $time);
        }
        return $res;
    }

    /**
     * 释放锁
     * @param $key
     * @return int
     */
    public static function release($key)
    {
        return Redis::del($key);
    }
}
