mirror of
https://github.com/cuigh/swirl
synced 2025-03-03 10:41:31 +00:00
Add cache for auth info
This commit is contained in:
parent
aaec9d54df
commit
805140a16b
13
biz/user.go
13
biz/user.go
@ -6,6 +6,7 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/cuigh/auxo/cache"
|
||||
"github.com/cuigh/auxo/errors"
|
||||
"github.com/cuigh/auxo/log"
|
||||
"github.com/cuigh/auxo/net/web"
|
||||
@ -306,6 +307,8 @@ func (b *userBiz) ldapSearchOne(setting *model.Setting, l *ldap.Conn, name strin
|
||||
|
||||
// Identify authenticate user
|
||||
func (b *userBiz) Identify(token string) (user web.User) {
|
||||
const cacheKey = "auth_user"
|
||||
|
||||
do(func(d dao.Interface) {
|
||||
var (
|
||||
roles []*model.Role
|
||||
@ -321,6 +324,15 @@ func (b *userBiz) Identify(token string) (user web.User) {
|
||||
return
|
||||
}
|
||||
|
||||
value := cache.Get(cacheKey, session.UserID)
|
||||
if !value.IsNil() {
|
||||
user = &model.AuthUser{}
|
||||
if err = value.Scan(user); err == nil {
|
||||
return
|
||||
}
|
||||
log.Get("user").Warnf("Load auth user from cache failed: %v", err)
|
||||
}
|
||||
|
||||
u, err := d.UserGetByID(session.UserID)
|
||||
if err != nil {
|
||||
log.Get("user").Errorf("Load user failed: %v", err)
|
||||
@ -342,6 +354,7 @@ func (b *userBiz) Identify(token string) (user web.User) {
|
||||
}
|
||||
}
|
||||
user = model.NewAuthUser(u, roles)
|
||||
cache.Set(user, cacheKey, session.UserID)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
@ -19,4 +19,12 @@ log:
|
||||
writers:
|
||||
- name: console
|
||||
type: console
|
||||
layout: '[{L}]{T}: {M}{N}'
|
||||
layout: '[{L}]{T}: {M}{N}'
|
||||
|
||||
cache:
|
||||
- provider: memory
|
||||
enabled: true
|
||||
error: log
|
||||
time: 10m
|
||||
keys:
|
||||
auth_user: 3m
|
||||
|
Loading…
Reference in New Issue
Block a user