Skip to content

Commit e2f0faa

Browse files
committed
Address feedback
1 parent 9a32d39 commit e2f0faa

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

src/Symfony/Component/Lock/Store/MysqlStore.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ class MysqlStore implements PersistingStoreInterface
3131

3232
private int $connectionId;
3333

34+
private \PDOStatement $saveStmt;
35+
36+
private \PDOStatement $existsStmt;
37+
38+
private \PDOStatement $deleteStmt;
39+
3440
public function __construct(\PDO|string $connOrDsn, array $options = [])
3541
{
3642
if ($connOrDsn instanceof \PDO) {
@@ -52,10 +58,11 @@ public function save(Key $key): void
5258
return;
5359
}
5460

61+
$stmt = $this->saveStmt ??
62+
$this->saveStmt = $this->getConnection()->prepare('SELECT IF(IS_USED_LOCK(:name) = CONNECTION_ID(), -1, GET_LOCK(:name, 0))');
63+
5564
$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]);
5966
$result = $stmt->fetchColumn();
6067

6168
// lock acquired
@@ -83,9 +90,10 @@ public function putOffExpiration(Key $key, float $ttl): void
8390

8491
public function delete(Key $key): void
8592
{
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)]);
8997

9098
$key->removeState($this->getStateKey($key));
9199
}
@@ -97,18 +105,19 @@ public function exists(Key $key): bool
97105
return false;
98106
}
99107

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)]);
103112
$result = $stmt->fetchColumn();
104113

105-
if (1 === $result) {
106-
return true;
107-
}
114+
if (1 !== $result) {
115+
$key->removeState($stateKey);
108116

109-
$key->removeState($stateKey);
117+
return false;
118+
}
110119

111-
return false;
120+
return true;
112121
}
113122

114123
private function getConnection(): \PDO

0 commit comments

Comments
 (0)