Browse Source

✨ feat: 使用redis优化性能

Pchen. 2 months ago
parent
commit
92c177c36a
4 changed files with 61 additions and 30 deletions
  1. 4 0
      apis/GitActions/ChangeBranch.js
  2. 4 0
      apis/GitActions/GitPull.js
  3. 21 10
      apis/Repos/GetRepoLog.js
  4. 32 20
      apis/Repos/GitContributors.js

+ 4 - 0
apis/GitActions/ChangeBranch.js

@@ -2,6 +2,7 @@ const API = require("../../lib/API")
 const AccessControl = require("../../lib/AccessControl")
 const { BaseStdResponse } = require("../../BaseStdResponse")
 const db = require("../../plugin/DataBase/db")
+const redis = require('../../plugin/DataBase/Redis')
 const simpleGit = require('simple-git')
 
 class ChangeBranch extends API {
@@ -41,6 +42,9 @@ class ChangeBranch extends API {
             })
 
         try {
+            const redisKey = `gitLogs:${r[0].path}`
+            await redis.del(redisKey)
+
             const git = simpleGit()
             await git.cwd(r[0].path)
 

+ 4 - 0
apis/GitActions/GitPull.js

@@ -2,6 +2,7 @@ const API = require("../../lib/API")
 const AccessControl = require("../../lib/AccessControl")
 const { BaseStdResponse } = require("../../BaseStdResponse")
 const db = require("../../plugin/DataBase/db")
+const redis = require('../../plugin/DataBase/Redis')
 const simpleGit = require('simple-git')
 
 class GitPull extends API {
@@ -41,6 +42,9 @@ class GitPull extends API {
             })
 
         try {
+            const redisKey = `gitLogs:${r[0].path}`
+            await redis.del(redisKey)
+
             const git = simpleGit()
             await git.cwd(r[0].path)
             await git.pull('origin')

+ 21 - 10
apis/Repos/GetRepoLog.js

@@ -2,6 +2,7 @@ const API = require("../../lib/API")
 const AccessControl = require("../../lib/AccessControl")
 const { BaseStdResponse } = require("../../BaseStdResponse")
 const db = require("../../plugin/DataBase/db")
+const redis = require('../../plugin/DataBase/Redis')
 const simpleGit = require('simple-git')
 
 class GetRepoLog extends API {
@@ -41,15 +42,25 @@ class GetRepoLog extends API {
             })
 
         try {
-            const git = simpleGit()
-            await git.cwd(r[0].path)
-
-            const rawLogs = await git.raw([
-                "log",
-                "--no-merges",
-                "--pretty=format:%H|%an|%ae|%ad|%s",
-                "--shortstat"
-            ])
+            let rawLogs
+            const redisKey = `gitLogs:${r[0].path}`
+            const cachedLogs = await redis.get(redisKey)
+
+            if (cachedLogs)
+                rawLogs = JSON.parse(cachedLogs)
+            else {
+                const git = simpleGit()
+                await git.cwd(r[0].path)
+
+                rawLogs = await git.raw([
+                    "log",
+                    "--no-merges",
+                    "--pretty=format:%H|%an|%ae|%ad|%s",
+                    "--shortstat"
+                ])
+
+                redis.set(redisKey, 86400, JSON.stringify(logs))
+            }
 
             const lines = rawLogs.split("\n")
             const commits = []
@@ -66,7 +77,7 @@ class GetRepoLog extends API {
                         hash,
                         name,
                         email,
-                        date, 
+                        date,
                         message: messageParts.join("|").trim()
                     }
 

+ 32 - 20
apis/Repos/GitContributors.js

@@ -2,6 +2,7 @@ const API = require("../../lib/API")
 const AccessControl = require("../../lib/AccessControl")
 const { BaseStdResponse } = require("../../BaseStdResponse")
 const db = require("../../plugin/DataBase/db")
+const redis = require('../../plugin/DataBase/Redis')
 const simpleGit = require('simple-git')
 
 class GitContributors extends API {
@@ -14,24 +15,35 @@ class GitContributors extends API {
 
     async analyzeGitContributors(repoPath) {
         try {
-            const git = simpleGit()
-            await git.cwd(repoPath)
-    
-            // 获取详细日志,带上插入删除统计并排除 merge 提交
-            const logs = await git.raw([
-                "log",
-                "--no-merges",
-                "--pretty=format:%H|%an|%ae",
-                "--shortstat"
-            ])
-    
+            // 检查 Redis 中是否已存在缓存日志
+            const redisKey = `gitLogs:${repoPath}`
+            const cachedLogs = await redis.get(redisKey)
+
+            let logs
+            if (cachedLogs)
+                logs = JSON.parse(cachedLogs)
+            else {
+                const git = simpleGit()
+                await git.cwd(repoPath)
+
+                // 获取详细日志,带上插入删除统计并排除 merge 提交
+                logs = await git.raw([
+                    "log",
+                    "--no-merges",
+                    "--pretty=format:%H|%an|%ae",
+                    "--shortstat"
+                ])
+
+                redis.set(redisKey, 86400, JSON.stringify(logs))
+            }
+
             const lines = logs.split("\n")
             const contributors = {}
             let currentAuthor = null
-    
+
             for (let i = 0; i < lines.length; i++) {
                 const line = lines[i].trim()
-    
+
                 if (line.includes("|")) {
                     const [commitHash, name, email] = line.split("|")
                     currentAuthor = email // 用邮箱作为唯一 key 更稳妥
@@ -47,7 +59,7 @@ class GitContributors extends API {
                         }
                     }
                     contributors[currentAuthor].commits += 1
-    
+
                     // 接下来的 shortstat 统计信息在下一行
                     const statLine = lines[i + 1]?.trim() || ""
                     const statMatch = {
@@ -55,31 +67,31 @@ class GitContributors extends API {
                         insertions: statLine.match(/(\d+) insertion[s]?\(\+\)/),
                         deletions: statLine.match(/(\d+) deletion[s]?\(-\)/),
                     }
-    
+
                     const files = statMatch.filesChanged ? parseInt(statMatch.filesChanged[1], 10) : 0
                     const insertions = statMatch.insertions ? parseInt(statMatch.insertions[1], 10) : 0
                     const deletions = statMatch.deletions ? parseInt(statMatch.deletions[1], 10) : 0
-    
+
                     contributors[currentAuthor].filesChanged += files
                     contributors[currentAuthor].linesAdded += insertions
                     contributors[currentAuthor].linesDeleted += deletions
                     contributors[currentAuthor].linesChanged += insertions + deletions
-    
+
                     if (statLine) i++ // 跳过 shortstat 行
                 }
             }
-    
+
             const sorted = Object.values(contributors).sort(
                 (a, b) => b.linesChanged - a.linesChanged || b.commits - a.commits
             )
-    
+
             return sorted
         } catch (err) {
             console.error("分析 Git 贡献者失败:", err)
             throw new Error("分析 Git 贡献者失败")
         }
     }
-    
+
 
     async onRequest(req, res) {
         let { uuid, session, id } = req.query