代码拉取完成,页面将自动刷新
package ttlmap
import (
"sync"
"time"
"gitee.com/MikeDDD/gu/guutil"
"gitee.com/MikeDDD/gu/logs"
)
type entity struct {
key string
value any
expireAt int64
}
type TtlMap struct {
lock *sync.RWMutex
entities map[string]*entity
}
func New() *TtlMap {
m := &TtlMap{
lock: new(sync.RWMutex),
entities: make(map[string]*entity),
}
guutil.GoWithRecover(func() {
for range time.NewTicker(time.Second).C {
m.recover()
}
})
return m
}
func (t *TtlMap) recover() {
defer guutil.InDeferRecover()
t.lock.Lock()
defer t.lock.Unlock()
for k, v := range t.entities {
if time.Now().UnixMilli() > v.expireAt {
logs.Debug("recover %v", k)
delete(t.entities, k)
}
}
}
func (t *TtlMap) Set(key string, value any, ttl time.Duration) {
t.lock.Lock()
defer t.lock.Unlock()
t.entities[key] = &entity{
key: key,
value: value,
expireAt: time.Now().UnixMilli() + ttl.Milliseconds(),
}
}
func (t *TtlMap) SetSpecific(key string, value any, expireAt int64) {
t.lock.Lock()
defer t.lock.Unlock()
t.entities[key] = &entity{
key: key,
value: value,
expireAt: expireAt,
}
}
func (t *TtlMap) Get(key string) any {
t.lock.RLock()
defer t.lock.RUnlock()
v, exists := t.entities[key]
if !exists {
return nil
}
if time.Now().UnixMilli() >= v.expireAt {
return nil
}
return v.value
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。