|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Lock\Store; |
| 13 | + |
| 14 | +use Symfony\Component\Lock\Exception\LockConflictedException; |
| 15 | +use Symfony\Component\Lock\Key; |
| 16 | +use Symfony\Component\Lock\SharedLockStoreInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * InMemoryStore is a PersistingStoreInterface implementation using |
| 20 | + * php-array to manage locks. |
| 21 | + * |
| 22 | + * @author Jérémy Derussé <[email protected]> |
| 23 | + */ |
| 24 | +class InMemoryStore implements SharedLockStoreInterface |
| 25 | +{ |
| 26 | + private $locks = []; |
| 27 | + private $readLocks = []; |
| 28 | + |
| 29 | + public function save(Key $key) |
| 30 | + { |
| 31 | + $hashKey = (string) $key; |
| 32 | + $token = $this->getUniqueToken($key); |
| 33 | + if (isset($this->locks[$hashKey])) { |
| 34 | + // already acquired |
| 35 | + if ($this->locks[$hashKey] === $token) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + throw new LockConflictedException(); |
| 40 | + } |
| 41 | + |
| 42 | + // check for promotion |
| 43 | + if (isset($this->readLocks[$hashKey][$token]) && 1 === \count($this->readLocks[$hashKey])) { |
| 44 | + unset($this->readLocks[$hashKey]); |
| 45 | + $this->locks[$hashKey] = $token; |
| 46 | + |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + if (\count($this->readLocks[$hashKey] ?? []) > 0) { |
| 51 | + throw new LockConflictedException(); |
| 52 | + } |
| 53 | + |
| 54 | + $this->locks[$hashKey] = $token; |
| 55 | + } |
| 56 | + |
| 57 | + public function saveRead(Key $key) |
| 58 | + { |
| 59 | + $hashKey = (string) $key; |
| 60 | + $token = $this->getUniqueToken($key); |
| 61 | + |
| 62 | + // check if lock is already acquired in read mode |
| 63 | + if (isset($this->readLocks[$hashKey])) { |
| 64 | + $this->readLocks[$hashKey][$token] = true; |
| 65 | + |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + // check for demotion |
| 70 | + if (isset($this->locks[$hashKey])) { |
| 71 | + if ($this->locks[$hashKey] !== $token) { |
| 72 | + throw new LockConflictedException(); |
| 73 | + } |
| 74 | + |
| 75 | + unset($this->locks[$hashKey]); |
| 76 | + } |
| 77 | + |
| 78 | + $this->readLocks[$hashKey][$token] = true; |
| 79 | + } |
| 80 | + |
| 81 | + public function putOffExpiration(Key $key, float $ttl) |
| 82 | + { |
| 83 | + // do nothing, memory locks forever. |
| 84 | + } |
| 85 | + |
| 86 | + public function delete(Key $key) |
| 87 | + { |
| 88 | + $hashKey = (string) $key; |
| 89 | + $token = $this->getUniqueToken($key); |
| 90 | + |
| 91 | + unset($this->readLocks[$hashKey][$token]); |
| 92 | + if (($this->locks[$hashKey] ?? null) === $token) { |
| 93 | + unset($this->locks[$hashKey]); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public function exists(Key $key) |
| 98 | + { |
| 99 | + $hashKey = (string) $key; |
| 100 | + $token = $this->getUniqueToken($key); |
| 101 | + |
| 102 | + return isset($this->readLocks[$hashKey][$token]) || ($this->locks[$hashKey] ?? null) === $token; |
| 103 | + } |
| 104 | + |
| 105 | + private function getUniqueToken(Key $key): string |
| 106 | + { |
| 107 | + if (!$key->hasState(__CLASS__)) { |
| 108 | + $token = base64_encode(random_bytes(32)); |
| 109 | + $key->setState(__CLASS__, $token); |
| 110 | + } |
| 111 | + |
| 112 | + return $key->getState(__CLASS__); |
| 113 | + } |
| 114 | +} |
0 commit comments