Skip to content

Commit e1901e5

Browse files
committed
Better key naming when possible
Use plain text for key name when they are short enough. It helps to understand where inserted locks come from when looking at the database.
1 parent 6027ca0 commit e1901e5

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

src/Symfony/Component/Lock/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add support for `valkey:` / `valkeys:` schemes
8+
* Use plain text (vs hash) for short key names with database stores
89

910
7.2
1011
---

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ private function init(array $options, float $gcProbability, int $initialTtl): vo
4848
/**
4949
* Returns a hashed version of the key.
5050
*/
51-
private function getHashedKey(Key $key): string
51+
private function getKeyName(Key $key): string
5252
{
53+
$keyAsString = (string) $key;
54+
if (strlen($keyAsString) <= 64) {
55+
return $keyAsString;
56+
}
5357
return hash('sha256', (string) $key);
5458
}
5559

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function save(Key $key): void
102102

103103
try {
104104
$this->conn->executeStatement($sql, [
105-
$this->getHashedKey($key),
105+
$this->getKeyName($key),
106106
$this->getUniqueToken($key),
107107
], [
108108
ParameterType::STRING,
@@ -115,7 +115,7 @@ public function save(Key $key): void
115115

116116
try {
117117
$this->conn->executeStatement($sql, [
118-
$this->getHashedKey($key),
118+
$this->getKeyName($key),
119119
$this->getUniqueToken($key),
120120
], [
121121
ParameterType::STRING,
@@ -147,7 +147,7 @@ public function putOffExpiration(Key $key, $ttl): void
147147
$result = $this->conn->executeQuery($sql, [
148148
$ttl,
149149
$uniqueToken,
150-
$this->getHashedKey($key),
150+
$this->getKeyName($key),
151151
$uniqueToken,
152152
], [
153153
ParameterType::INTEGER,
@@ -167,7 +167,7 @@ public function putOffExpiration(Key $key, $ttl): void
167167
public function delete(Key $key): void
168168
{
169169
$this->conn->delete($this->table, [
170-
$this->idCol => $this->getHashedKey($key),
170+
$this->idCol => $this->getKeyName($key),
171171
$this->tokenCol => $this->getUniqueToken($key),
172172
]);
173173
}
@@ -176,7 +176,7 @@ public function exists(Key $key): bool
176176
{
177177
$sql = "SELECT 1 FROM $this->table WHERE $this->idCol = ? AND $this->tokenCol = ? AND $this->expirationCol > {$this->getCurrentTimestampStatement()}";
178178
$result = $this->conn->fetchOne($sql, [
179-
$this->getHashedKey($key),
179+
$this->getKeyName($key),
180180
$this->getUniqueToken($key),
181181
], [
182182
ParameterType::STRING,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function save(Key $key): void
9898
$stmt = $conn->prepare($sql);
9999
}
100100

101-
$stmt->bindValue(':id', $this->getHashedKey($key));
101+
$stmt->bindValue(':id', $this->getKeyName($key));
102102
$stmt->bindValue(':token', $this->getUniqueToken($key));
103103

104104
try {
@@ -134,7 +134,7 @@ public function putOffExpiration(Key $key, float $ttl): void
134134
$stmt = $this->getConnection()->prepare($sql);
135135

136136
$uniqueToken = $this->getUniqueToken($key);
137-
$stmt->bindValue(':id', $this->getHashedKey($key));
137+
$stmt->bindValue(':id', $this->getKeyName($key));
138138
$stmt->bindValue(':token1', $uniqueToken);
139139
$stmt->bindValue(':token2', $uniqueToken);
140140
$result = $stmt->execute();
@@ -152,7 +152,7 @@ public function delete(Key $key): void
152152
$sql = "DELETE FROM $this->table WHERE $this->idCol = :id AND $this->tokenCol = :token";
153153
$stmt = $this->getConnection()->prepare($sql);
154154

155-
$stmt->bindValue(':id', $this->getHashedKey($key));
155+
$stmt->bindValue(':id', $this->getKeyName($key));
156156
$stmt->bindValue(':token', $this->getUniqueToken($key));
157157
$stmt->execute();
158158
}
@@ -162,7 +162,7 @@ public function exists(Key $key): bool
162162
$sql = "SELECT 1 FROM $this->table WHERE $this->idCol = :id AND $this->tokenCol = :token AND $this->expirationCol > {$this->getCurrentTimestampStatement()}";
163163
$stmt = $this->getConnection()->prepare($sql);
164164

165-
$stmt->bindValue(':id', $this->getHashedKey($key));
165+
$stmt->bindValue(':id', $this->getKeyName($key));
166166
$stmt->bindValue(':token', $this->getUniqueToken($key));
167167
$result = $stmt->execute();
168168

0 commit comments

Comments
 (0)