diff --git a/app/core/redis.php b/app/core/redis.php index f848d93..8dff58e 100644 --- a/app/core/redis.php +++ b/app/core/redis.php @@ -1,14 +1,31 @@ connect($config['redis']['host'], $config['redis']['port'])) { - throw new RuntimeException('Redis connection failed.'); + try { + if (!@$redis->connect($config['redis']['host'], $config['redis']['port'])) { + $redis = null; + } + } catch (Throwable $e) { + $redis = null; } } + return $redis; } diff --git a/app/models/Paste.php b/app/models/Paste.php index ecf512b..04729ed 100644 --- a/app/models/Paste.php +++ b/app/models/Paste.php @@ -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');