Skip to content

Commit

Permalink
Merge pull request #84 from Penryn/main
Browse files Browse the repository at this point in the history
perf(room): 增加空教室查询接口的缓存功能
  • Loading branch information
XiMo-210 authored Nov 7, 2024
2 parents 07267c0 + 90242c3 commit 656d4c8
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions app/controllers/funcControllers/zfController/zfController.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package zfController

import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"math/rand"
"time"
Expand All @@ -10,6 +12,7 @@ import (
"wejh-go/app/services/sessionServices"
"wejh-go/app/services/userServices"
"wejh-go/app/utils"
"wejh-go/config/redis"
)

type form struct {
Expand Down Expand Up @@ -170,14 +173,41 @@ func GetRoom(c *gin.Context) {
return
}

// 使用 Redis 缓存键,包含查询参数
cacheKey := fmt.Sprintf("room:%s:%s:%s:%s:%s:%s", postForm.Year, postForm.Term, postForm.Campus, postForm.Weekday, postForm.Week, postForm.Sections)

// 从 Redis 中获取缓存结果
cachedResult, cacheErr := redis.RedisClient.Get(c, cacheKey).Result()
if cacheErr == nil {
var result []map[string]interface{}
if err := json.Unmarshal([]byte(cachedResult), &result); err == nil {
utils.JsonSuccessResponse(c, result)
return
} else {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
}

result, err := funnelServices.GetRoom(user, postForm.Year, postForm.Term, postForm.Campus, postForm.Weekday, postForm.Week, postForm.Sections, loginType)
if err != nil {
if err == apiException.NoThatPasswordOrWrong {
userServices.DelPassword(user, loginType)
_ = c.AbortWithError(200, err)
return
}
_ = c.AbortWithError(200, err)
return
}
// 将结果缓存到 Redis 中
if result != nil {
resultJson, _ := json.Marshal(result)
err = redis.RedisClient.Set(c, cacheKey, string(resultJson), 1*time.Hour).Err()
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
}
utils.JsonSuccessResponse(c, result)
}

Expand Down

0 comments on commit 656d4c8

Please sign in to comment.