Add Lock on PHP concurrent operation

Source: Internet
Author: User
Tags crc32
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.
  • 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.