MemcachedLock.php 1.4 KB

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