1
0

CADTrait.php 794 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Memcache;
  8. trait CADTrait {
  9. abstract public function get($key);
  10. abstract public function remove($key);
  11. abstract public function add($key, $value, $ttl = 0);
  12. /**
  13. * Compare and delete
  14. *
  15. * @param string $key
  16. * @param mixed $old
  17. * @return bool
  18. */
  19. public function cad($key, $old) {
  20. //no native cas, emulate with locking
  21. if ($this->add($key . '_lock', true)) {
  22. if ($this->get($key) === $old) {
  23. $this->remove($key);
  24. $this->remove($key . '_lock');
  25. return true;
  26. } else {
  27. $this->remove($key . '_lock');
  28. return false;
  29. }
  30. } else {
  31. return false;
  32. }
  33. }
  34. }