MemcachedLock.php 960B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace Illuminate\Cache;
  3. use Illuminate\Contracts\Cache\Lock as LockContract;
  4. class MemcachedLock extends Lock implements LockContract
  5. {
  6. /**
  7. * The Memcached instance.
  8. *
  9. * @var \Memcached
  10. */
  11. protected $memcached;
  12. /**
  13. * Create a new lock instance.
  14. *
  15. * @param \Memcached $memcached
  16. * @param string $name
  17. * @param int $seconds
  18. * @return void
  19. */
  20. public function __construct($memcached, $name, $seconds)
  21. {
  22. parent::__construct($name, $seconds);
  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, 1, $this->seconds
  34. );
  35. }
  36. /**
  37. * Release the lock.
  38. *
  39. * @return void
  40. */
  41. public function release()
  42. {
  43. $this->memcached->delete($this->name);
  44. }
  45. }