You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.3 KiB
62 lines
1.3 KiB
package cacheApi
|
|
|
|
import (
|
|
"epur-pay/model"
|
|
"epur-pay/pkg/rds"
|
|
"epur-pay/pkg/utils"
|
|
"github.com/muesli/cache2go"
|
|
"time"
|
|
)
|
|
|
|
type User struct {
|
|
Data *cache2go.CacheTable
|
|
duplicate *User
|
|
}
|
|
|
|
func (p *User) Refresh() *User {
|
|
p.duplicate = &User{}
|
|
p.duplicate.Data = cache2go.Cache("userList")
|
|
return p.duplicate
|
|
}
|
|
|
|
func (p *User) RefreshRow(u *model.User) *model.User {
|
|
p.Data.Add(u.Uid, time.Duration(7*24*60*60)*time.Second, u)
|
|
return u
|
|
}
|
|
|
|
func (p *User) RefreshBatchRow(u ...*model.User) {
|
|
for index, _ := range u {
|
|
p.RefreshRow(u[index])
|
|
}
|
|
}
|
|
|
|
func (p *User) RefreshSelectRow(uid int64) *model.User {
|
|
user := model.User{}
|
|
utils.DbErrSkipRecordNotFound(rds.DB.Raw(`select * from user where uid = ?`, uid).Scan(&user).Error)
|
|
|
|
if user.Uid <= 0 {
|
|
return nil
|
|
}
|
|
// 获取用户拥有的权限
|
|
utils.DbErrSkipRecordNotFound(rds.DB.Table(model.UserRole{}.TableName()).Select(`role_id`).
|
|
Where(`uid = ?`, user.Uid).Scan(&user.RoleIds).Error)
|
|
|
|
// 刷新缓存
|
|
p.RefreshRow(&user)
|
|
return &user
|
|
}
|
|
|
|
func (p *User) RefreshSelectBatchRow(uid ...int64) {
|
|
for _, item := range uid {
|
|
p.RefreshSelectRow(item)
|
|
}
|
|
}
|
|
|
|
func (p *User) Get(uid int64) *model.User {
|
|
if row, err := p.Data.Value(uid); err == nil {
|
|
return row.Data().(*model.User)
|
|
} else {
|
|
return p.RefreshSelectRow(uid)
|
|
}
|
|
}
|