Browse Source

🎈 perf: 在更多场景使用redis

Pchen. 2 months ago
parent
commit
24beec5bfd

+ 1 - 1
apis/GitActions/ChangeBranch.js

@@ -42,7 +42,7 @@ class ChangeBranch extends API {
             })
 
         try {
-            const redisKey = `gitLogs:${r[0].path}`
+            const redisKey = [`gitLogs:${r[0].path}`,`contributors:${r[0].path}`,`codeStats:${r[0].path}`]
             await redis.del(redisKey)
 
             const git = simpleGit()

+ 2 - 2
apis/GitActions/GitPull.js

@@ -42,12 +42,12 @@ class GitPull extends API {
             })
 
         try {
-            const redisKey = `gitLogs:${r[0].path}`
+            const redisKey = [`gitLogs:${r[0].path}`,`contributors:${r[0].path}`,`codeStats:${r[0].path}`]
             await redis.del(redisKey)
 
             const git = simpleGit()
             await git.cwd(r[0].path)
-            await git.pull('origin')
+            await git.pull()
 
             res.json({
                 ...BaseStdResponse.OK

+ 1 - 1
apis/Repos/DeleteRepo.js

@@ -31,7 +31,7 @@ class DeleteRepo extends API {
         let r = await db.query(sql, [uuid, id])
 
         if (r && r[0].path) {
-            const redisKey = `gitLogs:${r[0].path}`
+            const redisKey = [`gitLogs:${r[0].path}`,`contributors:${r[0].path}`,`codeStats:${r[0].path}`]
             await redis.del(redisKey)
 
             fs.rm(r[0].path, { recursive: true, force: true }, (err) => { 

+ 14 - 4
apis/Repos/GitCodeStats.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')
 const fs = require("fs")
 const path = require("path")
@@ -173,6 +174,13 @@ class gitCodeStats extends API {
                 msg: '仓库未成功克隆!'
             })
 
+        const cachedCodeStats = await redis.get(`codeStats:${r[0].path}`)
+        if(cachedCodeStats)
+            return res.json({
+                ...BaseStdResponse.OK,
+                data: JSON.parse(cachedCodeStats)
+            })
+
         let totalLines = 0
         let languageStats = {}
         try {
@@ -206,12 +214,14 @@ class gitCodeStats extends API {
             })
         }
 
+        const data = { totalLines, languageStats }
+
         res.json({
             ...BaseStdResponse.OK,
-            data: {
-                totalLines,
-                languageStats
-            }
+            data
+        })
+        redis.set(`codeStats:${r[0].path}`, JSON.stringify(data), {
+            EX: 172800
         })
     }
 }

+ 9 - 1
apis/Repos/GitContributors.js

@@ -125,6 +125,12 @@ class GitContributors extends API {
 
         let contributors
         try {
+            const cachedContributors = await redis.get(`contributors:${r[0].path}`)
+            if (cachedContributors)
+                return res.json({
+                    ...BaseStdResponse.OK,
+                    data: JSON.parse(contributors)
+                })
             contributors = await this.analyzeGitContributors(r[0].path)
         } catch (error) {
             this.logger.error('获取仓库开发者失败!' + error.stack)
@@ -138,7 +144,9 @@ class GitContributors extends API {
             ...BaseStdResponse.OK,
             data: contributors
         })
-
+        redis.set(`contributors:${r[0].path}`, JSON.stringify(contributors), {
+            EX: 172800
+        })
     }
 }