Add-Lock under PHP concurrent operation
How PHP solves multiple threads reading and writing the same file
As we all know, PHP is not a multi-threading concept, although we can still use the "imperfect" method to simulate multithreading. simply put, the queue is processed. This is done by locking and unlocking the file. When a file is manipulated by a user, the file is locked, and the other user can only wait, which is not perfect enough, but it can also meet some of the less demanding applications.
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) {if (!is_dir ($path)) {mkdir ($path, 0777);} $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.