phplock (php process lock) v1.0 beta1_PHP tutorial

WBOY
Release: 2016-07-21 15:43:00
Original
1101 people have browsed it

In web development, we often cache time-consuming operations of our database, but there may be a trap. At the moment when the cache fails, a large number of accesses get cache failure indications, and all go to the backend to query the database, resulting in a large number of databases at the same time. Time-consuming queries may cause problems such as database downtime. This problem is deeply hidden and difficult to find. This project is mainly used to solve the inter-process lock problem of PHP.
Example:

Copy code The code is as follows:

/**
* Test example, open two pages at the same time, you can find the code that only one page can always enter the lock range at the same time
* @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 lock
rn";
ob_end_flush();
flush ();
ob_flush();
sleep (5); //Sleep for 20 seconds, simulate concurrent operations
echo "Execution completed
rn";
$lock-> ;unlock ();
$lock->endLock ();
echo "Release lock completed
rn";
/**
* 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) {
//The cache does not exist, start locking
$lock = new PHPLock ('lock/', $key);
$lock->startLock ();
$lock->startLock ();
//Try to determine whether there is data in the cache. There may have been access to rebuild the cache, so there is no need to query the database again
$cache=getCache();
if(!$cache){
//Database query operation, the code is omitted
$data=$dbdata;
setCache($key,$data );
}
//Release lock
$lock->unlock ();
$lock->endLock ();
}
?>

Recommended related articles
Analysis and research on PHP process locking problem

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320818.htmlTechArticleIn web development, we often cache time-consuming operations of our database, but there may be a trap in caching At the moment of failure, a large number of accesses were marked as cache invalid, all...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!