Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JWT UserInfo 缓存问题 #16

Open
azhengyongqin opened this issue Mar 11, 2024 · 0 comments
Open

JWT UserInfo 缓存问题 #16

azhengyongqin opened this issue Mar 11, 2024 · 0 comments

Comments

@azhengyongqin
Copy link

作者这里的的缓存 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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant