/** * redis锁 * $mkey string 键值 * $second string 秒,锁定多少秒,-1时销毁 * return bool */ function redisLook($mkey = '',$second = 5){ $cache = \think\facade\Cache::init(); $_redis = $cache->handler(); if ($second == -1){ return $_redis->del($mkey); } $lock_time = $_redis->setnx($mkey,time()+$second); if ($lock_time == false){ $lock_time = $_redis->get($mkey); if(time()>$lock_time){ $_redis->del($mkey); $lock_time = $_redis->setnx($mkey,time()+$second); if ($lock_time == false) return false; }else{ return false; } } return true; }