Ver Fonte

添加redis配置

zhensolid há 1 ano atrás
pai
commit
3a463bb05d
3 ficheiros alterados com 65 adições e 30 exclusões
  1. 3 0
      add_record.php
  2. 20 3
      config.php
  3. 42 27
      search.php

+ 3 - 0
add_record.php

@@ -17,6 +17,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
         'comments' => $comments
     ]);
 
+    // 清除该用户的缓存
+    $redis->del("debt_info:" . $debtorName);
+
     header('Location: index.php');
     exit;
 }

+ 20 - 3
config.php

@@ -1,8 +1,8 @@
 <?php
 $host = 'localhost';
-$dbname = 'byc_mysql'; // 请替换为你的数据库名
-$username = 'kali';     // 请替换为你的数据库用户名
-$password = 'uKPAYkIZ2cFlKPN[';     // 请替换为你的数据库密码
+$dbname = 'byc_mysql';
+$username = 'kali';
+$password = 'uKPAYkIZ2cFlKPN[';
 
 try {
     $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
@@ -10,4 +10,21 @@ try {
 } catch (PDOException $e) {
     die("数据库连接失败: " . $e->getMessage());
 }
+
+// Redis 配置(不使用密码)
+$REDIS_ENABLED = false;
+$redis = null;
+
+if (extension_loaded('redis')) {
+    try {
+        $redis = new Redis();
+        if (@$redis->connect('localhost', 6379)) {
+            // 暂时注释掉密码认证
+            // $redis->auth('19841122Cy!');
+            $REDIS_ENABLED = true;
+        }
+    } catch (Exception $e) {
+        error_log("Redis连接失败: " . $e->getMessage());
+    }
+}
 ?>

+ 42 - 27
search.php

@@ -3,8 +3,19 @@ require 'config.php';
 
 if (isset($_GET['debtorName'])) {
     $debtorName = $_GET['debtorName'];
-
-    // 检查总欠款数据库中是否存在该姓名
+    
+    // 先尝试从Redis获取缓存
+    if ($REDIS_ENABLED) {
+        $cacheKey = "debt_info:" . $debtorName;
+        $cachedResult = $redis->get($cacheKey);
+        
+        if ($cachedResult) {
+            echo $cachedResult;
+            exit;
+        }
+    }
+    
+    // 如果没有缓存,则查询数据库
     $stmt = $pdo->prepare("SELECT * FROM Debtors WHERE debtor_name = :debtorName");
     $stmt->execute(['debtorName' => $debtorName]);
     $debtor = $stmt->fetch(PDO::FETCH_ASSOC);
@@ -20,41 +31,45 @@ if (isset($_GET['debtorName'])) {
         $viewStmt->execute(['debtorName' => $debtorName]);
         $viewData = $viewStmt->fetch(PDO::FETCH_ASSOC);
 
-        echo "<div class='search-result'>";
-
-        // 更新后的输出格式
-        echo "<div class='result-header'>";
-        echo "<h2 class='debtor-name'>{$debtor['debtor_name']}</h2>";
-        echo "<p class='remaining-debt'>当前欠款余额: <span class='amount'>¥{$viewData['剩余欠款金额']}</span></p>";
-        echo "</div>";
+        // 构建输出
+        $output = "<div class='search-result'>";
+        $output .= "<div class='result-header'>";
+        $output .= "<h2 class='debtor-name'>{$debtor['debtor_name']}</h2>";
+        $output .= "<p class='remaining-debt'>当前欠款余额: <span class='amount'>¥{$viewData['剩余欠款金额']}</span></p>";
+        $output .= "</div>";
 
-        echo "<div class='repayment-section'>";
-        echo "<h3>还款记录</h3>";
+        $output .= "<div class='repayment-section'>";
+        $output .= "<h3>还款记录</h3>";
 
         if ($repayments) {
-            echo "<div class='repayment-list'>";
+            $output .= "<div class='repayment-list'>";
             foreach ($repayments as $repayment) {
-                echo "<div class='repayment-item'>";
-                echo "<div class='repayment-date'><span class='label'>日期:</span> {$repayment['repayment_date']}</div>";
-                echo "<div class='repayment-details'>";
-                echo "<div class='repayment-amount'><span class='label'>金额:</span> ¥{$repayment['repayment_amount']}</div>";
-                echo "<div class='repayment-method'><span class='label'>支付方式:</span> {$repayment['repayment_method']}</div>";
+                $output .= "<div class='repayment-item'>";
+                $output .= "<div class='repayment-date'><span class='label'>日期:</span> {$repayment['repayment_date']}</div>";
+                $output .= "<div class='repayment-details'>";
+                $output .= "<div class='repayment-amount'><span class='label'>金额:</span> ¥{$repayment['repayment_amount']}</div>";
+                $output .= "<div class='repayment-method'><span class='label'>支付方式:</span> {$repayment['repayment_method']}</div>";
                 if (!empty($repayment['comments'])) {
-                    echo "<div class='repayment-comment'><span class='label'>备注:</span> {$repayment['comments']}</div>";
+                    $output .= "<div class='repayment-comment'><span class='label'>备注:</span> {$repayment['comments']}</div>";
                 }
-                echo "</div>";
-                echo "</div>";
+                $output .= "</div>";
+                $output .= "</div>";
             }
-            echo "</div>";
+            $output .= "</div>";
         } else {
-            echo "<p class='no-repayments'>暂无还款记录</p>";
+            $output .= "<p class='no-repayments'>暂无还款记录</p>";
         }
-        echo "</div>";
-        echo "</div>";
+        $output .= "</div>";
+        $output .= "</div>";
+
+        // 如果Redis可用,缓存结果
+        if ($REDIS_ENABLED) {
+            $redis->setex($cacheKey, 60, $output);
+        }
+
+        echo $output;
     } else {
-        echo "<div class='no-result'><i class='fas fa-exclamation-circle'></i> 未找到相关记录</div>";
+        echo "<p class='no-result'>未找到相关记录</p>";
     }
-} else {
-    echo "<div class='no-input'><i class='fas fa-info-circle'></i> 请输入姓名进行查询</div>";
 }
 ?>