@@ -31,6 +31,12 @@ class MysqlStore implements PersistingStoreInterface
31
31
32
32
private int $ connectionId ;
33
33
34
+ private \PDOStatement $ saveStmt ;
35
+
36
+ private \PDOStatement $ existsStmt ;
37
+
38
+ private \PDOStatement $ deleteStmt ;
39
+
34
40
public function __construct (\PDO |string $ connOrDsn , array $ options = [])
35
41
{
36
42
if ($ connOrDsn instanceof \PDO ) {
@@ -52,10 +58,11 @@ public function save(Key $key): void
52
58
return ;
53
59
}
54
60
61
+ $ stmt = $ this ->saveStmt ??
62
+ $ this ->saveStmt = $ this ->getConnection ()->prepare ('SELECT IF(IS_USED_LOCK(:name) = CONNECTION_ID(), -1, GET_LOCK(:name, 0)) ' );
63
+
55
64
$ name = self ::getLockName ($ key );
56
- $ stmt = $ this ->getConnection ()->prepare ('SELECT IF(IS_USED_LOCK(:name) = CONNECTION_ID(), -1, GET_LOCK(:name, 0)) ' );
57
- $ stmt ->bindValue (':name ' , $ name , \PDO ::PARAM_STR );
58
- $ stmt ->execute ();
65
+ $ stmt ->execute (['name ' => $ name ]);
59
66
$ result = $ stmt ->fetchColumn ();
60
67
61
68
// lock acquired
@@ -83,9 +90,10 @@ public function putOffExpiration(Key $key, float $ttl): void
83
90
84
91
public function delete (Key $ key ): void
85
92
{
86
- $ stmt = $ this ->getConnection ()->prepare ('DO RELEASE_LOCK(:name) ' );
87
- $ stmt ->bindValue (':name ' , self ::getLockName ($ key ), \PDO ::PARAM_STR );
88
- $ stmt ->execute ();
93
+ $ stmt = $ this ->deleteStmt ??
94
+ $ this ->deleteStmt = $ this ->getConnection ()->prepare ('DO RELEASE_LOCK(:name) ' );
95
+
96
+ $ stmt ->execute (['name ' => self ::getLockName ($ key )]);
89
97
90
98
$ key ->removeState ($ this ->getStateKey ($ key ));
91
99
}
@@ -97,18 +105,19 @@ public function exists(Key $key): bool
97
105
return false ;
98
106
}
99
107
100
- $ stmt = $ this ->getConnection ()->prepare ('SELECT IF(IS_USED_LOCK(:name) = CONNECTION_ID(), 1, 0) ' );
101
- $ stmt ->bindValue (':name ' , self ::getLockName ($ key ), \PDO ::PARAM_STR );
102
- $ stmt ->execute ();
108
+ $ stmt = $ this ->existsStmt ??
109
+ $ this ->existsStmt = $ this ->getConnection ()->prepare ('SELECT IS_USED_LOCK(:name) = CONNECTION_ID() ' );
110
+
111
+ $ stmt ->execute (['name ' => self ::getLockName ($ key )]);
103
112
$ result = $ stmt ->fetchColumn ();
104
113
105
- if (1 === $ result ) {
106
- return true ;
107
- }
114
+ if (1 !== $ result ) {
115
+ $ key ->removeState ($ stateKey );
108
116
109
- $ key ->removeState ($ stateKey );
117
+ return false ;
118
+ }
110
119
111
- return false ;
120
+ return true ;
112
121
}
113
122
114
123
private function getConnection (): \PDO
0 commit comments