以下代码是一个程序中的一个类文件(lock.php),先说下他lock.php实际的作用:
在未知(是我不理解)的情况下,会在缓存文件夹 cache下生成一个.lock文件,例如index.php.lock  如果出现这个index.php.lock文件,那网站的首页就打不开了,必须手动删除它,再刷新首页,出现正常的缓存文件index.php 才能正常打开系统。这个故障是不定时发生的,我现在也不理解在什么情况下才会发生,不理解为什么源码的作者为什么要设置这个功能。因为不知道最终来源,也没办法找源码的作者咨询了。前几天,我把这个类文件lock.php删除了,网站一直正常,我以为这样就不会再有index.php.lock 文件了,但今天系统又打不开了,发现正常的cache/index.php缓存不见了,把这个类文件还原,然后再刷新系统,出现正常的cache/index.php,系统也就正常了。lock.php的源码如下:
<?phpclass lock{
private $num,$lock_er,$lock_file,$timeout;
public $islock;
private function file_timeout(){
if(file_exists($this->lock_file)){
if(time()-filemtime($this->lock_file)>=$this->timeout){
@unlink($this->lock_file);
return true;
}
return false;
}else return true;
}
function __construct($num=1,$timeout=3,$wait=false,$wait_time=0,$er = ''){
$this->num=$num;
$er ||$er = $_SERVER['SCRIPT_FILENAME'];
$er = md5($er);
$this->lock_er = $er;
$this->timeout=$timeout;
$this->lock_file=d('./cache/lock/'.$this->lock_er.$this->num.'.lock');
if(file_exists($this->lock_file)){
$this->islock=!$this->file_timeout();
}else $this->islock=false;
if(!$this->islock)touch($this->lock_file);
else {
if($wait){
$wait_start=0;
while(!$this->file_timeout()){
if($wait_time>0){
$wait_start+=100000;
if($wait_start>$wait_time)break;
}
}
file_exists($this->lock_file)&&@unlink($this->lock_file);
touch($this->lock_file);
$this->islock=false;
}
}
}
function __destruct(){
$this->close();
}
public function close(){
!$this->islock&&file_exists($this->lock_file)&&@unlink($this->lock_file);
}
}?>

解决方案 »

  1.   

    将所有 $this->islock= ……的赋值语句都改成true或false(二选一,其中一个放行,另一个锁死)应该就解除这个功能,当然先备份原文件
      

  2.   

    我怎么觉得你这个代码不能正常运行呢?
    if(!$this->islock) touch($this->lock_file);//修改缓存文件的访问时间
    执行的条件是 $this->islock 为假
    而 $this->islock 为假的一个情况是:
    $this->islock=!$this->file_timeout();
    即方法 $this->file_timeout 返回真
    而 $this->file_timeout 方法返回真时有
    @unlink($this->lock_file);//删除缓存文件
    return true;既然缓存文件被删除了,那么 touch($this->lock_file) 不就要报错了吗?
    Warning: touch() [function.touch]: Unable to create file because Invalid argument另外下面还有
    file_exists($this->lock_file)&&@unlink($this->lock_file);//缓存文件存在就删除掉
    touch($this->lock_file);//又在修改不存在的文件的访问时间
      

  3.   


    确实是因为这个文件网站经常出错。出错的现象就是,经常报  php template:' . $tpl_file . ' is parseing,please wait...public static function load_base($tpl_file, $cache_file, $ignoreTime = false)
        {
            $parse = $ignoreTime;
            if (!$ignoreTime)
            {
                if (!file_exists($cache_file))
                {
                    if (file_exists($tpl_file))
                    {
                        $parse = true;
                    }
                    else
                    {
                        common::show_message("Current template file '" . basename($tpl_file) . "' not found or have no access!");
                    }
                }
                else
                {
                    if (file_exists($tpl_file) && (filemtime($tpl_file) > filemtime($cache_file)))
                        $parse = true;
                }
            }
            if ($parse)
            {
                $cacheLockFile = $cache_file . '.lock';
                if (!file_exists($cacheLockFile))
                {
                    self::$subTplList = array();
                    $tpl_file = d($tpl_file);
                    $cache_file = d($cache_file);
                    $cache_file_info = pathinfo($cache_file);
                    common::create_folder($cache_file_info['dirname']);
                    touch($cacheLockFile);
                    $code = file_get_contents($tpl_file);
                    $code = self::parse_code($code);
                    $code = '<?php !defined("IN_JB")&&exit("error");' . substr($code, 6);
                    $refreshCode = '';
                    foreach (self::$subTplList as $subTpl)
                    {
                        $refreshCode && $refreshCode.='||';
                        $refreshCode.='filemtime(\'' . $subTpl . '\')>$_tplModify';
                    }
                    $refreshCode && ($refreshCode = '$_tplModify=filemtime(\'' . $cache_file . '\');if(' . $refreshCode . '){include(template::load_base(\'' . $tpl_file . '\',\'' . $cache_file . '\',true));exit;}') && $code = '<?php ' . $refreshCode . '?>' . $code;
                    $code = parse_php::formatArray($code);
                    file_put_contents($cache_file, $code);
                    self::$js_lib = array();
                    self::$css_lib = array();
                    @unlink($cacheLockFile);
                }
                else
                {
                    exit('php template:' . $tpl_file . ' is parseing,please wait...');
                }
            }
            return $cache_file;
        }报错的时候(php template:index.html is parseing,please wait...),存在一个cache/index.php.lock的文件,我把这个文件删除,刷新首页,就会出现一个cache/index.php 这时网站就正常了。