Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package cache

import (
"encoding/json"
"golang.org/x/net/context"
"time"
"user_system/config"
"user_system/internal/model"
"user_system/pkg/constant"
"user_system/utils"

"golang.org/x/net/context"
)

func GetUserInfoFromCache(username string) (*model.User, error) {
Expand All @@ -28,7 +29,7 @@ func SetUserCacheInfo(user *model.User) error {
return err
}
expired := time.Second * time.Duration(config.GetGlobalConf().Cache.UserExpired)
_, err = utils.GetRedisCli().Set(context.Background(), redisKey, val, expired*time.Second).Result()
_, err = utils.GetRedisCli().Set(context.Background(), redisKey, val, expired).Result()
return err
}

Expand All @@ -48,13 +49,13 @@ func GetSessionInfo(session string) (*model.User, error) {
return nil, err
}
user := &model.User{}
err = json.Unmarshal([]byte(val), &user)
err = json.Unmarshal([]byte(val), user)
return user, err
}

func SetSessionInfo(user *model.User, session string) error {
redisKey := constant.SessionKeyPrefix + session
val, err := json.Marshal(&user)
val, err := json.Marshal(user)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/router/router.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package router

import (
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"net/http"
"strconv"
api "user_system/api/http/v1"
"user_system/config"
"user_system/pkg/constant"

"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)

// InitRouterAndServe 路由配置、启动服务
Expand Down Expand Up @@ -58,6 +59,5 @@ func AuthMiddleWare() gin.HandlerFunc {
// 返回错误
c.JSON(http.StatusUnauthorized, gin.H{"error": "err"})
c.Abort()
return
}
}
13 changes: 7 additions & 6 deletions utils/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package utils

import (
"fmt"
"sync"
"user_system/config"

"github.com/redis/go-redis/v9"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
"sync"
"user_system/config"
)

var (
Expand All @@ -28,12 +29,12 @@ func initRedis() {
if redisConn == nil {
panic("failed to call redis.NewClient")
}
res, err := redisConn.Set(context.Background(), "abc", 100, 60).Result()
log.Infof("res=======%v,err======%v", res, err)
_, err = redisConn.Ping(context.Background()).Result()
_, err := redisConn.Ping(context.Background()).Result()
if err != nil {
panic("Failed to ping redis, err:%s")
panic("Failed to ping redis, err:" + err.Error())
}
res, err := redisConn.Set(context.Background(), "abc", 100, 60).Result()
log.Infof("res=======%v,err======%v", res, err)
}

func CloseRedis() {
Expand Down
5 changes: 3 additions & 2 deletions utils/redis_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package utils

import (
"golang.org/x/net/context"
"testing"
"time"
"user_system/config"

"golang.org/x/net/context"
)

func TestRedis(t *testing.T) {
Expand All @@ -16,7 +17,7 @@ func TestRedis(t *testing.T) {
}
t.Logf("res=%s", res)

val := GetRedisCli().Get(ctx, "2222").Val()
val, err := GetRedisCli().Get(ctx, "2222").Result()
if err != nil {
t.Errorf("redis get err:%v", err)
}
Expand Down