@@ -31,9 +31,6 @@ class MysqlStore implements PersistingStoreInterface
31
31
32
32
private int $ connectionId ;
33
33
34
- /** @var bool[] */
35
- private static array $ locksAcquired = [];
36
-
37
34
public function __construct (\PDO |string $ connOrDsn , array $ options = [])
38
35
{
39
36
if ($ connOrDsn instanceof \PDO ) {
@@ -51,20 +48,20 @@ public function __construct(\PDO|string $connOrDsn, array $options = [])
51
48
52
49
public function save (Key $ key ): void
53
50
{
54
- $ id = $ this ->getLockId ($ key );
55
-
56
- if (self ::$ locksAcquired [$ id ] ?? false ) {
51
+ $ stateKey = $ this ->getStateKey ($ key );
52
+ if ($ key ->hasState ($ stateKey )) {
57
53
return ;
58
54
}
59
55
56
+ $ name = self ::getLockName ($ key );
60
57
$ stmt = $ this ->conn ->prepare ('SELECT IF(IS_USED_LOCK(:name) = CONNECTION_ID(), -1, GET_LOCK(:name, 0)) ' );
61
- $ stmt ->bindValue (':name ' , self :: getLockName ( $ key ) , \PDO ::PARAM_STR );
58
+ $ stmt ->bindValue (':name ' , $ name , \PDO ::PARAM_STR );
62
59
$ stmt ->execute ();
63
60
$ result = $ stmt ->fetchColumn ();
64
61
65
62
// lock acquired
66
63
if (1 === $ result ) {
67
- self :: $ locksAcquired [ $ id ] = true ;
64
+ $ key -> setState ( $ stateKey , $ name ) ;
68
65
69
66
return ;
70
67
}
@@ -91,12 +88,12 @@ public function delete(Key $key): void
91
88
$ stmt ->bindValue (':name ' , self ::getLockName ($ key ), \PDO ::PARAM_STR );
92
89
$ stmt ->execute ();
93
90
94
- unset( self :: $ locksAcquired [ $ this ->getLockId ($ key )] );
91
+ $ key -> removeState ( $ this ->getStateKey ($ key ));
95
92
}
96
93
97
94
public function exists (Key $ key ): bool
98
95
{
99
- return self :: $ locksAcquired [ $ this ->getLockId ($ key )] ?? false ;
96
+ return $ key -> hasState ( $ this ->getStateKey ($ key )) ;
100
97
}
101
98
102
99
private function getConnection (): \PDO
@@ -122,13 +119,13 @@ private function assertMysqlDriver(): void
122
119
}
123
120
}
124
121
125
- private function getLockId (Key $ key ): string
122
+ private function getStateKey (Key $ key ): string
126
123
{
127
124
if (!isset ($ this ->connectionId )) {
128
125
$ this ->connectionId = $ this ->getConnection ()->query ('SELECT CONNECTION_ID() ' )->fetchColumn ();
129
126
}
130
127
131
- return $ this -> connectionId .'_ ' .spl_object_id ( $ key ) ;
128
+ return __CLASS__ .'_ ' .$ this -> connectionId ;
132
129
}
133
130
134
131
private static function getLockName (Key $ key ): string
0 commit comments