8000 Better key naming when possible · symfony/symfony@8508d8e · GitHub
[go: up one dir, main page]

Skip to content

Commit 8508d8e

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 50d7b25 commit 8508d8e

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

src/Symfony/Component/Lock/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.4
5+
---
6+
7+
* Use plain text (vs hash) for short key names with database stores
8+
49
7.3
510
---
611

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ private function init(array $options, float $gcProbability, int $initialTtl): vo
4646
}
4747

4848
/**
49-
* Returns a hashed version of the key.
49+
* Returns the key name in the database.
50+
* It returns a sha256 hash if the original key name is longer than 64 chars.
5051
*/
51-
private function getHashedKey(Key $key): string
52+
private function getKeyName(string $key): string
5253
{
53-
return hash('sha256', (string) $key);
54+
return strlen($key) < 64 ? $key : hash('sha256', $key);
5455
}
5556

5657
private function getUniqueToken(Key $key): string

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)
0