Skip to content

JWT UserInfo 缓存问题 #16

@azhengyongqin

Description

@azhengyongqin

作者这里的的缓存 InitUserInfo() 方法有问题吧

如果用 user_key 作为缓存的 key :

  1. 第二帐户登录的时候,缓存会覆盖。
  2. 如果这时候,第一个帐户的 token没有过期,然后在 task 中调用 GetUserInfo() , 拿到的是第二个用户的信息。

var userKey string = "user_key"
type UserInfo struct {
Id uint `json:"id"`
}
func InitUserInfo(ctx context.Context, user *UserInfo) error {
cachedUser, err := GetUserInfo(ctx)
if err != nil {
return err
}
// 如果缓存中存在用户信息,直接返回
if cachedUser != nil {
return nil
}
// 如果缓存中不存在用户信息,将用户信息存储到缓存中
if err := SetUserInCache(ctx, user, cache.RedisClient); err != nil {
return err
}
return nil
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions