时间:2021-07-01 10:21:17 帮助过:10人阅读
针对以上的2种情况,现在的解决方法是对处理过程进行锁机制,通过PHP实现如下:
用到了Eaccelerator的内存锁和文件锁,原理:判断系统中是否安了EAccelerator如果有则使用内存锁,如果不存在,则进行文件锁。根据带入的key的不同可以实现多个锁直接的并行处理,类似Innodb的行级锁。
具体类如下:
eAccelerator = function_exists("eaccelerator_lock");
if(!$this->eAccelerator)
{
$this->path = $path.($this->_mycrc32($name) % $this->hashNum).'.txt';
}
$this->name = $name;
}
/**
* crc32
* crc32封装
* @param int $string
* @return int
*/
private function _mycrc32($string)
{
$crc = abs (crc32($string));
if ($crc & 0x80000000) {
$crc ^= 0xffffffff;
$crc += 1;
}
return $crc;
}
/**
* 加锁
* Enter description here ...
*/
public function lock()
{
//如果无法开启ea内存锁,则开启文件锁
if(!$this->eAccelerator)
{
//配置目录权限可写
$this->fp = fopen($this->path, 'w+');
if($this->fp === false)
{
return false;
}
return flock($this->fp, LOCK_EX);
}else{
return eaccelerator_lock($this->name);
}
}
/**
* 解锁
* Enter description here ...
*/
public function unlock()
{
if(!$this->eAccelerator)
{
if($this->fp !== false)
{
flock($this->fp, LOCK_UN);
clearstatcache();
}
//进行关闭
fclose($this->fp);
}else{
return eaccelerator_unlock($this->name);
}
}
}
?>
使用如下:
$lock = new CacheLock('key_name');
$lock->lock();
//logic here
$lock->unlock();
//使用过程中需要注意下文件锁所在路径需要有写权限.
http://www.bkjia.com/PHPjc/752442.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752442.htmlTechArticle有这么一个需求:生成文件的时候,由于多用户都有权限进行生成,防止并发下,导致生成的结果出现错误,需要对生成的过程进行加锁,...