Home > php教程 > php手册 > PHP进程锁类PHPLock程序代码

PHP进程锁类PHPLock程序代码

WBOY
Release: 2016-05-25 16:45:42
Original
1098 people have browsed it

为了更好的控制php程序同时操作的一些问题我整理了一个进程锁的类我们可以利用这个进程锁实现程序的控制,程序代码如下:

<?php 
//+---------------------------------------------- 
//|    Usage: 
//+---------------------------------------------- 
//|    public function _initialize(){ 
//|        import(&#39;@.Util.PHPLock&#39;); 
//| 
//|        if(PHPLock::islocked()){ 
//|            echo "[+] Status: Locked\n"; 
//|            echo "[+] Exit\n"; 
//|            exit(); 
//|        }else{ 
//|            echo "[+] Status: Unlocked\n"; 
//|            echo "[-] Locking Now\n"; 
//|            PHPLock::lock(); 
//|        } 
//|    } 
//| 
//|    function __destruct(){ 
//|        if(true === PHPLock::unlock()){ 
//|            echo "[+] Unlock Success\n"; 
//|        } 
//|    }  
//+---------------------------------------------- 
class PHPLock 
{ 
    const PHPLOCK_TIMEOUT = 1200; 
    static private $pid = null; 
     
    static public function lock(){ 
        $key = self::__getKey(); 
        self::$pid = time(); 
        F($key, self::$pid); 
        return true; 
    } 
     
    static public function unlock(){ 
        $key = self::__getKey(); 
        if(self::$pid){ 
            F($key, null); 
            return true; 
        } 
        return; 
    } 
     
    static public function islocked(){ 
        $key = self::__getKey(); 
        $time = F($key); 
        if(!$time){ 
            return false; 
        }elseif(time() - $time >= self::getTimeout()){ 
            self::unlock(); 
            return false; 
        }else{ 
            return true; 
        } 
    } 
     
    static public function getTimeout(){ 
        $key = str_replace(self::__getKey(), &#39;_Lock&#39;, &#39;_TIMEOUT&#39;); 
        $expire = C($key) ? C($key) : self::PHPLOCK_TIMEOUT; 
        return $expire; 
    } 
     
    static private function __getKey(){ 
        return (defined(&#39;GROUP_NAME&#39;) ? GROUP_NAME.&#39;_&#39; : &#39;&#39;) . MODULE_NAME . &#39;_&#39; . ACTION_NAME . &#39;_Lock&#39;; 
    } 
}
Copy after login


文章链接:

随便收藏,请保留本文地址!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template