Phplock of PHP process lock v1.0 beta1

  • 2020-03-31 19:23:54
  • OfStack

In web development, we often cache our database time-consuming operations, but there may be a trap, in the moment of cache invalidation, a large number of access to the cache invalidation mark, all to the backend database query, resulting in a large number of database time-consuming query, database downtime and other problems. This problem is deeply hidden and not easy to find. This project is mainly used to solve the interprocess locking problem in PHP.
Example:
 
<?php 
/** 
*  Test the example, open two pages at the same time, you can find only one page at a time into the lock interval of the 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> Into the lock </span><br />rn"; 
ob_end_flush(); 
flush(); 
ob_flush(); 
sleep ( 5 ); //Sleep for 20 seconds, simulating concurrent operations
echo " completes <br />rn"; 
$lock->unlock (); 
$lock->endLock (); 
echo " Release lock completed <br />rn"; 
 
function getCache($key) { 
    return $cache; 
} 
 
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 there is data in the cache. You may already have access to the rebuild cache, so you do not need to query the database again
    $cache=getCache(); 
    if(!$cache){ 
        //Database query operation, code omitted
        $data=$dbdata; 
        setCache($key,$data); 
    } 
    //Release the lock
    $lock->unlock (); 
    $lock->endLock (); 
} 
?> 

Recommended articles
(link: #)

Related articles: