enhance(user):增加专门用于用户登录的用户缓存设计。

This commit is contained in:
徐涛 2022-08-29 08:15:38 +08:00
parent 6e3632a381
commit afea03a8e3
2 changed files with 28 additions and 2 deletions

View File

@ -19,3 +19,17 @@ type User struct {
func (User) TableName() string { func (User) TableName() string {
return "user" return "user"
} }
type UserWithCredentials struct {
Created `xorm:"extends"`
Id string `xorm:"varchar(120) pk not null" json:"id"`
Username string `xorm:"varchar(30) not null" json:"username"`
Password string `xorm:"varchar(256) not null" json:"credential"`
ResetNeeded bool `xorm:"bool not null" json:"resetNeeded"`
Type int8 `xorm:"smallint not null" json:"type"`
Enabled bool `xorm:"bool not null" json:"enabled"`
}
func (UserWithCredentials) TableName() string {
return "user"
}

View File

@ -21,7 +21,7 @@ type _UserService struct{}
var UserService _UserService var UserService _UserService
func (u _UserService) ProcessEnterpriseUserLogin(username, password string) (*model.Session, error) { func (u _UserService) ProcessEnterpriseUserLogin(username, password string) (*model.Session, error) {
user, err := u.findUserByUsername(username) user, err := u.findUserWithCredentialsByUsername(username)
if err != nil { if err != nil {
return nil, err return nil, err
@ -63,7 +63,7 @@ func (u _UserService) ProcessEnterpriseUserLogin(username, password string) (*mo
} }
func (u _UserService) ProcessManagementUserLogin(username, password string) (*model.Session, error) { func (u _UserService) ProcessManagementUserLogin(username, password string) (*model.Session, error) {
user, err := u.findUserByUsername(username) user, err := u.findUserWithCredentialsByUsername(username)
if err != nil { if err != nil {
return nil, err return nil, err
@ -266,6 +266,18 @@ func (_UserService) SearchLimitUsers(keyword string, limit int) ([]model.JoinedU
return users, nil return users, nil
} }
func (_UserService) findUserWithCredentialsByUsername(username string) (*model.UserWithCredentials, error) {
if cachedUser, _ := cache.RetreiveSearch[model.UserWithCredentials]("user_with_credentials", username); cachedUser != nil {
return cachedUser, nil
}
user := new(model.UserWithCredentials)
has, err := global.DBConn.Where(builder.Eq{"username": username}).NoAutoCondition().Get(user)
if has {
cache.CacheSearch(*user, []string{"user"}, "user_with_credentials", username)
}
return _postProcessSingle(user, has, err)
}
func (_UserService) findUserByUsername(username string) (*model.User, error) { func (_UserService) findUserByUsername(username string) (*model.User, error) {
if cachedUser, _ := cache.RetreiveSearch[model.User]("user", username); cachedUser != nil { if cachedUser, _ := cache.RetreiveSearch[model.User]("user", username); cachedUser != nil {
return cachedUser, nil return cachedUser, nil