12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace yii\mutex;
- use yii\base\InvalidConfigException;
- class MysqlMutex extends DbMutex
- {
-
- public function init()
- {
- parent::init();
- if ($this->db->driverName !== 'mysql') {
- throw new InvalidConfigException('In order to use MysqlMutex connection must be configured to use MySQL database.');
- }
- }
-
- protected function acquireLock($name, $timeout = 0)
- {
- return $this->db->useMaster(function ($db) use ($name, $timeout) {
-
- return (bool) $db->createCommand(
- 'SELECT GET_LOCK(:name, :timeout)',
- [':name' => $this->hashLockName($name), ':timeout' => $timeout]
- )->queryScalar();
- });
- }
-
- protected function releaseLock($name)
- {
- return $this->db->useMaster(function ($db) use ($name) {
-
- return (bool) $db->createCommand(
- 'SELECT RELEASE_LOCK(:name)',
- [':name' => $this->hashLockName($name)]
- )->queryScalar();
- });
- }
-
- protected function hashLockName($name) {
- return sha1($name);
- }
- }
|