MysqlMutex.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @link https://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license https://www.yiiframework.com/license/
  6. */
  7. namespace yii\mutex;
  8. use yii\base\InvalidConfigException;
  9. use yii\db\Expression;
  10. /**
  11. * MysqlMutex implements mutex "lock" mechanism via MySQL locks.
  12. *
  13. * Application configuration example:
  14. *
  15. * ```
  16. * [
  17. * 'components' => [
  18. * 'db' => [
  19. * 'class' => 'yii\db\Connection',
  20. * 'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
  21. * ]
  22. * 'mutex' => [
  23. * 'class' => 'yii\mutex\MysqlMutex',
  24. * ],
  25. * ],
  26. * ]
  27. * ```
  28. *
  29. * @see Mutex
  30. *
  31. * @author resurtm <resurtm@gmail.com>
  32. * @since 2.0
  33. */
  34. class MysqlMutex extends DbMutex
  35. {
  36. /**
  37. * @var Expression|string|null prefix value. If null (by default) then connection's current database name is used.
  38. * @since 2.0.47
  39. */
  40. public $keyPrefix = null;
  41. /**
  42. * Initializes MySQL specific mutex component implementation.
  43. * @throws InvalidConfigException if [[db]] is not MySQL connection.
  44. */
  45. public function init()
  46. {
  47. parent::init();
  48. if ($this->db->driverName !== 'mysql') {
  49. throw new InvalidConfigException('In order to use MysqlMutex connection must be configured to use MySQL database.');
  50. }
  51. if ($this->keyPrefix === null) {
  52. $this->keyPrefix = new Expression('DATABASE()');
  53. }
  54. }
  55. /**
  56. * Acquires lock by given name.
  57. * @param string $name of the lock to be acquired.
  58. * @param int $timeout time (in seconds) to wait for lock to become released.
  59. * @return bool acquiring result.
  60. * @see https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_get-lock
  61. */
  62. protected function acquireLock($name, $timeout = 0)
  63. {
  64. return $this->db->useMaster(function ($db) use ($name, $timeout) {
  65. /** @var \yii\db\Connection $db */
  66. $nameData = $this->prepareName();
  67. return (bool)$db->createCommand(
  68. 'SELECT GET_LOCK(' . $nameData[0] . ', :timeout), :prefix',
  69. array_merge(
  70. [':name' => $this->hashLockName($name), ':timeout' => $timeout, ':prefix' => $this->keyPrefix],
  71. $nameData[1]
  72. )
  73. )->queryScalar();
  74. });
  75. }
  76. /**
  77. * Releases lock by given name.
  78. * @param string $name of the lock to be released.
  79. * @return bool release result.
  80. * @see https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_release-lock
  81. */
  82. protected function releaseLock($name)
  83. {
  84. return $this->db->useMaster(function ($db) use ($name) {
  85. /** @var \yii\db\Connection $db */
  86. $nameData = $this->prepareName();
  87. return (bool)$db->createCommand(
  88. 'SELECT RELEASE_LOCK(' . $nameData[0] . '), :prefix',
  89. array_merge(
  90. [':name' => $this->hashLockName($name), ':prefix' => $this->keyPrefix],
  91. $nameData[1]
  92. )
  93. )->queryScalar();
  94. });
  95. }
  96. /**
  97. * Prepare lock name
  98. * @return array expression and params
  99. * @since 2.0.48
  100. */
  101. protected function prepareName()
  102. {
  103. $params = [];
  104. $expression = 'SUBSTRING(CONCAT(:prefix, :name), 1, 64)';
  105. if ($this->keyPrefix instanceof Expression) {
  106. $expression = strtr($expression, [':prefix' => $this->keyPrefix->expression]);
  107. $params = $this->keyPrefix->params;
  108. }
  109. return [$expression, $params];
  110. }
  111. /**
  112. * Generate hash for lock name to avoid exceeding lock name length limit.
  113. *
  114. * @param string $name
  115. * @return string
  116. * @since 2.0.16
  117. * @see https://github.com/yiisoft/yii2/pull/16836
  118. */
  119. protected function hashLockName($name)
  120. {
  121. return sha1($name);
  122. }
  123. }