PHP进程锁类PHPLock程序代码
发布:smiling 来源: PHP粉丝网 添加日期:2014-06-15 23:33:21 浏览: 评论:0
为了更好的控制php程序同时操作的一些问题我整理了一个进程锁的类我们可以利用这个进程锁实现程序的控制,程序代码如下:
- <?php
- //+----------------------------------------------
- //| Usage:
- //+----------------------------------------------
- //| public function _initialize(){
- //| import('@.Util.PHPLock');
- //|
- //| 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(), '_Lock', '_TIMEOUT');
- $expire = C($key) ? C($key) : self::PHPLOCK_TIMEOUT;
- return $expire;
- }
- static private function __getKey(){
- return (defined('GROUP_NAME') ? GROUP_NAME.'_' : '') . MODULE_NAME . '_' . ACTION_NAME . '_Lock';
- }
- }
- ?>
Tags: PHP进程锁类 PHPLock程序
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)