42 lines
906 B
Go
42 lines
906 B
Go
package service
|
||
|
||
import (
|
||
"VideoStream/dao"
|
||
"VideoStream/worker"
|
||
"encoding/json"
|
||
"fmt"
|
||
"strconv"
|
||
)
|
||
|
||
// 获取用户信息,有redis缓存
|
||
func GetUserByIDWithCache(id int) dao.User {
|
||
if id <= 0 {
|
||
return dao.User{}
|
||
}
|
||
var user dao.User
|
||
//先从redis获取
|
||
key := "user_info_" + strconv.Itoa(id)
|
||
user_str := worker.GetRedis(key)
|
||
if user_str != "" {
|
||
err := json.Unmarshal([]byte(user_str), &user)
|
||
if err != nil {
|
||
fmt.Println("get user info , json unmarshal error:", err, "\tuser_str:", user_str)
|
||
return dao.User{}
|
||
}
|
||
} else {
|
||
user = dao.FindUserByID2(id)
|
||
if user.ID != 0 {
|
||
userJson, err := json.Marshal(user)
|
||
if err != nil {
|
||
fmt.Println("get user info , json marshal error:", err)
|
||
return dao.User{}
|
||
}
|
||
success := worker.SetRedis(key, string(userJson))
|
||
if !success {
|
||
fmt.Println("set redis error,user json:", string(userJson))
|
||
}
|
||
}
|
||
}
|
||
return user
|
||
}
|