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.