This commit is contained in:
2026-07-10 02:39:19 +03:30
parent d5639f05fc
commit 2b3c03b62a
2 changed files with 53 additions and 19 deletions

View File

@@ -21,12 +21,24 @@ class Paste
if ($expire_time !== null) {
$ttl = max(1, $expire_time - time());
$redis = redisClient();
$redis->setex("paste:{$id}", $ttl, json_encode([
'encrypted_text' => $encrypted_text,
'iv' => $iv,
'password_hash' => $password_hash,
]));
return true;
if ($redis !== null) {
try {
$redis->setex("paste:{$id}", $ttl, json_encode([
'encrypted_text' => $encrypted_text,
'iv' => $iv,
'password_hash' => $password_hash,
]));
return true;
} catch (Throwable $e) {
// Fall back to MySQL so expiring pastes still work without Redis.
}
}
$stmt = $this->pdo->prepare(
'INSERT INTO pastes (id, encrypted_text, iv, expire_time, password_hash)
VALUES (?, ?, ?, ?, ?)'
);
return $stmt->execute([$id, $encrypted_text, $iv, $expire_time, $password_hash]);
}
$stmt = $this->pdo->prepare(
@@ -39,16 +51,21 @@ class Paste
public function get(string $id): array|false
{
$redis = redisClient();
$raw = $redis->get("paste:{$id}");
if ($raw !== false) {
$json = json_decode($raw, true);
return [
'encrypted_text' => $json['encrypted_text'],
'iv' => $json['iv'],
'password_hash' => $json['password_hash'],
'expire_time' => time() + $redis->ttl("paste:{$id}"),
];
if ($redis !== null) {
try {
$raw = $redis->get("paste:{$id}");
if ($raw !== false) {
$json = json_decode($raw, true);
return [
'encrypted_text' => $json['encrypted_text'],
'iv' => $json['iv'],
'password_hash' => $json['password_hash'],
'expire_time' => time() + $redis->ttl("paste:{$id}"),
];
}
} catch (Throwable $e) {
// Ignore Redis failures and fall back to MySQL.
}
}
$stmt = $this->pdo->prepare('SELECT * FROM pastes WHERE id = ? LIMIT 1');