PHP implementation code for common locks and locks under concurrency _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags crc32
PHP implementation code for common locks and locks under concurrency. This is the case in recent projects. 1. when a file is generated, multiple users have the permission to generate the file to prevent errors in the generated results due to concurrency. in this case, you need to perform the generation process in the latest project.
1. when a file is generated, multiple users have the permission to generate the file to prevent errors in the generated results due to concurrency. you need to lock the generated process, only one user is allowed to perform operations within a period of time. a lock is required to lock the operation process.
2. when the cache is used, the cache failure may cause most concurrent requests to penetrate the database instantly. at this time, you also need to lock the operation in the same concurrent process.

In the above two cases, the solution is to lock the processing process, and implement the following through PHP:
The memory lock and file lock of the Eaccelerator are used. The principle is as follows:
Checks whether the system has an EAccelerator. If yes, the memory lock is used. If no EAccelerator exists, the file lock is used.
Multiple locks can be directly processed in parallel based on different imported keys, similar to the row-level locks of Innodb.

Use:
$ Lock = new CacheLock ('key _ name ');
$ Lock-> lock ();
// Logic here
$ Lock-> unlock ();
// Note that the write permission is required for the path of the file lock.
The specific classes are as follows:

The code is as follows:


/**
* CacheLock process lock is mainly used to obtain the cache of a single process when the cache fails, preventing excessive SQL requests from penetrating the database.
* It is used to solve PHP lock control during concurrency and lock processes through files/eaccelerator
* If the eaccelerator is not used, file lock processing is performed, and corresponding granularity locks are generated in the corresponding directory.
* If eaccelerator is used, it is processed in the memory, and the performance is relatively high.
* Different locks are executed in parallel, similar to mysql innodb row-level locks.
* This class on the basis of sunli phplock made a little modification http://code.google.com/p/phplock
* @ Author yangxinqi
*
*/
Class CacheLock
{
// File lock storage path
Private $ path = null;
// File Handle
Private $ fp = null;
// Lock granularity. the larger the setting, the smaller the granularity.
Private $ hashNum = 100;
// Cache key
Private $ name;
// Whether the eaccelerator flag exists
Private $ eAccelerator = false;
/**
* Constructor
* Import the lock storage path and the cache key name, which can be concurrent
* @ Param string $ path: the directory where the lock is stored, ending "/"
* @ Param string $ name cache key
*/
Public function _ construct ($ name, $ path = 'lock \\')
{
// Determine whether an eAccelerator exists. after eAccelerator is enabled, the memory lock can be performed to improve efficiency.
$ This-> eAccelerator = function_exists ("eaccelerator_lock ");
If (! $ This-> eAccelerator)
{
$ This-> path = $ path. ($ this-> _ mycrc32 ($ name) % $ this-> hashnum1_.'.txt ';
}
$ This-> name = $ name;
}
/**
* Crc32
* Crc32 encapsulation
* @ Param int $ string
* @ Return int
*/
Private function _ mycrc32 ($ string)
{
$ Crc = abs (crc32 ($ string ));
If ($ crc> 0x80000000 ){
$ Crc ^ = 0 xffffffff;
$ Crc + = 1;
}
Return $ crc;
}
/**
* Lock
* Enter description here...
*/
Public function lock ()
{
// If the ea memory lock cannot be enabled, the file lock is enabled.
If (! $ This-> eAccelerator)
{
// Configure the directory permission 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 ();
}
// Close
Fclose ($ this-> fp );
} Else {
Return eaccelerator_unlock ($ this-> name );
}
}
}


This class on the basis of Sun Li class made a small improvement. Specific can see the http://code.google.com/p/phplock thanks Sun shared spirit!

Token 1. when a file is generated, multiple users have the permission to generate the file to prevent errors in the generated results due to concurrent requests. the generated process must be performed...

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.