Skip to content

Commit

Permalink
✨ 成绩查询缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhenShaw committed Jul 8, 2020
1 parent 753aac9 commit 43b88b1
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 50 deletions.
61 changes: 61 additions & 0 deletions Server/env/redis.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package env

import (
"encoding/json"
"fmt"
"github.com/astaxie/beego/logs"
"github.com/go-redis/redis"
_ "golang.org/x/sync/singleflight"
"time"
)

var RedisCli *redis.Client
Expand All @@ -31,3 +33,62 @@ func InitRedis() (err error) {
return
}

type CacheOptions struct {
Key string
Duration time.Duration
Fun func() (interface{}, error) //用于获取结果数据的函数,错误向上传递
Receiver interface{} //初始化的 结构体指针或者map,用来存放结果
}

//获取缓存,不存在则调用fun获取数据加入缓存,fun的错误向上传递
func GetSetCache(c *CacheOptions) (using bool, err error) {

if c == nil || c.Receiver == nil || c.Key == "" {
err = fmt.Errorf("illegal arguments")
logs.Error(err)
return
}

//====查询缓存
val, err := RedisCli.Get(c.Key).Result()
if err != nil && err != redis.Nil {
logs.Error(err)
return
}

if err == redis.Nil {
//调用函数获取数据
c.Receiver, err = c.Fun()
if err != nil {
return
}
if fmt.Sprint(c.Receiver) == "<nil>" {
return false, nil
}

//加入缓存
logs.Debug("Set cache %s", c.Key)
var buf []byte
buf, err = json.Marshal(&c.Receiver)
if err != nil {
logs.Error(err)
return
}
err = RedisCli.Set(c.Key, string(buf), c.Duration).Err()
if err != nil {
logs.Error(err)
return
}

} else {
//解析缓存
using = true
logs.Debug("Hit cache %s", c.Key)
err = json.Unmarshal([]byte(val), &c.Receiver)
if err != nil {
logs.Error(err)
return
}
}
return
}
88 changes: 38 additions & 50 deletions Server/routers/jw.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func Course(w http.ResponseWriter, r *http.Request) {
}
}

pkg.SetDemoCache("course", client.GetUsername(), data)
go pkg.SetDemoCache("course", client.GetUsername(), data)
Response(w, r, data, http.StatusOK, "request ok")

//====响应后的处理
Expand Down Expand Up @@ -299,11 +299,12 @@ func Exam(w http.ResponseWriter, r *http.Request) {
Response(w, r, nil, http.StatusInternalServerError, err.Error())
return
}
pkg.SetDemoCache("exam", client.GetUsername(), data)
go pkg.SetDemoCache("exam", client.GetUsername(), data)
Response(w, r, data, http.StatusOK, "request ok")
}

func Grade(w http.ResponseWriter, r *http.Request) {

//从context提取客户端
c := r.Context().Value("client")
if c == nil {
Expand All @@ -316,7 +317,16 @@ func Grade(w http.ResponseWriter, r *http.Request) {
return
}

data, err := client.GetAllGrade("", "")
//====缓存处理
var gs = &env.CacheOptions{
Key: fmt.Sprintf("gzhupi:grade:%s", client.GetUsername()),
Duration: 15 * time.Minute,
Receiver: new(gzhu_jw.GradeData),
Fun: func() (interface{}, error) {
return client.GetAllGrade("", "")
},
}
usingCache, err := env.GetSetCache(gs)
if err != nil {
logs.Error(err)
Jwxt.Delete(getCacheKey(r, client.GetUsername()))
Expand All @@ -327,10 +337,12 @@ func Grade(w http.ResponseWriter, r *http.Request) {
Response(w, r, nil, http.StatusInternalServerError, err.Error())
return
}
pkg.SetDemoCache("grade", client.GetUsername(), data)
var data = gs.Receiver.(*gzhu_jw.GradeData)

go pkg.SetDemoCache("grade", client.GetUsername(), data)
Response(w, r, data, http.StatusOK, "request ok")

if data == nil || data.StuInfo == nil {
if data == nil || data.StuInfo == nil || usingCache {
return
}

Expand Down Expand Up @@ -396,7 +408,7 @@ func EmptyRoom(w http.ResponseWriter, r *http.Request) {
Response(w, r, nil, http.StatusInternalServerError, err.Error())
return
}
pkg.SetDemoCache("empty-room", client.GetUsername(), data)
go pkg.SetDemoCache("empty-room", client.GetUsername(), data)
Response(w, r, data, http.StatusOK, "request ok")
}

Expand All @@ -420,7 +432,7 @@ func Achieve(w http.ResponseWriter, r *http.Request) {
Response(w, r, nil, http.StatusInternalServerError, err.Error())
return
}
pkg.SetDemoCache("achieve", client.GetUsername(), data)
go pkg.SetDemoCache("achieve", client.GetUsername(), data)
Response(w, r, data, http.StatusOK, "request ok")
}

Expand Down Expand Up @@ -477,54 +489,30 @@ func Rank(w http.ResponseWriter, r *http.Request) {
}
logs.Info("用户:%s IP: %s 接口:%s ", username, reqip.GetClientIP(r), r.URL.Path)

var data map[string]interface{}
var client pkg.Jwxt
client = &gzhu_jw.JWClient{Username: username}
if isTestUser(username) {
client = &pkg.Demo{Username: username}
}

//====查询缓存
key := fmt.Sprintf("gzhupi:rank:%s", username)
val, err := env.RedisCli.Get(key).Result()
if err != nil && err != redis.Nil {
//====缓存处理
var gs = &env.CacheOptions{
Key: fmt.Sprintf("gzhupi:rank:%s", username),
Duration: 15 * time.Minute,
Receiver: make(map[string]interface{}),
Fun: func() (interface{}, error) {
return client.GetRank(client.GetUsername())
},
}
_, err = env.GetSetCache(gs)
if err != nil {
logs.Error(err)
Response(w, r, nil, http.StatusInternalServerError, err.Error())
return
}
if err == redis.Nil {

var client pkg.Jwxt
client = &gzhu_jw.JWClient{Username: username}
if isTestUser(username) {
client = &pkg.Demo{Username: username}
}
data, err = client.GetRank(client.GetUsername())
if err != nil {
logs.Error(err)
Response(w, r, nil, http.StatusInternalServerError, err.Error())
return
}
//加入缓存
logs.Debug("Set cache %s", key)
buf, err := json.Marshal(&data)
if err != nil {
logs.Error(err)
Response(w, r, nil, http.StatusInternalServerError, err.Error())
return
}
//缓存
err = env.RedisCli.Set(key, string(buf), 10*time.Minute).Err()
if err != nil {
logs.Error(err)
Response(w, r, nil, http.StatusInternalServerError, err.Error())
return
}
} else {
//解析缓存
logs.Debug("Hit cache %s", key)
err = json.Unmarshal([]byte(val), &data)
if err != nil {
logs.Error(err)
Response(w, r, nil, http.StatusInternalServerError, err.Error())
return
}
}
pkg.SetDemoCache("rank", username, data)
var data = gs.Receiver.(map[string]interface{})

go pkg.SetDemoCache("rank", username, data)
Response(w, r, data, http.StatusOK, "request ok")
}

0 comments on commit 43b88b1

Please sign in to comment.