Lock.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Illuminate\Cache;
  3. use Illuminate\Contracts\Cache\Lock as LockContract;
  4. use Illuminate\Contracts\Cache\LockTimeoutException;
  5. use Illuminate\Support\InteractsWithTime;
  6. use Illuminate\Support\Sleep;
  7. use Illuminate\Support\Str;
  8. abstract class Lock implements LockContract
  9. {
  10. use InteractsWithTime;
  11. /**
  12. * The name of the lock.
  13. *
  14. * @var string
  15. */
  16. protected $name;
  17. /**
  18. * The number of seconds the lock should be maintained.
  19. *
  20. * @var int
  21. */
  22. protected $seconds;
  23. /**
  24. * The scope identifier of this lock.
  25. *
  26. * @var string
  27. */
  28. protected $owner;
  29. /**
  30. * The number of milliseconds to wait before re-attempting to acquire a lock while blocking.
  31. *
  32. * @var int
  33. */
  34. protected $sleepMilliseconds = 250;
  35. /**
  36. * Create a new lock instance.
  37. *
  38. * @param string $name
  39. * @param int $seconds
  40. * @param string|null $owner
  41. * @return void
  42. */
  43. public function __construct($name, $seconds, $owner = null)
  44. {
  45. if (is_null($owner)) {
  46. $owner = Str::random();
  47. }
  48. $this->name = $name;
  49. $this->owner = $owner;
  50. $this->seconds = $seconds;
  51. }
  52. /**
  53. * Attempt to acquire the lock.
  54. *
  55. * @return bool
  56. */
  57. abstract public function acquire();
  58. /**
  59. * Release the lock.
  60. *
  61. * @return bool
  62. */
  63. abstract public function release();
  64. /**
  65. * Returns the owner value written into the driver for this lock.
  66. *
  67. * @return string
  68. */
  69. abstract protected function getCurrentOwner();
  70. /**
  71. * Attempt to acquire the lock.
  72. *
  73. * @param callable|null $callback
  74. * @return mixed
  75. */
  76. public function get($callback = null)
  77. {
  78. $result = $this->acquire();
  79. if ($result && is_callable($callback)) {
  80. try {
  81. return $callback();
  82. } finally {
  83. $this->release();
  84. }
  85. }
  86. return $result;
  87. }
  88. /**
  89. * Attempt to acquire the lock for the given number of seconds.
  90. *
  91. * @param int $seconds
  92. * @param callable|null $callback
  93. * @return mixed
  94. *
  95. * @throws \Illuminate\Contracts\Cache\LockTimeoutException
  96. */
  97. public function block($seconds, $callback = null)
  98. {
  99. $starting = $this->currentTime();
  100. while (! $this->acquire()) {
  101. Sleep::usleep($this->sleepMilliseconds * 1000);
  102. if ($this->currentTime() - $seconds >= $starting) {
  103. throw new LockTimeoutException;
  104. }
  105. }
  106. if (is_callable($callback)) {
  107. try {
  108. return $callback();
  109. } finally {
  110. $this->release();
  111. }
  112. }
  113. return true;
  114. }
  115. /**
  116. * Returns the current owner of the lock.
  117. *
  118. * @return string
  119. */
  120. public function owner()
  121. {
  122. return $this->owner;
  123. }
  124. /**
  125. * Determines whether this lock is allowed to release the lock in the driver.
  126. *
  127. * @return bool
  128. */
  129. public function isOwnedByCurrentProcess()
  130. {
  131. return $this->getCurrentOwner() === $this->owner;
  132. }
  133. /**
  134. * Specify the number of milliseconds to sleep in between blocked lock acquisition attempts.
  135. *
  136. * @param int $milliseconds
  137. * @return $this
  138. */
  139. public function betweenBlockedAttemptsSleepFor($milliseconds)
  140. {
  141. $this->sleepMilliseconds = $milliseconds;
  142. return $this;
  143. }
  144. }