1 Star 0 Fork 0

h79/gothird

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
item.go 2.08 KB
一键复制 编辑 原始数据 按行查看 历史
package token
import (
"gitee.com/h79/gothird/token/validate"
"go.uber.org/zap"
"sync"
)
type item struct {
App *App
appAt updateTime
tk Token
vMapAt updateTime
vmu sync.RWMutex
vMap VMap
}
func (it *item) refresh(data string) {
go func(data string) {
_, _ = it.tk.RefreshAccessToken(data)
}(data)
}
func (it *item) read(key string) *validate.Data {
if it == nil {
return &validate.Empty
}
it.vmu.RLock()
defer it.vmu.RUnlock()
if v, found := it.vMap[key]; found {
return v
}
return nil
}
func (it *item) readData(key string) string {
if it == nil {
return ""
}
it.vmu.RLock()
defer it.vmu.RUnlock()
if v, found := it.vMap[key]; found {
return v.Data
}
return ""
}
func (it *item) write(key string, v *validate.Data) {
zap.L().Debug("Token:write", zap.String("id", it.App.Id), zap.String("appId", it.App.AppId), zap.Any("validate", v))
it.vmu.Lock()
defer it.vmu.Unlock()
if d, found := it.vMap[key]; found {
d.Data = v.Data
d.Expire = v.Expire
} else {
it.vMap[key] = v
}
it.vMapAt.Update()
}
func (it *item) clear(key string) (*validate.Data, error) {
zap.L().Debug("Token:clear", zap.String("id", it.App.Id), zap.String("appId", it.App.AppId), zap.Any("key", key))
it.vmu.Lock()
defer it.vmu.Unlock()
if d, found := it.vMap[key]; found {
delete(it.vMap, key)
it.vMapAt.Update()
return d, nil
}
return nil, ErrNotFound
}
func (it *item) vMapExpire() bool {
fRefresh := false
keys := make([]string, 0)
// 这个如果数据太大,会有效率问题,需要改进。
it.vmu.RLock()
for k, v := range it.vMap {
if v.IsValid() && v.IsExpired() {
keys = append(keys, k)
}
}
it.vmu.RUnlock()
for i := range keys {
zap.L().Warn("Token:expire", zap.String("id", it.App.Id), zap.String("appId", it.App.AppId), zap.Any("key", keys[i]))
switch keys[i] {
case NRefreshToken:
it.refresh(it.readData(keys[i]))
case NAccessToken:
it.refresh("")
if _, err := it.clear(keys[i]); err == nil {
fRefresh = true
}
default:
if _, err := it.clear(keys[i]); err == nil {
fRefresh = true
}
}
}
return fRefresh
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/h79/gothird.git
git@gitee.com:h79/gothird.git
h79
gothird
gothird
v1.8.25

搜索帮助