- 用SyncMapWithTimeout管理tasksch任务

This commit is contained in:
gazebo
2019-08-07 14:55:54 +08:00
parent 8768fe8236
commit f06456cec1
3 changed files with 40 additions and 34 deletions

View File

@@ -0,0 +1,29 @@
package jxutils
import (
"sync"
"time"
"git.rosy.net.cn/baseapi/utils"
)
type SyncMapWithTimeout struct {
sync.Map
timers sync.Map
}
func (m *SyncMapWithTimeout) StoreWithTimeout(key, value interface{}, timeout time.Duration) {
m.Map.Store(key, value)
m.timers.Store(key, utils.AfterFuncWithRecover(timeout, func() {
m.Delete(key)
}))
}
func (m *SyncMapWithTimeout) Delete(key interface{}) {
m.Map.Delete(key)
if value, ok := m.timers.Load(key); ok {
timer := value.(*time.Timer)
timer.Stop()
}
m.timers.Delete(key)
}