1 Star 0 Fork 0

h79 / gothird

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
item.go 2.07 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2024-01-20 13:58 . LOG
package token
import (
"fmt"
"gitee.com/h79/gothird/token/validate"
"gitee.com/h79/goutils/common/logger"
"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()
if v, found := it.vMap[key]; found {
it.vmu.RUnlock()
return v
}
it.vmu.RUnlock()
return nil
}
func (it *item) readData(key string) string {
if it == nil {
return ""
}
it.vmu.RLock()
if v, found := it.vMap[key]; found {
it.vmu.RUnlock()
return v.Data
}
it.vmu.RUnlock()
return ""
}
func (it *item) write(key string, v *validate.Data) {
logger.D("Token", "write, id= %s,appId=%s,validate= %v", it.App.Id, it.App.AppId, 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) {
logger.D("Token", "clear, id=%s, appId=%s, key=%s", it.App.Id, it.App.AppId, 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, fmt.Errorf("not found for key= '%s'", key)
}
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 {
logger.W("Token", "expire, id=%s, appId=%s, key=%s", it.App.Id, it.App.AppId, 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
}
1
https://gitee.com/h79/gothird.git
git@gitee.com:h79/gothird.git
h79
gothird
gothird
v1.8.103

搜索帮助