代码拉取完成,页面将自动刷新
package utils
import (
"sync"
"time"
)
type KeyValueCache interface {
Set(k, v string, tm *time.Time) error
Get(k string) (string, bool)
Del(k string) error
}
type MapCacheItem struct {
Value string
Expire *time.Time
}
type MapCache struct {
val sync.Map
}
func (m *MapCache) Set(k, v string, tm *time.Time) error {
m.val.Store(k, &MapCacheItem{
Value: v,
Expire: tm,
})
return nil
}
func (m *MapCache) Get(k string) (string, bool) {
if vr, ok := m.val.Load(k); ok {
if v, ok := vr.(*MapCacheItem); ok {
if v.Expire == nil || v.Expire.After(time.Now()) {
return v.Value, true
}
}
}
return "", false
}
func (m *MapCache) Del(k string) error {
m.val.LoadAndDelete(k)
return nil
}
func NewMapCache() *MapCache {
return &MapCache{}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。