8000 [Lock] Better key naming when possible by sylfabre · Pull Request #60526 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
sylfabre committed Jun 18, 2025
commit 0993adb6eb98e7311e91266bf0e1eb41d87dd7ae
5 changes: 5 additions & 0 deletions src/Symfony/Component/Lock/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.4
---

* Use plain text (vs. hash) for short key names with database stores

7.3
---

Expand Down
7 changes: 4 additions & 3 deletions src/Symfony/Component/Lock/Store/DatabaseTableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ private function init(array $options, float $gcProbability, int $initialTtl): vo
}

/**
* Returns a hashed version of the key.
* Returns the key name in the database.
* It returns a sha256 hash, if the original key name is longer than 64 chars.
*/
private function getHashedKey(Key $key): string
private function getKeyName(string $key): string
{
return hash('sha256', (string) $key);
return strlen($key) < 64 ? $key : hash('sha256', $key);
}

private function getUniqueToken(Key $key): string
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/Lock/Store/DoctrineDbalStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function save(Key $key): void

try {
$this->conn->executeStatement($sql, [
$this->getHashedKey($key),
$this->getKeyName($key),
$this->getUniqueToken($key),
], [
ParameterType::STRING,
Expand All @@ -115,7 +115,7 @@ public function save(Key $key): void

try {
$this->conn->executeStatement($sql, [
$this->getHashedKey($key),
$this->getKeyName($key),
$this->getUniqueToken($key),
], [
ParameterType::STRING,
Expand Down Expand Up @@ -147,7 +147,7 @@ public function putOffExpiration(Key $key, $ttl): void
$result = $this->conn->executeQuery($sql, [
$ttl,
$uniqueToken,
$this->getHashedKey($key),
$this->getKeyName($key),
$uniqueToken,
], [
ParameterType::INTEGER,
Expand All @@ -167,7 +167,7 @@ public function putOffExpiration(Key $key, $ttl): void
public function delete(Key $key): void
{
$this->conn->delete($this->table, [
$this->idCol => $this->getHashedKey($key),
$this->idCol => $this->getKeyName($key),
$this->tokenCol => $this->getUniqueToken($key),
]);
}
Expand All @@ -176,7 +176,7 @@ public function exists(Key $key): bool
{
$sql = "SELECT 1 FROM $this->table WHERE $this->idCol = ? AND $this->tokenCol = ? AND $this->expirationCol > {$this->getCurrentTimestampStatement()}";
$result = $this->conn->fetchOne($sql, [
$this->getHashedKey($key),
$this->getKeyName($key),
$this->getUniqueToken($key),
], [
ParameterType::STRING,
7440 Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Lock/Store/PdoStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function save(Key $key): void
$stmt = $conn->prepare($sql);
}

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

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

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

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

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

Expand Down
Loading
0