1 .在该服务器下,是否使用文件锁定机制? php
//*
用于解决PHP的并发锁定控制,在不同的锁定之间并行执行,如mysql innodb的行级锁定
*/
类文件锁定{
//文件锁定存储路径
私有$ path='/dev/shm ';
//文件句柄
private $fp=' ';
//锁定文件
私有$ lockfile=' ';
//*
*构造函数
* @param string $name锁定键
*/
公共函数_ _ construct ($ name ) )。
{
$ this-lockfile=$ this-path.MD5 ($ name ).'.lock ';
}
//*
*锁住
*/
公共函数锁定(
{
$this-FP=fopen($this-lockfile,' a ';
if($this-FP===false ) {
返回假;
}
register _ shut down _ function (array ) $this,' unlock ' );
返回流($ this-FP,LOCK_EX ); 获取//排他锁定
}
//*
*解锁(尽可能手动解锁) ) ) ) ) ) )。
*/
公共函数解锁(
{
if($this-FP!==false
@flock($this-FP,LOCK_UN );
clearstatcache (;
}
@fclose($this-FP );
@unlink($this-lockfile;
}
}
2 .对于多服务器,使用redis incr机制(如果incr返回值不为1,则确定是否为die )? php
名称空间锁定;
用户处理程序(redis handler;
类就绪锁定
{
const key _ lock=' lock : % s : % s '; //tag uid
私密性$ lockey=' ';
私密函数_ _ construct ($ tag,$uid ) )。
{
$ this-lockey=sprintf (self :3360 key _ lock,$tag,$uid );
}
publicstaticfunctioninstance ($ tag,$uid ) )。
{
$self=newself($tag,$uid );
返回$ self-lock (;
}
公共函数锁定(
{
$ incr=redis handler :3360 getredislock (-incr ) $this-lockey );
if($InCr!=1) {
返回假;
}
redis handler :3360 getredislock ()-expire ) $this-lockey,10 );
register _ shut down _ function (array ) $this,' unlock ' );
返回真;
}
公共函数解锁(
{
returnredishandler :3360 getredislock (-p expire ) $this-lockey,1 );
}
}
redis handler :3360 getredislock是redis单实例模式实例