Add cache for auth info

This commit is contained in:
cuigh 2017-12-05 12:19:32 +08:00
parent aaec9d54df
commit 805140a16b
3 changed files with 23 additions and 1 deletions

View File

@ -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
}

View File

@ -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

View File

@ -9,6 +9,7 @@ import (
"github.com/cuigh/auxo/app"
"github.com/cuigh/auxo/app/flag"
_ "github.com/cuigh/auxo/cache/memory"
"github.com/cuigh/auxo/config"
"github.com/cuigh/auxo/data/valid"
"github.com/cuigh/auxo/net/web"