Bigger than bigger phplockphp process lock V10 Beta1

Source: Internet
Author: User
In web development, we often cache time-consuming operations of our database, but there may be a trap, in the event of a cache failure, a large number of access to cache invalidation of the flag, all go back to query the database, resulting in a large number of database time-consuming query, the occurrence of database downtime and other problems. This problem is hidden deep and is not easy to find. This project is mainly used to solve the inter-process lock problem of PHP.
Example:

Copy the Code code as follows:


/**
* Test example, open two pages at the same time, you can find always only one page into the lock interval code
* @link http://code.google.com/p/phplock/
* @author Sunli
* @svnversion $Id: test.php 2 2009-11-24 07:14:27z sunli1223 $
* @version v1.0 Beta1
* @license Apache License Version 2.0
* @copyright sunli1223@gmail.com
*/
Require ' class.phplock.php ';
$lock = new Phplock (' lock/', ' lockname ');
$lock->startlock ();
$lock->startlock ();
Process Code
echo "Enter the lock
\ r \ n ";
Ob_end_flush ();
Flush ();
Ob_flush ();
Sleep (5); Hibernate for 20 seconds, simulating concurrent operations
echo "Execution complete
\ r \ n ";
$lock->unlock ();
$lock->endlock ();
echo "Release lock complete
\ r \ n ";
/**
* Cache operation
*
* @return $array
*/
function GetCache ($key) {
return $cache;
}
/**
* Set Cache
*
* @param string $key
* @param array $value
*/
function Setcache ($key, $value) {
}
$cache =getcache ($key);
if (! $cache) {
Cache does not exist and locks are started
$lock = new Phplock (' lock/', $key);
$lock->startlock ();
$lock->startlock ();
Try to determine if the cache has data, you may have access to the rebuild cache, you do not need to query the database again
$cache =getcache ();
if (! $cache) {
Database query operation, code omitted
$data = $dbdata;
Setcache ($key, $data);
}
Release lock
$lock->unlock ();
$lock->endlock ();
}
?>


Related articles recommended
Analysis and research of PHP process lock problem

The above describes the bigger than bigger phplockphp process lock V10 beta1, including bigger than bigger content, I hope the PHP tutorial interested in a friend helpful.

  • 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.