There is a requirement: when generating files, because many users have permission to build, to prevent concurrency, resulting in the resulting error, need to lock the generated process, only allow a user to operate within a time, this time need to use the lock, the operation process lock up. When the cache is used, the cache failure can cause most concurrent requests to penetrate into the database in a moment, and it is also necessary to lock the operation in the same concurrent process.
For the above 2 cases, the current solution is to handle the process of locking mechanism, through the implementation of PHP as follows:
Using the Eaccelerator memory lock and file lock, the principle: Determine whether the system is eaccelerator if there is a memory lock, if not present, then file lock. Depending on the key being brought in, the parallel processing of multiple locks can be implemented, similar to InnoDB row-level locks.
The specific classes are as follows:
Eaccelerator = function_exists ("Eaccelerator_lock"), if (! $this->eaccelerator) {$this->path = $path. $this->_mycrc32 ($name)% $this->hashnum). TXT ';} $this->name = $name;} /** * CRC32 * CRC32 package * @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 the EA memory lock cannot be opened, open the file lock if (! $this->eaccelerator) {// Config directory permission writable $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) {FL Ock ($this->fp, Lock_un); Clearstatcache ();} To close fclose ($this->fp);} Else{return Eaccelerator_unlock ($this->name);}}? >
Use the following:
$lock = new Cachelock (' Key_name '); $lock->lock ();//logic here$lock->unlock ();//You need to be aware of the need to have write permission on the path where the file lock is used.
http://www.bkjia.com/PHPjc/752442.html www.bkjia.com true http://www.bkjia.com/PHPjc/752442.html techarticle There is a need: when generating files, because many users have permissions to build, to prevent concurrency, resulting in an error in the resulting results, need to lock the generated process, ...