search
Homephp教程php手册PHP中使用Memache作为进程锁的操作类分享,phpmemache

PHP中使用Memache作为进程锁的操作类分享,phpmemache

<&#63;php

// 使用Memache 作为进程锁 

class lock_processlock{
	
	
	// key 的前缀
	protected $sLockKeyPre;
	// 重试间隔
 protected $iLockRetryInterval;
	//重试次数 
 protected $iLockRetryCount;
	//锁的过期时间
 protected $iLockCacheTimeout;
 // 锁过期后的回调函数
 protected $onLockTimeoutFunc;
	// memache 的实例
 protected $oMemcache;
	// 存储memcache失败后重试次数 
 protected $iMemcacheRetryCount;
	
	
	
	
	 public function __construct ($onLockTimeoutFunc=NULL) {
  $aLockConfig = get_config('', 'lock');
  $this->sLockKeyPre = self::LOCK_KEY_PRE;
  $this->iLockRetryInterval = self::LOCK_RETRY_INTERVAL;
  $this->iLockRetryCount =self::LOCK_RETRY_COUNT;
  $this->iLockCacheTimeout = self::LOCK_CACHE_TIMEOUT;
  $this->iMemcacheRetryCount = self::LOCK_CACHE_TIMEOUT;
		if(!$onLockTimeoutFunc){
			// 如果加锁不成功则调用回调函数,如果没有回调函数,使用本类中所带的 
			$onLockTimeoutFunc ='onLockTimeout' ; 
		}
  $this->onLockTimeoutFunc = $onLockTimeoutFunc;
 }
	
	
	/**
	连接memcache 服务器 
	*/
	public function connect() {
		if (! isset ( $this->oMemcache )) {
			$this->oMemcache = new Memcache ();
			$this->oMemcache->connect ( '127.0.0.1', 11211 );
		}
		return $this->oMemcache;
	}
	
	
	/*
	向MeMcache中添加 key
	*/
	public addMemcache($sKey, $sValue, $iTimeout){
		
		for($i= 0 ; $i<$this->iMemcacheRetryCount){
			$bRes = $this->oMemcache->add($sKey, $sValue, $iTimeout);
			if($bRes){
				return true ; 
			}
				// 如果加锁不成功,sleep 之后,从新加锁
			usleep($this->iLockRetryInterval*1000);
			
		}
		return false ; 
		
	}
	
	
	/*
	加锁 
	*/
	public function lock($sLockID){
		
		$oMemcache = $this->connect();
	 $sKey = $this->sLockKeyPre . $sLockID;
		
		// 加锁如果不成功可以多试几次 
		
		for($i = 0 ; $i <$this->iLockRetryCount ; $i++){
			
			// 这里设置value 的值可以随便设置 
			if($this->addMemcache($sKey,'1',$this->iLockCacheTimeout)){
				return true ; 
			}
			
			// 如果加锁不成功,sleep 之后,从新加锁
			usleep($this->iLockRetryInterval*1000);
			
		}
		
		// 若还不成功,则加锁失败,调用回调函数,.也就是失败后需要处理的操作 
		if(is_callable($this->onLockTimeoutFunc)){
			// 调用函数 
			call_user_func($this->onLockTimeoutFunc); 
		}
		
	}
	
	
	/*
	解锁操作 
	*/
	public function unlock($sLockID){
		
		$oMemcache = $this->connect();
	 $sKey = $this->sLockKeyPre . $sLockID;
		// 删除key
		return $this->oMemcache->delete($sKey);
		
	}
	
	
	/**
	如果加锁不成功,则执行如下操作 
	*/
	 public function onLockTimeout(){
		 
		 echo ("加锁超时");
	 }
  
}



// 应用实例 

 $oLock = new lock_processlock();
 $lockResource = "test";
 // 加锁
 $oLock->lock($lockResource);
 // 解锁
 $oLock->unlock($lockResource);



Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.