CacheLock.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Illuminate\Cache;
  3. class CacheLock extends Lock
  4. {
  5. /**
  6. * The cache store implementation.
  7. *
  8. * @var \Illuminate\Contracts\Cache\Store
  9. */
  10. protected $store;
  11. /**
  12. * Create a new lock instance.
  13. *
  14. * @param \Illuminate\Contracts\Cache\Store $store
  15. * @param string $name
  16. * @param int $seconds
  17. * @param string|null $owner
  18. * @return void
  19. */
  20. public function __construct($store, $name, $seconds, $owner = null)
  21. {
  22. parent::__construct($name, $seconds, $owner);
  23. $this->store = $store;
  24. }
  25. /**
  26. * Attempt to acquire the lock.
  27. *
  28. * @return bool
  29. */
  30. public function acquire()
  31. {
  32. if (method_exists($this->store, 'add') && $this->seconds > 0) {
  33. return $this->store->add(
  34. $this->name, $this->owner, $this->seconds
  35. );
  36. }
  37. if (! is_null($this->store->get($this->name))) {
  38. return false;
  39. }
  40. return ($this->seconds > 0)
  41. ? $this->store->put($this->name, $this->owner, $this->seconds)
  42. : $this->store->forever($this->name, $this->owner);
  43. }
  44. /**
  45. * Release the lock.
  46. *
  47. * @return bool
  48. */
  49. public function release()
  50. {
  51. if ($this->isOwnedByCurrentProcess()) {
  52. return $this->store->forget($this->name);
  53. }
  54. return false;
  55. }
  56. /**
  57. * Releases this lock regardless of ownership.
  58. *
  59. * @return void
  60. */
  61. public function forceRelease()
  62. {
  63. $this->store->forget($this->name);
  64. }
  65. /**
  66. * Returns the owner value written into the driver for this lock.
  67. *
  68. * @return mixed
  69. */
  70. protected function getCurrentOwner()
  71. {
  72. return $this->store->get($this->name);
  73. }
  74. }