<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\common\library;

class Lock
{
    protected $lockName = '';
    protected $redis = null;
    protected $isLocked = false;
    
    public function __construct()
    {
        
    }
    
    public function start($lockName,$time=5){
        $this->lockName = $lockName;
        if (extension_loaded('redis')) {
            
            $redis = new \Redis();
            $redis->connect('127.0.0.1', 6379);
            
            $sss = $redis->get($this->lockName);
            print_r($sss);die;
            $this->isLocked = $redis->setnx($this->lockName, time() + $time);
            $this->redis = $redis;
            print_r($this->isLocked);die;
            if ($this->isLocked) {
                return true;
            } 
            else {
                return false;
            }
            
        } 
        else{
            return true;
        }
    } 
    
    public function end(){
        if (extension_loaded('redis') && $this->isLocked && $this->redis && $this->lockName) {
            $redis = $this->redis;
            $redis->del($this->lockName);
        }
    }
}