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