<?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->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->eacce Lerator) {if ($this->FP!== false) {flock ($this->fp, Lock_un); Clearstatcache (); }//To close fclose ($this->FP); }else{return Eaccelerator_unlock ($this->name); } }}