Skip to content

[Lock] Better key naming when possible #60526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
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 May 23, 2025
commit e1901e5cdebc64fa40f0f9c48b7ad389be2b3ef4
1 change: 1 addition & 0 deletions src/Symfony/Component/Lock/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

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

7.2
---
Expand Down
6 changes: 5 additions & 1 deletion src/Symfony/Component/Lock/Store/DatabaseTableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ private function init(array $options, float $gcProbability, int $initialTtl): vo
/**
* Returns a hashed version of the key.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be adjusted

*/
private function getHashedKey(Key $key): string
private function getKeyName(Key $key): string
{
$keyAsString = (string) $key;
if (strlen($keyAsString) <= 64) {
return $keyAsString;
}
return hash('sha256', (string) $key);
Comment on lines +51 to 57
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd keep the original name + leverage PHP to do the cast:

Suggested change
private function getKeyName(Key $key): string
{
$keyAsString = (string) $key;
if (strlen($keyAsString) <= 64) {
return $keyAsString;
}
return hash('sha256', (string) $key);
private function getHashedKey(string $key): string
{
return strlen($keyAsString) <= 64 ? $key : hash('sha256', $key);

}

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,
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