fix: bug
This commit is contained in:
@@ -1,14 +1,31 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
function redisClient(): Redis
|
function redisClient(): ?Redis
|
||||||
{
|
{
|
||||||
static $redis = null;
|
static $redis = null;
|
||||||
|
static $initialized = false;
|
||||||
|
|
||||||
|
if ($initialized) {
|
||||||
|
return $redis;
|
||||||
|
}
|
||||||
|
|
||||||
|
$initialized = true;
|
||||||
|
|
||||||
|
if (!class_exists('Redis')) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
if ($redis === null) {
|
if ($redis === null) {
|
||||||
$config = require __DIR__ . '/../config/config.php';
|
$config = require __DIR__ . '/../config/config.php';
|
||||||
$redis = new Redis();
|
$redis = new Redis();
|
||||||
if (!@$redis->connect($config['redis']['host'], $config['redis']['port'])) {
|
try {
|
||||||
throw new RuntimeException('Redis connection failed.');
|
if (!@$redis->connect($config['redis']['host'], $config['redis']['port'])) {
|
||||||
|
$redis = null;
|
||||||
|
}
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
$redis = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $redis;
|
return $redis;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,12 +21,24 @@ class Paste
|
|||||||
if ($expire_time !== null) {
|
if ($expire_time !== null) {
|
||||||
$ttl = max(1, $expire_time - time());
|
$ttl = max(1, $expire_time - time());
|
||||||
$redis = redisClient();
|
$redis = redisClient();
|
||||||
$redis->setex("paste:{$id}", $ttl, json_encode([
|
if ($redis !== null) {
|
||||||
'encrypted_text' => $encrypted_text,
|
try {
|
||||||
'iv' => $iv,
|
$redis->setex("paste:{$id}", $ttl, json_encode([
|
||||||
'password_hash' => $password_hash,
|
'encrypted_text' => $encrypted_text,
|
||||||
]));
|
'iv' => $iv,
|
||||||
return true;
|
'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(
|
$stmt = $this->pdo->prepare(
|
||||||
@@ -39,16 +51,21 @@ class Paste
|
|||||||
public function get(string $id): array|false
|
public function get(string $id): array|false
|
||||||
{
|
{
|
||||||
$redis = redisClient();
|
$redis = redisClient();
|
||||||
$raw = $redis->get("paste:{$id}");
|
if ($redis !== null) {
|
||||||
|
try {
|
||||||
if ($raw !== false) {
|
$raw = $redis->get("paste:{$id}");
|
||||||
$json = json_decode($raw, true);
|
if ($raw !== false) {
|
||||||
return [
|
$json = json_decode($raw, true);
|
||||||
'encrypted_text' => $json['encrypted_text'],
|
return [
|
||||||
'iv' => $json['iv'],
|
'encrypted_text' => $json['encrypted_text'],
|
||||||
'password_hash' => $json['password_hash'],
|
'iv' => $json['iv'],
|
||||||
'expire_time' => time() + $redis->ttl("paste:{$id}"),
|
'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');
|
$stmt = $this->pdo->prepare('SELECT * FROM pastes WHERE id = ? LIMIT 1');
|
||||||
|
|||||||
Reference in New Issue
Block a user