acquire($mutexName)) { } 또 다른 {}"."/> acquire($mutexName)) { } 또 다른 {}".">
mutex yii를 어떻게 사용하나요? Mutex
Yii 소스 코드 분석의 Mutex 구성 요소를 사용하면 동시 프로세스가 서로 실행되어 "경합 조건"을 방지할 수 있습니다. 이는 "잠금" 메커니즘을 사용하여 달성됩니다. 잠재적인 동시 스레드는 해당 데이터에 액세스하기 전에 잠금을 획득하여 협력합니다.
관련 권장 사항: yii tutorial
사용 예:
if ($mutex->acquire($mutexName)) { // business logic execution } else { // execution is blocked! }
이것은 기본 클래스이므로 확장되어야 합니다. 실제 잠금 메커니즘을 구현합니다.
소스 코드 구현을 살펴보겠습니다.
<?php /** * @link http://www.yiiframework.com/ * @copyright Copyright (c) 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */ namespace yii\mutex; use yii\base\Component; /** * The Mutex component allows mutual execution of concurrent processes in order to prevent "race conditions". * * This is achieved by using a "lock" mechanism. Each possibly concurrent thread cooperates by acquiring * a lock before accessing the corresponding data. * * Usage example: * * ``` * if ($mutex->acquire($mutexName)) { * // business logic execution * } else { * // execution is blocked! * } * ``` * * This is a base class, which should be extended in order to implement the actual lock mechanism. * * @author resurtm <resurtm@gmail.com> * @since 2.0 */ abstract class Mutex extends Component { /** * @var bool whether all locks acquired in this process (i.e. local locks) must be released automatically * before finishing script execution. Defaults to true. Setting this property to true means that all locks * acquired in this process must be released (regardless of errors or exceptions). */ public $autoRelease = true;//是否自动释放锁 /** * @var string[] names of the locks acquired by the current PHP process. */ private $_locks = [];//当前进程拥有的锁信息 /** * 初始化Mutex组件 */ public function init() { if ($this->autoRelease) {//如果是自动释放锁 $locks = &$this->_locks; //注册shutdown回调函数,在这里做锁的释放动作 register_shutdown_function(function () use (&$locks) { foreach ($locks as $lock) { $this->release($lock); } }); } } /** * Acquires a lock by name. * @param string $name of the lock to be acquired. Must be unique. * @param int $timeout time (in seconds) to wait for lock to be released. Defaults to zero meaning that method will return * false immediately in case lock was already acquired. * @return bool lock acquiring result. */ public function acquire($name, $timeout = 0)//按名称获取锁 { if ($this->acquireLock($name, $timeout)) { $this->_locks[] = $name; return true; } return false; } /** * Releases acquired lock. This method will return false in case the lock was not found. * @param string $name of the lock to be released. This lock must already exist. * @return bool lock release result: false in case named lock was not found.. */ public function release($name)//按名称释放锁 { if ($this->releaseLock($name)) { $index = array_search($name, $this->_locks); if ($index !== false) { unset($this->_locks[$index]); } return true; } return false; } /** * This method should be extended by a concrete Mutex implementations. Acquires lock by name. * @param string $name of the lock to be acquired. * @param int $timeout time (in seconds) to wait for the lock to be released. * @return bool acquiring result. */ abstract protected function acquireLock($name, $timeout = 0);//抽象函数,按名称获取锁 /** * This method should be extended by a concrete Mutex implementations. Releases lock by given name. * @param string $name of the lock to be released. * @return bool release result. */ abstract protected function releaseLock($name);//抽象函数,按名称释放锁 }
기본 클래스로서 하위 클래스는 acquireLock 및 releaseLock 메서드, 즉 특정 잠금 및 잠금 해제 전략을 구현해야 합니다. .
위 내용은 뮤텍스를 사용하는 방법 yii의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!