代码拉取完成,页面将自动刷新
package token
import (
"encoding/gob"
"fmt"
"gitee.com/h79/goutils/common/logger"
"gitee.com/h79/goutils/common/system"
"time"
)
const (
fAll = 1
fReadId = 2
fVMap = 3
fApp = 4
ownerGob = "/owner.gob"
)
type AppItem struct {
item
conf Config
itemPath string
fUpdate chan int
}
type ConfigOptions func(conf *Config)
func NewAppItem(app App, tk Token, options ...ConfigOptions) *AppItem {
return newAppItem(&app, tk, VMap{}, options...)
}
func newAppItem(app *App, tk Token, vMap VMap, options ...ConfigOptions) *AppItem {
//for create file directory
ap := &AppItem{
item: item{
App: app,
tk: tk,
vMap: vMap,
appAt: updateTime{updateAt: time.Now().Unix()},
},
fUpdate: make(chan int, 10),
}
if len(vMap) > 0 {
//为了触发保存
ap.vMapAt.Update()
}
for i := range options {
options[i](&ap.conf)
}
ap.itemPath = fmt.Sprintf("%s/%s/%s", rootPath, ap.App.Type, fileId(ap.App.Id))
if err := checkPath(ap.itemPath); err != nil {
logger.E("Token", "create directory path failure, err= %v", err)
}
return ap
}
func (app *AppItem) changeSecret(secret string) {
app.App.Secret = secret
if app.tk != nil {
app.tk.SetSecret(secret)
}
app.appAt.Update()
app.fUpdate <- fApp
}
func (app *AppItem) start() {
var count = 0
sys := system.Exit()
minute := time.NewTicker(time.Minute)
for {
select {
case <-minute.C:
app.checkExpire()
if count%5 == 0 {
//5分钟保存一次
count = 0
app.saveOwner()
}
count++
case f := <-app.fUpdate:
if f == fAll {
app.save()
}
if f == fVMap {
app.saveOwner()
}
if f == fApp {
app.saveApp()
}
case <-sys.Done():
return
}
}
}
func (app *AppItem) checkExpire() {
//自已的值
if app.vMapExpire() {
app.itemUpdated(fVMap)
}
}
func (app *AppItem) itemUpdated(f int) {
app.fUpdate <- f
}
func (app *AppItem) load() {
if !app.conf.IsLocal() {
return
}
if len(app.itemPath) == 0 {
return
}
app.loadOwner()
}
func (app *AppItem) loadOwner() {
filename := app.itemPath + ownerGob
app.vmu.Lock()
defer app.vmu.Unlock()
err := loadFile(filename, func(de *gob.Decoder) error {
return de.Decode(&app.vMap)
})
if err != nil {
logger.E("Token", "loadOwner path= %s, id= %s, appId= %s, err= %s",
app.itemPath, app.App.Id, app.App.AppId, err)
return
}
app.vMapAt.Complete()
logger.D("Token", "loadOwner path= %s, id= %s, appId= %s",
app.itemPath, app.App.Id, app.App.AppId)
for _, v := range app.vMap {
logger.D("Token", "--loadOwner data= %s", v.String())
}
}
func (app *AppItem) saveOwner() {
app.vmu.Lock()
defer app.vmu.Unlock()
if !app.vMapAt.HasUpdate() {
return
}
if err := app.checkPath(); err != nil {
return
}
filename := app.itemPath + ownerGob
err := saveFile(filename, func(en *gob.Encoder) error {
return en.Encode(app.vMap)
})
if err != nil {
logger.E("Token", "saveOwner path= %s, file= %s, id= %s, appId= %s, err= %s",
filename, fileId(app.App.Id), app.App.Id, app.App.AppId, err)
return
}
app.vMapAt.Complete()
}
func (app *AppItem) saveApp() {
if !app.conf.IsLocal() {
return
}
if !app.appAt.HasUpdate() {
return
}
filename := fmt.Sprintf("%s/%s.gob", appPath, fileId(app.App.Id))
err := saveFile(filename, func(en *gob.Encoder) error {
return en.Encode(app.App)
})
if err != nil {
logger.E("Token", "saveApp path= %s, file= %s, id= %s, appId= %s, err= %s",
filename, fileId(app.App.Id), app.App.Id, app.App.AppId, err)
return
}
app.appAt.Complete()
}
func (app *AppItem) save() {
app.saveApp()
app.saveOwner()
}
func (app *AppItem) checkPath() error {
if !app.conf.IsLocal() {
return ErrNotSupported
}
return checkPath(app.itemPath)
}
func (app *AppItem) Path() string {
return fileId(app.App.Id)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。