PHP Process Lock related content

Source: Internet
Author: User
<?php/** * Cachelock process lock, mainly used for cache invalidation of single process cache acquisition, to prevent excessive SQL requests through the database * to solve PHP lock control in the concurrency, through the file/eaccelerator inter-process lock * If you do not use Eaccelerator to do file lock processing, will do the corresponding directory to generate corresponding granularity of the lock * using eaccelerator in memory processing, performance is relatively high * different locks in parallel execution, similar to the MySQL InnoDB row-level lock * This class made a few changes on the basis of Sunli's Phplock http://code.google.com/p/phplock * @author Yangxinqi * */namespace common\business;class    phplock{//File lock store path private $path = null;    File handle private $fp = NULL;    Lock granularity, setting the larger the granularity the smaller the private $hashNum = 100;    Cache key Private $name;    Whether there is a eaccelerator flag private $eAccelerator = false; /** * constructor * The storage path of the incoming lock, and the name of the cache key, allowing concurrent * @param string $path Lock directory, ending with "/" @param string $name CAC He key */Public function __construct ($name, $path = ' lock\\ ') {$app _path = str_replace (' thinkphp/', ', TH        Ink_path);        $is _cli = Php_sapi_name ();        if ($is _cli = = ' CLI ' | | $is _cli = = ' Cli_server ') {$app _path = Trim (App_path, '. ');         } else {   $app _path = $app _path.        Trim (App_path, '. '); } $path = $app _path. '        /common/business/lockfile/';            if (!file_exists ($path)) {mkdir ($path);        chmod ($path, 777);        }//$path. = '/protected/runtime/cache/'; Determine if there is a eaccelerator, eaccelerator can be improved after enabling the memory lock $this->eaccelerator = function_exists ("Eaccelerator_lock")        ; if (! $this->eaccelerator) {//$this->path = $path. ( $this->_mycrc32 ($name)% $this->hashnum).            TXT '; $this->path = $path. SHA1 ($name).        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, theOpen file Lock if (! $this->eaccelerator) {//config directory permission writable $this-&GT;FP = fopen ($this->path, ' W            +');            if ($this-&GT;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->eacce                Lerator) {if ($this-&GT;FP!== false) {flock ($this->fp, Lock_un);            Clearstatcache ();        }//To close fclose ($this-&GT;FP);        }else{return Eaccelerator_unlock ($this->name); }    }}

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.