Phplock (PHP process Lock) v1.0 beta1_php Tips

Source: Internet
Author: User
In web development, we often cache the time consuming operation of our database, but there may be a trap, in the cache failure of the moment, a large number of access to the cache failure of the indicator, all go to the backend query database, resulting in a large number of database time-consuming query, the emergence of database downtime and so on This problem is hidden deep and is not easy to find. This project is mainly used to solve the inter-process lock problem in PHP.
Example:
Copy Code code as follows:

<?php
/**
* Test example, open two pages at the same time, you can find that always can 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 "<span> Enter lock </span><br/>\r\n";
Ob_end_flush ();
Flush ();
Ob_flush ();
Sleep (5); Hibernate 20 seconds to simulate concurrent operations
echo "Execution completed <br/>\r\n";
$lock->unlock ();
$lock->endlock ();
echo "Release lock completes <br/>\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, start locking
$lock = new Phplock (' lock/', $key);
$lock->startlock ();
$lock->startlock ();
Try to determine if the cache has data, you may already have access to rebuild the 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-in problem

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.