MemcacheLockingProvider.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Lock;
  23. use OCP\IMemcacheTTL;
  24. use OCP\Lock\LockedException;
  25. use OCP\IMemcache;
  26. class MemcacheLockingProvider extends AbstractLockingProvider {
  27. /**
  28. * @var \OCP\IMemcache
  29. */
  30. private $memcache;
  31. /**
  32. * @param \OCP\IMemcache $memcache
  33. * @param int $ttl
  34. */
  35. public function __construct(IMemcache $memcache, $ttl = 3600) {
  36. $this->memcache = $memcache;
  37. $this->ttl = $ttl;
  38. }
  39. private function setTTL($path) {
  40. if ($this->memcache instanceof IMemcacheTTL) {
  41. $this->memcache->setTTL($path, $this->ttl);
  42. }
  43. }
  44. /**
  45. * @param string $path
  46. * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
  47. * @return bool
  48. */
  49. public function isLocked($path, $type) {
  50. $lockValue = $this->memcache->get($path);
  51. if ($type === self::LOCK_SHARED) {
  52. return $lockValue > 0;
  53. } else if ($type === self::LOCK_EXCLUSIVE) {
  54. return $lockValue === 'exclusive';
  55. } else {
  56. return false;
  57. }
  58. }
  59. /**
  60. * @param string $path
  61. * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
  62. * @throws \OCP\Lock\LockedException
  63. */
  64. public function acquireLock($path, $type) {
  65. if ($type === self::LOCK_SHARED) {
  66. if (!$this->memcache->inc($path)) {
  67. throw new LockedException($path);
  68. }
  69. } else {
  70. $this->memcache->add($path, 0);
  71. if (!$this->memcache->cas($path, 0, 'exclusive')) {
  72. throw new LockedException($path);
  73. }
  74. }
  75. $this->setTTL($path);
  76. $this->markAcquire($path, $type);
  77. }
  78. /**
  79. * @param string $path
  80. * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
  81. */
  82. public function releaseLock($path, $type) {
  83. if ($type === self::LOCK_SHARED) {
  84. if ($this->getOwnSharedLockCount($path) === 1) {
  85. $removed = $this->memcache->cad($path, 1); // if we're the only one having a shared lock we can remove it in one go
  86. if (!$removed) { //someone else also has a shared lock, decrease only
  87. $this->memcache->dec($path);
  88. }
  89. } else {
  90. // if we own more than one lock ourselves just decrease
  91. $this->memcache->dec($path);
  92. }
  93. } else if ($type === self::LOCK_EXCLUSIVE) {
  94. $this->memcache->cad($path, 'exclusive');
  95. }
  96. $this->markRelease($path, $type);
  97. }
  98. /**
  99. * Change the type of an existing lock
  100. *
  101. * @param string $path
  102. * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE
  103. * @throws \OCP\Lock\LockedException
  104. */
  105. public function changeLock($path, $targetType) {
  106. if ($targetType === self::LOCK_SHARED) {
  107. if (!$this->memcache->cas($path, 'exclusive', 1)) {
  108. throw new LockedException($path);
  109. }
  110. } else if ($targetType === self::LOCK_EXCLUSIVE) {
  111. // we can only change a shared lock to an exclusive if there's only a single owner of the shared lock
  112. if (!$this->memcache->cas($path, 1, 'exclusive')) {
  113. throw new LockedException($path);
  114. }
  115. }
  116. $this->setTTL($path);
  117. $this->markChange($path, $targetType);
  118. }
  119. }