There's this scenario in the recent project
1. When generating a file, because many users have the right to generate, to prevent concurrent, resulting in the result of the production of errors, need to build the process of locking, only allow a user in a time to operate, this time need to use the lock, the operation of the process of locking up.
2. Cache failure may cause instantaneous majority of concurrent requests to penetrate the database at the time of cache use, and it may be necessary to lock the operation in the same concurrent process.
For the above 2 cases, now the solution is to the process of locking mechanism, through the PHP implementation of the following
Use the Eaccelerator memory lock and file lock, the principle is as follows
Determine if the system is eaccelerator if there is a memory lock, if it does not exist, then file lock
Multiple lock direct parallel processing can be achieved based on the different key, similar to InnoDB row-level lock
Use the following:
$lock = new Cachelock (' Key_name ');
$lock->lock ();
Logic here
$lock->unlock ();
In use, you need to be aware that the path where the file lock is located requires write permission.
The specific classes are as follows:
Copy Code code as follows:
<?php
/**
* Cachelock process lock, mainly used for cache failure when the single process cache access, to prevent excessive SQL requests penetrate to the database
* To solve the PHP in the concurrency of the lock control, through the file/eaccelerator to process between the lock
* If you do not use Eaccelerator to do file lock processing, will be the corresponding directory to produce the corresponding granularity of the lock
* Use of eaccelerator in memory processing, relatively high performance
* Parallel execution between different locks, similar to MySQL InnoDB row-level locks
* This class made a little modification on the basis of the Sunli Phplock Http://code.google.com/p/phplock
* @author Yangxinqi
*
*/
Class Cachelock
{
File Lock Store Path
private $path = null;
File handle
private $fp = null;
The size of the lock, the larger the size of the smaller
Private $hashNum = 100;
Cache key
Private $name;
Is there a eaccelerator flag
Private $eAccelerator = false;
/**
* Constructor
* The storage path of the incoming lock and the name of the cache key, which can be concurrent
* @param string $path The storage directory of the lock, ending with "/"
* @param string $name cache key
*/
Public function __construct ($name, $path = ' lock\\ ')
{
Determine if there is a eaccelerator, the memory lock can be improved after Eaccelerator is enabled
$this->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;
}
/**
* Add lock
* Enter description here ...
*/
Public Function Lock ()
{
Open a file lock if the EA memory lock cannot be opened
if (! $this->eaccelerator)
{
Configure directory permissions to write
$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)
{
Flock ($this->fp, Lock_un);
Clearstatcache ();
}
To close
Fclose ($this->FP);
}else{
Return Eaccelerator_unlock ($this->name);
}
}
}
This class in the Sun Li class based on a small point of improvement. You can see Http://code.google.com/p/phplock thanks to the sharing spirit of the students!