There is such a scenario in a recent project
1. When generating files, because multiple users have permission to generate, to prevent concurrency, resulting in errors in the generated results, the generation process needs to be locked and only allowed When a user performs an operation within a period of time, a lock is needed at this time to lock the operation process.
2. When using cache, cache failure may cause most concurrent requests to penetrate the database in an instant At this time, you may also need to use a lock to lock this operation in the same concurrent process.
For the above two situations, the current solution is to implement a lock mechanism on the processing process, which is implemented through PHP as follows
The memory lock and file lock of Eaccelerator are used. The principle is as follows
Judge whether EAccelerator is installed in the system. If so, use the memory lock. If it does not exist, then perform file lock
This can be achieved according to the different keys brought in. Direct parallel processing of multiple locks, similar to Innodb's row-level lock
is used as follows:
$lock = new CacheLock('key_name');
$lock->lock();
//logic here
$lock->unlock();
//During use, please note that the path where the file lock is located requires write permission.
The specific classes are as follows:
Copy code The code is as follows:
/**
* CacheLock process lock, mainly used for single-process cache acquisition when the cache fails, to prevent excessive SQL requests from penetrating into the database
* Used to solve PHP lock control during concurrency, through the file/eaccelerator Perform inter-process locking
* If eaccelerator is not used, file lock processing will be performed, and locks with corresponding granularity will be generated in the corresponding directory
* If eaccelerator is used, the processing will be performed in memory, and the performance will be relatively high
* Different locks are executed in parallel, similar to mysql innodb's row-level lock
* This class is slightly modified based on sunli's phplock http://code.google.com/p/phplock
* @ author yangxinqi
*
*/
class CacheLock
{
//File lock storage path
private $path = null;
//File handle
private $fp = null;
//Lock granularity, the larger the setting, the smaller the granularity
private $hashNum = 100;
//cache key
private $name;
//Whether the eaccelerator flag exists
private $eAccelerator = false;
/**
* Constructor
* Pass in the storage path of the lock and the name of the cache key, so that concurrency can be performed
* @param string $path The storage directory of the lock, ending with "/"
* @param string $name cache key
* /
public function __construct($name,$path='lock\')
{
//Determine whether eAccelerator exists. After eAccelerator is enabled here, memory locking can be performed to improve efficiency
$this- >eAccelerator = function_exists("eaccelerator_lock");
if(!$this->eAccelerator)
{
$this->path = $path.($this->_mycrc32($ name) % $this->hashNum).'.txt';
}
$this->name = $name;
}
/**
* crc32
* crc32封装
* @param int $string
* @return int
*/
private function _mycrc32($string)
{
$crc = abs (crc32($string));
if ($crc & 0x80000000) {
$crc ^= 0xffffffff;
$crc += 1;
}
return $crc;
}
/**
* Lock
* Enter description here ...
*/
public function lock()
{
//if If the ea memory lock cannot be turned on, turn on the file lock
if(!$this->eAccelerator)
{
//The configuration directory permissions can be written
$this->fp = fopen($ this->path, 'w+');
if($this->fp === false)
{
return false;
}
return flock($this- >fp, LOCK_EX);
}else{
return eaccelerator_lock($this->name);
}
}
/**
* Unlock
* Enter description here ...
*/
public function unlock()
{
if(!$this->eAccelerator)
{
if($this->fp !== false)
{
flock( $this->fp, LOCK_UN);
clearstatcache();
}
//Close
fclose($this->fp);
}else{
return eaccelerator_unlock($this->name);
}
}
}
This class is slightly improved based on Sun Li’s class. For details, please see http://code.google.com/p/phplock. Thank you to Mr. Sun for your sharing spirit!
http://www.bkjia.com/PHPjc/322573.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322573.htmlTechArticleThere is such a scenario in a recent project 1. When generating files, because multiple users have permission to generate them , to prevent concurrency from causing errors in the generated results, the generation process needs to be carried out...