DynamoDbLock.php 1.5 KB

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