Cache.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\Lock\Backend;
  8. use Magento\Framework\Cache\FrontendInterface;
  9. /**
  10. * Implementation of the lock manager on the basis of the caching system.
  11. */
  12. class Cache implements \Magento\Framework\Lock\LockManagerInterface
  13. {
  14. /**
  15. * @var FrontendInterface
  16. */
  17. private $cache;
  18. /**
  19. * @param FrontendInterface $cache
  20. */
  21. public function __construct(FrontendInterface $cache)
  22. {
  23. $this->cache = $cache;
  24. }
  25. /**
  26. * @inheritdoc
  27. */
  28. public function lock(string $name, int $timeout = -1): bool
  29. {
  30. return $this->cache->save('1', $name, [], $timeout);
  31. }
  32. /**
  33. * @inheritdoc
  34. */
  35. public function unlock(string $name): bool
  36. {
  37. return $this->cache->remove($name);
  38. }
  39. /**
  40. * @inheritdoc
  41. */
  42. public function isLocked(string $name): bool
  43. {
  44. return (bool)$this->cache->test($name);
  45. }
  46. }