A 'class' that you cannot understand.

Source: Internet
Author: User
The following code is a class file (lock. php), let's talk about lock. php: when it is unknown (I don't understand it), it will generate a file in the cache folder & nbsp; cache. lock file, such as index. php. lock & nbsp; if there is a 'class' that I cannot understand, explain it.
The following code is a class file (lock. php) in a program. let's talk about its actual function of lock. php:
If it is unknown (I don't understand it), a file is generated in the cache folder. lock file, such as index. php. lock if this index appears. php. lock file, the home page of the website cannot be opened, you must manually delete it, and then refresh the home page, the normal cache file index appears. php can open the system normally.

This fault occurs from time to time, and I do not understand under what circumstances it will happen. I do not understand why the source code author wants to set this function. Because I do not know the final source, I cannot consult the source code author.

A few days ago, I locked this class file. php has been deleted and the website has been operating normally. I thought there would be no more index. php. the file is locked, but the system cannot be opened today, and the normal cache/index is found. php cache disappears. restore the class file and refresh the system. a normal cache/index will appear. php, the system is normal.

The source code of lock. php is as follows:
 
class lock{
private $num,$lock_marker,$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,$marker = ''){
$this->num=$num;
$marker ||$marker = $_SERVER['SCRIPT_FILENAME'];
$marker = md5($marker);
$this->lock_marker = $marker;
$this->timeout=$timeout;
$this->lock_file=d('./cache/lock/'.$this->lock_marker.$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);
}
}

?>

------ Solution --------------------
How do I think your code cannot run normally?
If (! $ This-> islock) touch ($ this-> lock_file); // modify the cache file access time
The execution condition is $ this-> islock is false.
One case where $ this-> islock is false is:
$ This-> islock =! $ This-> file_timeout ();
That is, the method $ this-> file_timeout returns true.
When the $ this-> file_timeout method returns true
@ Unlink ($ this-> lock_file); // deletes the cached file.
Return true;

Since the cached file has been deleted, will touch ($ this-> lock_file) not report an error?
Warning: touch () [function. touch]: Unable to create file because Invalid argument

In addition
File_exists ($ this-> lock_file) & @ unlink ($ this-> lock_file); // delete the cached file if it exists.
Touch ($ this-> lock_file); // modify the access time of a nonexistent file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.